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 http://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
13import arcade
14
15SPRITE_SCALING = 0.5
16
17SCREEN_WIDTH = 800
18SCREEN_HEIGHT = 600
19SCREEN_TITLE = "Sprite Face Left or Right Example"
20
21MOVEMENT_SPEED = 5
22
23# Index of textures, first element faces left, second faces right
24TEXTURE_LEFT = 0
25TEXTURE_RIGHT = 1
26
27
28class Player(arcade.Sprite):
29
30 def __init__(self):
31 super().__init__()
32
33 self.scale = SPRITE_SCALING
34 self.textures = []
35
36 # Load a left facing texture and a right facing texture.
37 # flipped_horizontally=True will mirror the image we load.
38 texture = arcade.load_texture(":resources:images/enemies/bee.png")
39 self.textures.append(texture)
40 texture = arcade.load_texture(":resources:images/enemies/bee.png",
41 flipped_horizontally=True)
42 self.textures.append(texture)
43
44 # By default, face right.
45 self.texture = texture
46
47 def update(self):
48 self.center_x += self.change_x
49 self.center_y += self.change_y
50
51 # Figure out if we should face left or right
52 if self.change_x < 0:
53 self.texture = self.textures[TEXTURE_LEFT]
54 elif self.change_x > 0:
55 self.texture = self.textures[TEXTURE_RIGHT]
56
57
58class MyGame(arcade.Window):
59 """
60 Main application class.
61 """
62
63 def __init__(self, width, height, title):
64 """
65 Initializer
66 """
67
68 # Call the parent class initializer
69 super().__init__(width, height, title)
70
71 # Variables that will hold sprite lists
72 self.player_sprite_list = None
73
74 # Set up the player info
75 self.player_sprite = None
76
77 # Set the background color
78 arcade.set_background_color(arcade.color.AMAZON)
79
80 def setup(self):
81 """ Set up the game and initialize the variables. """
82
83 # Sprite lists
84 self.player_sprite_list = arcade.SpriteList()
85
86 # Set up the player
87 self.player_sprite = Player()
88 self.player_sprite.center_x = SCREEN_WIDTH / 2
89 self.player_sprite.center_y = SCREEN_HEIGHT / 2
90 self.player_sprite_list.append(self.player_sprite)
91
92 def on_draw(self):
93 """
94 Render the screen.
95 """
96
97 # This command has to happen before we start drawing
98 arcade.start_render()
99
100 # Draw all the sprites.
101 self.player_sprite_list.draw()
102
103 def on_update(self, delta_time):
104 """ Movement and game logic """
105
106 # Call update on all sprites (The sprites don't do much in this
107 # example though.)
108 self.player_sprite_list.update()
109
110 def on_key_press(self, key, modifiers):
111 """Called whenever a key is pressed. """
112
113 if key == arcade.key.UP:
114 self.player_sprite.change_y = MOVEMENT_SPEED
115 elif key == arcade.key.DOWN:
116 self.player_sprite.change_y = -MOVEMENT_SPEED
117 elif key == arcade.key.LEFT:
118 self.player_sprite.change_x = -MOVEMENT_SPEED
119 elif key == arcade.key.RIGHT:
120 self.player_sprite.change_x = MOVEMENT_SPEED
121
122 def on_key_release(self, key, modifiers):
123 """Called when the user releases a key. """
124
125 if key == arcade.key.UP or key == arcade.key.DOWN:
126 self.player_sprite.change_y = 0
127 elif key == arcade.key.LEFT or key == arcade.key.RIGHT:
128 self.player_sprite.change_x = 0
129
130
131def main():
132 """ Main method """
133 window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
134 window.setup()
135 arcade.run()
136
137
138if __name__ == "__main__":
139 main()