Skip to content

Functions and Event Handling - Quiz


Quiz: Understanding Functions and Event Handling

Quiz 1: the update() function

Quiz: 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!

def update():
    # This code runs every frame
    player.x += speed * time.dt

Quiz 2: structure of a Sequence

Quiz: structure of a Sequence
Which object helps the bullets keep firing?
Show Answer

Answer: B) Sequence(Func(shoot), Wait(...), loop=True)

Sequence runs several actions in order:

shoot_sequence = Sequence(
    Func(shoot),       # 1. Run the shoot function
    Wait(0.1),         # 2. Wait 0.1 seconds
    loop=True          # 3. Back to step 1 (loop forever)
)

Quiz 3: why use time.dt

Quiz: 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

Quiz: key events
What is the name of the key event that fires **when the spacebar is released**?
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

Quiz: updating a mesh
After changing a mesh's vertices list, what is the name of the function you must call to reflect it on screen?
Show Answer

Answer: generate

After changing a mesh's vertices, you must always generate() call it:

# Add a vertex
mesh.vertices.append(Vec3(0, 0, 0))

# Delete a vertex
mesh.vertices.remove(some_vertex)

# Reflect the changes on screen
mesh.generate()  # Without this line, the screen won't update!

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 function
  • Sequence, Func, WaitCreating repeating actions with it
  • input() 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:

  1. Adding enemies: enemies.append()Creating more enemies with it
  2. Score system: increase the score when you defeat an enemy
  3. Explosion effect: add a particle effect when an enemy dies
# Score system example
score = 0
score_text = Text(text='Score: 0', position=(-0.85, 0.45))

# Increase score when an enemy is defeated
if enemy.hp <= 0:
    score += 100
    score_text.text = f'Score: {score}'
๐Ÿงฉ 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.