Moving Between Different Rooms

Screenshot of moving between rooms with sprites
sprite_rooms.py
  1"""
  2Sprite move between different rooms.
  3
  4Artwork from http://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"""
  9
 10import arcade
 11import os
 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", SPRITE_SCALING)
 57            wall.left = x
 58            wall.bottom = y
 59            room.wall_list.append(wall)
 60
 61    # Create left and right column of boxes
 62    for x in (0, SCREEN_WIDTH - SPRITE_SIZE):
 63        # Loop for each box going across
 64        for y in range(SPRITE_SIZE, SCREEN_HEIGHT - SPRITE_SIZE, SPRITE_SIZE):
 65            # Skip making a block 4 and 5 blocks up on the right side
 66            if (y != SPRITE_SIZE * 4 and y != SPRITE_SIZE * 5) or x == 0:
 67                wall = arcade.Sprite(":resources:images/tiles/boxCrate_double.png", 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", SPRITE_SCALING)
 73    wall.left = 7 * SPRITE_SIZE
 74    wall.bottom = 5 * SPRITE_SIZE
 75    room.wall_list.append(wall)
 76
 77    # If you want coins or monsters in a level, then add that code here.
 78
 79    # Load the background image for this level.
 80    room.background = arcade.load_texture(":resources:images/backgrounds/abstract_1.jpg")
 81
 82    return room
 83
 84
 85def setup_room_2():
 86    """
 87    Create and return room 2.
 88    """
 89    room = Room()
 90
 91    """ Set up the game and initialize the variables. """
 92    # Sprite lists
 93    room.wall_list = arcade.SpriteList()
 94
 95    # -- Set up the walls
 96    # Create bottom and top row of boxes
 97    # This y loops a list of two, the coordinate 0, and just under the top of window
 98    for y in (0, SCREEN_HEIGHT - SPRITE_SIZE):
 99        # Loop for each box going across
100        for x in range(0, SCREEN_WIDTH, SPRITE_SIZE):
101            wall = arcade.Sprite(":resources:images/tiles/boxCrate_double.png", SPRITE_SCALING)
102            wall.left = x
103            wall.bottom = y
104            room.wall_list.append(wall)
105
106    # Create left and right column of boxes
107    for x in (0, SCREEN_WIDTH - SPRITE_SIZE):
108        # Loop for each box going across
109        for y in range(SPRITE_SIZE, SCREEN_HEIGHT - SPRITE_SIZE, SPRITE_SIZE):
110            # Skip making a block 4 and 5 blocks up
111            if (y != SPRITE_SIZE * 4 and y != SPRITE_SIZE * 5) or x != 0:
112                wall = arcade.Sprite(":resources:images/tiles/boxCrate_double.png", SPRITE_SCALING)
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", SPRITE_SCALING)
118    wall.left = 5 * SPRITE_SIZE
119    wall.bottom = 6 * SPRITE_SIZE
120    room.wall_list.append(wall)
121    room.background = arcade.load_texture(":resources:images/backgrounds/abstract_2.jpg")
122
123    return room
124
125
126class MyGame(arcade.Window):
127    """ Main application class. """
128
129    def __init__(self, width, height, title):
130        """
131        Initializer
132        """
133        super().__init__(width, height, title)
134
135        # Set the working directory (where we expect to find files) to the same
136        # directory this .py file is in. You can leave this out of your own
137        # code, but it is needed to easily run the examples using "python -m"
138        # as mentioned at the top of this program.
139        file_path = os.path.dirname(os.path.abspath(__file__))
140        os.chdir(file_path)
141
142        # Sprite lists
143        self.current_room = 0
144
145        # Set up the player
146        self.rooms = None
147        self.player_sprite = None
148        self.player_list = None
149        self.physics_engine = None
150
151    def setup(self):
152        """ Set up the game and initialize the variables. """
153        # Set up the player
154        self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png", SPRITE_SCALING)
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, self.rooms[self.current_room].wall_list)
175
176    def on_draw(self):
177        """
178        Render the screen.
179        """
180
181        # This command has to happen before we start drawing
182        arcade.start_render()
183
184        # Draw the background texture
185        arcade.draw_lrwh_rectangle_textured(0, 0,
186                                            SCREEN_WIDTH, SCREEN_HEIGHT,
187                                            self.rooms[self.current_room].background)
188
189        # Draw all the walls in this room
190        self.rooms[self.current_room].wall_list.draw()
191
192        # If you have coins or monsters, then copy and modify the line
193        # above for each list.
194
195        self.player_list.draw()
196
197    def on_key_press(self, key, modifiers):
198        """Called whenever a key is pressed. """
199
200        if key == arcade.key.UP:
201            self.player_sprite.change_y = MOVEMENT_SPEED
202        elif key == arcade.key.DOWN:
203            self.player_sprite.change_y = -MOVEMENT_SPEED
204        elif key == arcade.key.LEFT:
205            self.player_sprite.change_x = -MOVEMENT_SPEED
206        elif key == arcade.key.RIGHT:
207            self.player_sprite.change_x = MOVEMENT_SPEED
208
209    def on_key_release(self, key, modifiers):
210        """Called when the user releases a key. """
211
212        if key == arcade.key.UP or key == arcade.key.DOWN:
213            self.player_sprite.change_y = 0
214        elif key == arcade.key.LEFT or key == arcade.key.RIGHT:
215            self.player_sprite.change_x = 0
216
217    def on_update(self, delta_time):
218        """ Movement and game logic """
219
220        # Call update on all sprites (The sprites don't do much in this
221        # example though.)
222        self.physics_engine.update()
223
224        # Do some logic here to figure out what room we are in, and if we need to go
225        # to a different room.
226        if self.player_sprite.center_x > SCREEN_WIDTH and self.current_room == 0:
227            self.current_room = 1
228            self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite,
229                                                             self.rooms[self.current_room].wall_list)
230            self.player_sprite.center_x = 0
231        elif self.player_sprite.center_x < 0 and self.current_room == 1:
232            self.current_room = 0
233            self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite,
234                                                             self.rooms[self.current_room].wall_list)
235            self.player_sprite.center_x = SCREEN_WIDTH
236
237
238def main():
239    """ Main method """
240    window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
241    window.setup()
242    arcade.run()
243
244
245if __name__ == "__main__":
246    main()