First event handler, keypress reverses direction.
import arcade class MyGame(arcade.Window): def __init__(self, width, height, title, bg_color): super().__init__(width, height, title) arcade.set_background_color(bg_color) self.width = width self.height = height self.position = 0 self.velocity = 1 self.radius = 30 def reverse(self): self.velocity *= -1 def on_draw(self): arcade.start_render() y = self.height / 2 arcade.draw_circle_filled(self.position, y, 30, arcade.color.RED) def update(self, delta_time): self.position += self.velocity * delta_time # Did the circle hit the right/left side of screen? is_at_right = self.position > self.width - self.radius is_at_left = self.position < self.radius if is_at_right or is_at_left: self.reverse() def on_key_press(self, key, modifiers): if key == arcade.key.LEFT or key == arcade.key.RIGHT: self.reverse() def main(): game = MyGame(600, 600, 'Drawing Example', arcade.color.WHEAT) game.position = 1 game.velocity = 200 arcade.run() if __name__ == '__main__': main()
- We make a method
reversethat let’s us do the work for reversing without thinking much about it.
- Combine the two tests – right edge and left edge – into one
- Add a method
on_key_press, which Arcade calls whenever a key is pressed
- This method is known as a handler…it handles events
- Arcade passes information from the event, into this this method, as arguments
- You can use this to decide which key was pressed.
- Add an
elsethat prints information about what key was pressed when it was not one of those two keys.
- No need for left or right…just have spacebar perform a reverse.