コード例 #1
0
ファイル: game_map.py プロジェクト: pythonarcade/roguelike
    def make_map(self, player: Entity, level: int):
        rooms = []
        num_rooms = 0
        center_of_last_room_x = None
        center_of_last_room_y = None

        for r in range(MAX_ROOMS):
            # random width and height
            w = randint(ROOM_MIN_SIZE, ROOM_MIN_SIZE)
            h = randint(ROOM_MIN_SIZE, ROOM_MIN_SIZE)

            # random position without going out of the boundaries of the map
            x = randint(0, self.map_width - w - 1)
            y = randint(0, self.map_height - h - 1)

            # "Rect" class makes rectangles easier to work with
            new_room = Rect(x, y, w, h)

            # run through the other rooms and see if they intersect with this one
            for other_room in rooms:
                if new_room.intersect(other_room):
                    break
            else:
                # this means there are no intersections, so this room is valid

                # "paint" it to the map's tiles
                self.create_room(new_room)

                # center coordinates of new room, will be useful later
                (new_x, new_y) = new_room.center()

                if num_rooms == 0:
                    # this is the first room, where the player starts at
                    player.x = new_x
                    player.y = new_y
                else:
                    # all rooms after the first:
                    # connect it to the previous room with a tunnel

                    # center coordinates of previous room
                    (prev_x, prev_y) = rooms[num_rooms - 1].center()
                    center_of_last_room_x = new_x
                    center_of_last_room_y = new_y

                    # flip a coin (random number that is either 0 or 1)
                    if randint(0, 1) == 1:
                        # first move horizontally, then vertically
                        self.create_h_tunnel(prev_x, new_x, prev_y)
                        self.create_v_tunnel(prev_y, new_y, new_x)
                    else:
                        # first move vertically, then horizontally
                        self.create_v_tunnel(prev_y, new_y, prev_x)
                        self.create_h_tunnel(prev_x, new_x, new_y)

                place_entities(new_room, self.creatures, self.entities,
                               MAX_ITEMS_PER_ROOM, level)

                # finally, append the new room to the list
                rooms.append(new_room)
                num_rooms += 1

        self.tiles[center_of_last_room_x][
            center_of_last_room_y] = TILE.STAIRS_DOWN