Moving Between Different Rooms

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