Functions and Event Handling - Quiz
Quiz: Understanding Functions and Event Handling
Quiz 1: the update() function
update() When is the function called?
Show Answer
Answer: B) Automatically every frame
update() The function is called automatically every frame while the game is running.
If the game runs at 60 FPS, it's called 60 times per second!
Quiz 2: structure of a Sequence
Show Answer
Answer: B) Sequence(Func(shoot), Wait(...), loop=True)
Sequence runs several actions in order:
Quiz 3: why use time.dt
player.position += direction * speed * time.dtin time.dtWhy do you use it?
Show Answer
Answer: C) To move at a constant speed regardless of computer performance
time.dt is the time interval between frames.
- 60 FPS computer: time.dt โ 0.0167 seconds
- 30 FPS computer: time.dt โ 0.0333 seconds
time.dt โ multiply by it and on any computer you the same distance per second move!
Quiz 4: key events in the input function
Hint: it's in the form "keyname up".
Show Answer
Answer: space up
There are two kinds of keyboard input events:
def input(key):
if key == 'space': # When a key is pressed
print("Pressed!")
if key == 'space up': # When a key is released
print("Released!")
It works the same for other keys: 'a', 'a up', 'w', 'w up' etc.
Quiz 5: updating a mesh
Show Answer
Answer: generate
After changing a mesh's vertices, you must always generate() call it:
Next step
Congratulations! You now understand the core concepts of functions and event handling.
What you learned in this lesson:
update()Running code every frame with a functionSequence,Func,WaitCreating repeating actions with itinput()Handling keyboard input with a function- Creating particle effects by manipulating a mesh's vertices
Try it yourself!
Run the code and try changing various values!
# Change the firing interval
shoot_cooldown = .05 # Faster firing (0.1 โ 0.05)
# Change the player speed
player.speed = 12 # Faster movement (8 โ 12)
# Change the bullet color
player.bullet_renderer.color = color.red # Red bullets
Small changes like these completely change the feel of the game!
Challenge
Try adding these features:
- Adding enemies:
enemies.append()Creating more enemies with it - Score system: increase the score when you defeat an enemy
- Explosion effect: add a particle effect when an enemy dies
๐งฉ Reviewing the Object concept
- An object is a LEGO block something that has both properties and behavior.
- A variable is sticky note, a function is Instruction manual for behaviors, and an object is a toy that combines the two.
- Ursina's Entity is an object too: it has both properties like model, color, and scale, and behaviors like animate_position().
- Putting objects into a list gives you a basket that you can pull from when needed and process all at once.