Move with Walls

sprite_move_walls.py
1"""
2Sprite Move With Walls
3
4Simple program to show basic sprite usage.
5
6Artwork from https://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
13
14SPRITE_SCALING = 0.5
15
16WINDOW_WIDTH = 1280
17WINDOW_HEIGHT = 720
18WINDOW_TITLE = "Sprite Move with Walls Example"
19
20MOVEMENT_SPEED = 5
21
22
23class GameView(arcade.View):
24 """ Main application class. """
25
26 def __init__(self):
27 """
28 Initializer
29 """
30 super().__init__()
31
32 # Sprite lists
33 self.coin_list = None
34 self.wall_list = None
35 self.player_list = None
36
37 # Set up the player
38 self.player_sprite = None
39 self.physics_engine = None
40
41 def setup(self):
42 """ Set up the game and initialize the variables. """
43
44 # Sprite lists
45 self.player_list = arcade.SpriteList()
46 self.wall_list = arcade.SpriteList()
47
48 # Set up the player
49 self.player_sprite = arcade.Sprite(
50 ":resources:images/animated_characters/female_person/femalePerson_idle.png",
51 scale=SPRITE_SCALING,
52 )
53 self.player_sprite.center_x = 50
54 self.player_sprite.center_y = 64
55 self.player_list.append(self.player_sprite)
56
57 # -- Set up the walls
58 # Create a row of boxes
59 for x in range(173, 650, 64):
60 wall = arcade.Sprite(":resources:images/tiles/boxCrate_double.png",
61 scale=SPRITE_SCALING)
62 wall.center_x = x
63 wall.center_y = 200
64 self.wall_list.append(wall)
65
66 # Create a column of boxes
67 for y in range(273, 500, 64):
68 wall = arcade.Sprite(":resources:images/tiles/boxCrate_double.png",
69 scale=SPRITE_SCALING)
70 wall.center_x = 465
71 wall.center_y = y
72 self.wall_list.append(wall)
73
74 self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite,
75 self.wall_list)
76
77 # Set the background color
78 self.background_color = arcade.color.AMAZON
79
80 def on_draw(self):
81 """
82 Render the screen.
83 """
84
85 # This command has to happen before we start drawing
86 self.clear()
87
88 # Draw all the sprites.
89 self.wall_list.draw()
90 self.player_list.draw()
91
92 def on_key_press(self, key, modifiers):
93 """Called whenever a key is pressed. """
94
95 if key == arcade.key.UP:
96 self.player_sprite.change_y = MOVEMENT_SPEED
97 elif key == arcade.key.DOWN:
98 self.player_sprite.change_y = -MOVEMENT_SPEED
99 elif key == arcade.key.LEFT:
100 self.player_sprite.change_x = -MOVEMENT_SPEED
101 elif key == arcade.key.RIGHT:
102 self.player_sprite.change_x = MOVEMENT_SPEED
103
104 def on_key_release(self, key, modifiers):
105 """Called when the user releases a key. """
106
107 if key == arcade.key.UP or key == arcade.key.DOWN:
108 self.player_sprite.change_y = 0
109 elif key == arcade.key.LEFT or key == arcade.key.RIGHT:
110 self.player_sprite.change_x = 0
111
112 def on_update(self, delta_time):
113 """ Movement and game logic """
114
115 # Call update on all sprites (The sprites don't do much in this
116 # example though.)
117 self.physics_engine.update()
118
119
120def main():
121 """ Main function """
122 # Create a window class. This is what actually shows up on screen
123 window = arcade.Window(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE)
124
125 # Create and setup the GameView
126 game = GameView()
127 game.setup()
128
129 # Show GameView on screen
130 window.show_view(game)
131
132 # Start the arcade game loop
133 arcade.run()
134
135
136if __name__ == "__main__":
137 main()