Search code examples
pythonursina

How can I restart the game by pressing "r"?


My plans are to restart the game / reset the character to the middle of the map.

import keyboard as kb
from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController

app = Ursina()
window.fps_counter.enabled = False
player = FirstPersonController()
Sky()

boxes = []

def random_color():
red = random.Random().random() * 20
green = random.Random().random() * 255
blue = random.Random().random() * 20
return color.rgb(red, green, blue)

def add_box(position):
boxes.append(
Button(
parent=scene,
model='cube',
origin=0.5,
color=random_color(),
position=position,
texture='grass'
)
)

for x in range(20):
for y in range(20):
add_box( (x, 0, y) )

def input(key):
for box in boxes:
if box.hovered:
if key == "right mouse down":
add_box(box.position + mouse.normal)
if key == "left mouse down":
boxes.remove(box)
destroy(box)
if kb.is_pressed("escape"):
exit()
if kb.is_pressed("shift"):
player.speed = 15
else:
player.speed = 7

app.run()

I can´t find help for this issue in the whole world of the internet. Please help me.


Solution

  • What do you mean with reset? Reset the hole game or just the player position?

    To reset player position you can do something like this:

    from ursina import *
    
    app = Ursina()
    
    # Change this to whatever your player is. In your case First person controller 
    player = Entity(model = 'cube')
    
    player_reset_position = (0,0,0)
    def input(key):
        if key == 'r':
            player.position = player_reset_position
    
    app.run()
    
    

    Your code with some fixes:

    from ursina import *
    from ursina.prefabs.first_person_controller import FirstPersonController
    
    app = Ursina()
    window.fps_counter.enabled = False
    player = FirstPersonController()
    Sky()
    
    boxes = []
    
    def random_color():
        red = random.Random().random() * 20
        green = random.Random().random() * 255
        blue = random.Random().random() * 20
        return color.rgb(red, green, blue)
    
    def add_box(position):
        boxes.append(
        Button(
            parent=scene,
            model='cube',
            origin=0.5,
            color=random_color(),
            position=position,
            texture='grass'
        )
    )
    
    for x in range(20):
        for y in range(20):
            add_box((x, 0, y))
    
    player_reset_position = (5,10,5)
    
    def input(key):
        for box in boxes:
            if box.hovered:
                if key == "right mouse down":
                    add_box(box.position + mouse.normal)
                if key == "left mouse down":
                    boxes.remove(box)
                    destroy(box)
    
        if key == ("escape"):
            exit()
        if key == ("shift"):
            player.speed = 15
        else:
            player.speed = 7
    
        if key == 'r':
            player.position = player_reset_position
    app.run()
    

    I have changed indentation and your input function where you had if kb.is_pressed("escape"): to if key == "escape":