Sprite: Face Left or Right#
1"""
2Sprite Facing Left or Right
3Face left or right depending on our direction
4
5Simple program to show basic sprite usage.
6
7Artwork from https://kenney.nl
8
9If Python and Arcade are installed, this example can be run from the command line with:
10python -m arcade.examples.sprite_face_left_or_right
11"""
12
13from __future__ import annotations
14
15import arcade
16
17SPRITE_SCALING = 0.5
18
19SCREEN_WIDTH = 800
20SCREEN_HEIGHT = 600
21SCREEN_TITLE = "Sprite Face Left or Right Example"
22
23MOVEMENT_SPEED = 5
24
25# Index of textures, first element faces left, second faces right
26TEXTURE_LEFT = 0
27TEXTURE_RIGHT = 1
28
29
30class Player(arcade.Sprite):
31
32 def __init__(self):
33 left, right = arcade.load_texture_pair(":resources:images/enemies/bee.png")
34 super().__init__(left, scale=SPRITE_SCALING)
35 self.textures.append(right)
36
37 def update(self):
38 self.center_x += self.change_x
39 self.center_y += self.change_y
40
41 # Figure out if we should face left or right
42 if self.change_x < 0:
43 self.texture = self.textures[TEXTURE_LEFT]
44 elif self.change_x > 0:
45 self.texture = self.textures[TEXTURE_RIGHT]
46
47
48class MyGame(arcade.Window):
49 """
50 Main application class.
51 """
52
53 def __init__(self, width, height, title):
54 """
55 Initializer
56 """
57
58 # Call the parent class initializer
59 super().__init__(width, height, title)
60
61 # Variables that will hold sprite lists
62 self.player_sprite_list = None
63
64 # Set up the player info
65 self.player_sprite = None
66
67 # Set the background color
68 self.background_color = arcade.color.AMAZON
69
70 def setup(self):
71 """ Set up the game and initialize the variables. """
72
73 # Sprite lists
74 self.player_sprite_list = arcade.SpriteList()
75
76 # Set up the player
77 self.player_sprite = Player()
78 self.player_sprite.center_x = SCREEN_WIDTH / 2
79 self.player_sprite.center_y = SCREEN_HEIGHT / 2
80 self.player_sprite_list.append(self.player_sprite)
81
82 def on_draw(self):
83 """
84 Render the screen.
85 """
86
87 # This command has to happen before we start drawing
88 self.clear()
89
90 # Draw all the sprites.
91 self.player_sprite_list.draw()
92
93 def on_update(self, delta_time):
94 """ Movement and game logic """
95
96 # Call update on all sprites (The sprites don't do much in this
97 # example though.)
98 self.player_sprite_list.update()
99
100 def on_key_press(self, key, modifiers):
101 """Called whenever a key is pressed. """
102
103 if key == arcade.key.UP:
104 self.player_sprite.change_y = MOVEMENT_SPEED
105 elif key == arcade.key.DOWN:
106 self.player_sprite.change_y = -MOVEMENT_SPEED
107 elif key == arcade.key.LEFT:
108 self.player_sprite.change_x = -MOVEMENT_SPEED
109 elif key == arcade.key.RIGHT:
110 self.player_sprite.change_x = MOVEMENT_SPEED
111
112 def on_key_release(self, key, modifiers):
113 """Called when the user releases a key. """
114
115 if key == arcade.key.UP or key == arcade.key.DOWN:
116 self.player_sprite.change_y = 0
117 elif key == arcade.key.LEFT or key == arcade.key.RIGHT:
118 self.player_sprite.change_x = 0
119
120
121def main():
122 """ Main function """
123 window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
124 window.setup()
125 arcade.run()
126
127
128if __name__ == "__main__":
129 main()