Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randint
- from map_objects.tile import Tile
- from map_objects.rectangle import Rect
- spawn_design = [
- [0, 0, 0, 0, 1, 0, 0, 0, 0],
- [0, 0, 1, 1, 1, 1, 1, 0, 0],
- [0, 1, 1, 1, 1, 1, 1, 1, 0],
- [0, 1, 1, 1, 1, 1, 1, 1, 0],
- [1, 1, 1, 1, 1, 1, 1, 1, 1],
- [0, 1, 1, 1, 1, 1, 1, 1, 0],
- [0, 1, 1, 1, 1, 1, 1, 1, 0],
- [0, 0, 1, 1, 1, 1, 1, 0, 0],
- [0, 0, 0, 0, 1, 0, 0, 0, 0],
- ]
- class GameMap:
- def __init__(self, width, height):
- self.width = width
- self.height = height
- self.tiles = self.initialize_tiles()
- def initialize_tiles(self):
- tiles = [[Tile(True) for y in range(self.height)] for x in range(self.width)]
- return tiles
- def create_room(self, room):
- #go through the tiles in the rectangle and make them passable
- for x in range(room.x1 + 1, room.x2):
- for y in range(room.y1 + 1, room.y2):
- self.tiles[x][y].blocked = False
- self.tiles[x][y].block_sight = False
- def create_spawn_room(self, room):
- assert len(spawn_design) == room.y2 - room.y1
- assert len(spawn_design[0]) == room.x2 - room.x1
- for y, row in enumerate(spawn_design):
- for x, row in enumerate(row):
- tile = self.tiles[x + room.x1][y + room.y1]
- self.tiles[x][y].blocked = False
- self.tiles[x][y].block_sight = False
- def create_x_tunnel(self, x1, x2, y):
- for x in range(min(x1, x2), max(x1, x2) + 1):
- self.tiles[x][y].blocked = False
- self.tiles[x][y].blocked_sight = False
- def create_y_tunnel(self, y1, y2, x):
- for y in range(min(y1, y2), max(y1, y2) + 1):
- self.tiles[x][y].blocked = False
- self.tiles[x][y].blocked_sight = False
- def make_map(self, max_rooms, room_min_size, room_max_size, map_width, map_height, player):
- rooms = []
- num_rooms = 0
- for r in range(max_rooms):
- if num_rooms == 0:
- w = 9
- h = 9
- else:
- w = randint(room_min_size, room_max_size)
- h = randint(room_min_size, room_max_size)
- x = randint(0, map_width - w - 1)
- y = randint(0, map_height - h - 1)
- # "Rect" class makes rectangles easier to work with
- new_room = Rect(x, y, w, h)
- #run through other rooms to see if they touch
- for other_rooms in rooms:
- if new_room.intersect(other_rooms):
- break
- else:
- #No touching, room is valid
- #Paint it to the maps tiles
- self.create_room(new_room)
- #Center Coords of Room
- (new_x, new_y) = new_room.center()
- if num_rooms == 0:
- #Starter room
- player.x = new_x
- player.y = new_y
- else:
- #all rooms after the first:
- #connect it to the previous room with a tunnel
- #center coords of previous room
- (prev_x, prev_y) = rooms[num_rooms - 1].center()
- #flip a coin
- if randint(0, 1) == 1:
- #first move x then y
- self.create_x_tunnel(prev_x, new_x, prev_y)
- self.create_y_tunnel(prev_y, new_y, new_x)
- else:
- #first move y then x
- self.create_y_tunnel(prev_y, new_y, prev_x)
- self.create_x_tunnel(prev_x, new_x, new_y)
- #append the new room to the list
- rooms.append(new_room)
- num_rooms += 1
- def is_blocked(self, x, y):
- if self.tiles[x][y].blocked:
- return True
- return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement