1"""
2Sprite move between different rooms.
3
4Artwork from https://kenney.nl
5
6If Python and Arcade are installed, this example can be run from the command line with:
7python -m arcade.examples.sprite_rooms
8"""
9from __future__ import annotations
10
11import arcade
12
13SPRITE_SCALING = 0.5
14SPRITE_NATIVE_SIZE = 128
15SPRITE_SIZE = int(SPRITE_NATIVE_SIZE * SPRITE_SCALING)
16
17SCREEN_WIDTH = SPRITE_SIZE * 14
18SCREEN_HEIGHT = SPRITE_SIZE * 10
19SCREEN_TITLE = "Sprite Rooms Example"
20
21MOVEMENT_SPEED = 5
22
23
24class Room:
25 """
26 This class holds all the information about the
27 different rooms.
28 """
29 def __init__(self):
30 # You may want many lists. Lists for coins, monsters, etc.
31 self.wall_list = None
32
33 # This holds the background images. If you don't want changing
34 # background images, you can delete this part.
35 self.background = None
36
37
38def setup_room_1():
39 """
40 Create and return room 1.
41 If your program gets large, you may want to separate this into different
42 files.
43 """
44 room = Room()
45
46 """ Set up the game and initialize the variables. """
47 # Sprite lists
48 room.wall_list = arcade.SpriteList()
49
50 # -- Set up the walls
51 # Create bottom and top row of boxes
52 # This y loops a list of two, the coordinate 0, and just under the top of window
53 for y in (0, SCREEN_HEIGHT - SPRITE_SIZE):
54 # Loop for each box going across
55 for x in range(0, SCREEN_WIDTH, SPRITE_SIZE):
56 wall = arcade.Sprite(":resources:images/tiles/boxCrate_double.png",
57 scale=SPRITE_SCALING)
58 wall.left = x
59 wall.bottom = y
60 room.wall_list.append(wall)
61
62 # Create left and right column of boxes
63 for x in (0, SCREEN_WIDTH - SPRITE_SIZE):
64 # Loop for each box going across
65 for y in range(SPRITE_SIZE, SCREEN_HEIGHT - SPRITE_SIZE, SPRITE_SIZE):
66 # Skip making a block 4 and 5 blocks up on the right side
67 if (y != SPRITE_SIZE * 4 and y != SPRITE_SIZE * 5) or x == 0:
68 wall = arcade.Sprite(":resources:images/tiles/boxCrate_double.png",
69 scale=SPRITE_SCALING)
70 wall.left = x
71 wall.bottom = y
72 room.wall_list.append(wall)
73
74 wall = arcade.Sprite(":resources:images/tiles/boxCrate_double.png",
75 scale=SPRITE_SCALING)
76 wall.left = 7 * SPRITE_SIZE
77 wall.bottom = 5 * SPRITE_SIZE
78 room.wall_list.append(wall)
79
80 # If you want coins or monsters in a level, then add that code here.
81
82 # Load the background image for this level.
83 room.background = arcade.load_texture(":resources:images/backgrounds/"
84 "abstract_1.jpg")
85
86 return room
87
88
89def setup_room_2():
90 """
91 Create and return room 2.
92 """
93 room = Room()
94
95 """ Set up the game and initialize the variables. """
96 # Sprite lists
97 room.wall_list = arcade.SpriteList()
98
99 # -- Set up the walls
100 # Create bottom and top row of boxes
101 # This y loops a list of two, the coordinate 0, and just under the top of window
102 for y in (0, SCREEN_HEIGHT - SPRITE_SIZE):
103 # Loop for each box going across
104 for x in range(0, SCREEN_WIDTH, SPRITE_SIZE):
105 wall = arcade.Sprite(":resources:images/tiles/boxCrate_double.png", scale=SPRITE_SCALING)
106 wall.left = x
107 wall.bottom = y
108 room.wall_list.append(wall)
109
110 # Create left and right column of boxes
111 for x in (0, SCREEN_WIDTH - SPRITE_SIZE):
112 # Loop for each box going across
113 for y in range(SPRITE_SIZE, SCREEN_HEIGHT - SPRITE_SIZE, SPRITE_SIZE):
114 # Skip making a block 4 and 5 blocks up
115 if (y != SPRITE_SIZE * 4 and y != SPRITE_SIZE * 5) or x != 0:
116 wall = arcade.Sprite(":resources:images/tiles/boxCrate_double.png", scale=SPRITE_SCALING)
117 wall.left = x
118 wall.bottom = y
119 room.wall_list.append(wall)
120
121 wall = arcade.Sprite(":resources:images/tiles/boxCrate_double.png", scale=SPRITE_SCALING)
122 wall.left = 5 * SPRITE_SIZE
123 wall.bottom = 6 * SPRITE_SIZE
124 room.wall_list.append(wall)
125 room.background = arcade.load_texture(":resources:images/backgrounds/abstract_2.jpg")
126
127 return room
128
129
130class MyGame(arcade.Window):
131 """ Main application class. """
132
133 def __init__(self, width, height, title):
134 """
135 Initializer
136 """
137 super().__init__(width, height, title)
138
139 # Sprite lists
140 self.current_room = 0
141
142 # Set up the player
143 self.rooms = None
144 self.player_sprite = None
145 self.player_list = None
146 self.physics_engine = None
147
148 def setup(self):
149 """ Set up the game and initialize the variables. """
150 # Set up the player
151 self.player_sprite = arcade.Sprite(
152 ":resources:images/animated_characters/female_person/femalePerson_idle.png",
153 scale=SPRITE_SCALING,
154 )
155 self.player_sprite.center_x = 100
156 self.player_sprite.center_y = 100
157 self.player_list = arcade.SpriteList()
158 self.player_list.append(self.player_sprite)
159
160 # Our list of rooms
161 self.rooms = []
162
163 # Create the rooms. Extend the pattern for each room.
164 room = setup_room_1()
165 self.rooms.append(room)
166
167 room = setup_room_2()
168 self.rooms.append(room)
169
170 # Our starting room number
171 self.current_room = 0
172
173 # Create a physics engine for this room
174 self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite,
175 self.rooms[self.current_room].wall_list)
176
177 def on_draw(self):
178 """
179 Render the screen.
180 """
181
182 # This command has to happen before we start drawing
183 self.clear()
184
185 # Draw the background texture
186 arcade.draw_lrwh_rectangle_textured(0, 0,
187 SCREEN_WIDTH, SCREEN_HEIGHT,
188 self.rooms[self.current_room].background)
189
190 # Draw all the walls in this room
191 self.rooms[self.current_room].wall_list.draw()
192
193 # If you have coins or monsters, then copy and modify the line
194 # above for each list.
195
196 self.player_list.draw()
197
198 def on_key_press(self, key, modifiers):
199 """Called whenever a key is pressed. """
200
201 if key == arcade.key.UP:
202 self.player_sprite.change_y = MOVEMENT_SPEED
203 elif key == arcade.key.DOWN:
204 self.player_sprite.change_y = -MOVEMENT_SPEED
205 elif key == arcade.key.LEFT:
206 self.player_sprite.change_x = -MOVEMENT_SPEED
207 elif key == arcade.key.RIGHT:
208 self.player_sprite.change_x = MOVEMENT_SPEED
209
210 def on_key_release(self, key, modifiers):
211 """Called when the user releases a key. """
212
213 if key == arcade.key.UP or key == arcade.key.DOWN:
214 self.player_sprite.change_y = 0
215 elif key == arcade.key.LEFT or key == arcade.key.RIGHT:
216 self.player_sprite.change_x = 0
217
218 def on_update(self, delta_time):
219 """ Movement and game logic """
220
221 # Call update on all sprites (The sprites don't do much in this
222 # example though.)
223 self.physics_engine.update()
224
225 # Do some logic here to figure out what room we are in, and if we need to go
226 # to a different room.
227 if self.player_sprite.center_x > SCREEN_WIDTH and self.current_room == 0:
228 self.current_room = 1
229 self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite,
230 self.rooms[self.current_room].wall_list)
231 self.player_sprite.center_x = 0
232 elif self.player_sprite.center_x < 0 and self.current_room == 1:
233 self.current_room = 0
234 self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite,
235 self.rooms[self.current_room].wall_list)
236 self.player_sprite.center_x = SCREEN_WIDTH
237
238
239def main():
240 """ Main function """
241 window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
242 window.setup()
243 arcade.run()
244
245
246if __name__ == "__main__":
247 main()