1"""
2Sprite Move With Walls
3
4Simple program to show basic sprite usage.
5
6Artwork from http://kenney.nl
7
8If Python and Arcade are installed, this example can be run from the command line with:
9python -m arcade.examples.sprite_move_walls
10"""
11
12import arcade
13import os
14
15SPRITE_SCALING = 0.5
16
17SCREEN_WIDTH = 800
18SCREEN_HEIGHT = 600
19SCREEN_TITLE = "Sprite Move with Walls Example"
20
21MOVEMENT_SPEED = 5
22
23
24class MyGame(arcade.Window):
25 """ Main application class. """
26
27 def __init__(self, width, height, title):
28 """
29 Initializer
30 """
31 super().__init__(width, height, title)
32
33 # Set the working directory (where we expect to find files) to the same
34 # directory this .py file is in. You can leave this out of your own
35 # code, but it is needed to easily run the examples using "python -m"
36 # as mentioned at the top of this program.
37 file_path = os.path.dirname(os.path.abspath(__file__))
38 os.chdir(file_path)
39
40 # Sprite lists
41 self.coin_list = None
42 self.wall_list = None
43 self.player_list = None
44
45 # Set up the player
46 self.player_sprite = None
47 self.physics_engine = None
48
49 def setup(self):
50 """ Set up the game and initialize the variables. """
51
52 # Sprite lists
53 self.player_list = arcade.SpriteList()
54 self.wall_list = arcade.SpriteList()
55
56 # Set up the player
57 self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png",
58 SPRITE_SCALING)
59 self.player_sprite.center_x = 50
60 self.player_sprite.center_y = 64
61 self.player_list.append(self.player_sprite)
62
63 # -- Set up the walls
64 # Create a row of boxes
65 for x in range(173, 650, 64):
66 wall = arcade.Sprite(":resources:images/tiles/boxCrate_double.png", SPRITE_SCALING)
67 wall.center_x = x
68 wall.center_y = 200
69 self.wall_list.append(wall)
70
71 # Create a column of boxes
72 for y in range(273, 500, 64):
73 wall = arcade.Sprite(":resources:images/tiles/boxCrate_double.png", SPRITE_SCALING)
74 wall.center_x = 465
75 wall.center_y = y
76 self.wall_list.append(wall)
77
78 self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite,
79 self.wall_list)
80
81 # Set the background color
82 arcade.set_background_color(arcade.color.AMAZON)
83
84 def on_draw(self):
85 """
86 Render the screen.
87 """
88
89 # This command has to happen before we start drawing
90 arcade.start_render()
91
92 # Draw all the sprites.
93 self.wall_list.draw()
94 self.player_list.draw()
95
96 def on_key_press(self, key, modifiers):
97 """Called whenever a key is pressed. """
98
99 if key == arcade.key.UP:
100 self.player_sprite.change_y = MOVEMENT_SPEED
101 elif key == arcade.key.DOWN:
102 self.player_sprite.change_y = -MOVEMENT_SPEED
103 elif key == arcade.key.LEFT:
104 self.player_sprite.change_x = -MOVEMENT_SPEED
105 elif key == arcade.key.RIGHT:
106 self.player_sprite.change_x = MOVEMENT_SPEED
107
108 def on_key_release(self, key, modifiers):
109 """Called when the user releases a key. """
110
111 if key == arcade.key.UP or key == arcade.key.DOWN:
112 self.player_sprite.change_y = 0
113 elif key == arcade.key.LEFT or key == arcade.key.RIGHT:
114 self.player_sprite.change_x = 0
115
116 def on_update(self, delta_time):
117 """ Movement and game logic """
118
119 # Call update on all sprites (The sprites don't do much in this
120 # example though.)
121 self.physics_engine.update()
122
123
124def main():
125 """ Main method """
126 window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
127 window.setup()
128 arcade.run()
129
130
131if __name__ == "__main__":
132 main()