SHARE
TWEET

Untitled

a guest Jun 25th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from random import randint
  2.  
  3. from map_objects.tile import Tile
  4. from map_objects.rectangle import Rect
  5.  
  6. spawn_design = [
  7.             [0, 0, 0, 0, 1, 0, 0, 0, 0],
  8.             [0, 0, 1, 1, 1, 1, 1, 0, 0],
  9.             [0, 1, 1, 1, 1, 1, 1, 1, 0],
  10.             [0, 1, 1, 1, 1, 1, 1, 1, 0],
  11.             [1, 1, 1, 1, 1, 1, 1, 1, 1],
  12.             [0, 1, 1, 1, 1, 1, 1, 1, 0],
  13.             [0, 1, 1, 1, 1, 1, 1, 1, 0],
  14.             [0, 0, 1, 1, 1, 1, 1, 0, 0],
  15.             [0, 0, 0, 0, 1, 0, 0, 0, 0],
  16.         ]
  17.  
  18. class GameMap:
  19.     def __init__(self, width, height):
  20.         self.width = width
  21.         self.height = height
  22.         self.tiles = self.initialize_tiles()
  23.  
  24.     def initialize_tiles(self):
  25.         tiles = [[Tile(True) for y in range(self.height)] for x in range(self.width)]
  26.        
  27.         return tiles
  28.  
  29.     def create_room(self, room):
  30.         #go through the tiles in the rectangle and make them passable
  31.         for x in range(room.x1 + 1, room.x2):
  32.             for y in range(room.y1 + 1, room.y2):
  33.                 self.tiles[x][y].blocked = False
  34.                 self.tiles[x][y].block_sight = False
  35.  
  36.     def create_spawn_room(self, room):
  37.         assert len(spawn_design) == room.y2 - room.y1
  38.         assert len(spawn_design[0]) == room.x2 - room.x1
  39.         for y, row in enumerate(spawn_design):
  40.             for x, row in enumerate(row):
  41.                 tile = self.tiles[x + room.x1][y + room.y1]
  42.                 self.tiles[x][y].blocked = False
  43.                 self.tiles[x][y].block_sight = False
  44.  
  45.     def create_x_tunnel(self, x1, x2, y):
  46.         for x in range(min(x1, x2), max(x1, x2) + 1):
  47.             self.tiles[x][y].blocked = False
  48.             self.tiles[x][y].blocked_sight = False
  49.  
  50.     def create_y_tunnel(self, y1, y2, x):
  51.         for y in range(min(y1, y2), max(y1, y2) + 1):
  52.             self.tiles[x][y].blocked = False
  53.             self.tiles[x][y].blocked_sight = False
  54.  
  55.     def make_map(self, max_rooms, room_min_size, room_max_size, map_width, map_height, player):
  56.         rooms = []
  57.         num_rooms = 0
  58.  
  59.         for r in range(max_rooms):
  60.             if num_rooms == 0:
  61.                 w = 9
  62.                 h = 9
  63.             else:
  64.                 w = randint(room_min_size, room_max_size)
  65.                 h = randint(room_min_size, room_max_size)
  66.                
  67.             x = randint(0, map_width - w - 1)
  68.             y = randint(0, map_height - h - 1)
  69.  
  70.             # "Rect" class makes rectangles easier to work with
  71.             new_room = Rect(x, y, w, h)
  72.  
  73.             #run through other rooms to see if they touch
  74.             for other_rooms in rooms:
  75.                 if new_room.intersect(other_rooms):
  76.                     break
  77.             else:
  78.                 #No touching, room is valid
  79.  
  80.                 #Paint it to the maps tiles
  81.                 self.create_room(new_room)
  82.  
  83.                 #Center Coords of Room
  84.                 (new_x, new_y) = new_room.center()
  85.  
  86.                 if num_rooms == 0:
  87.                     #Starter room
  88.                     player.x = new_x
  89.                     player.y = new_y
  90.                 else:
  91.                     #all rooms after the first:
  92.                     #connect it to the previous room with a tunnel
  93.  
  94.                     #center coords of previous room
  95.                     (prev_x, prev_y) = rooms[num_rooms - 1].center()
  96.  
  97.                     #flip a coin
  98.                     if randint(0, 1) == 1:
  99.                         #first move x then y
  100.                         self.create_x_tunnel(prev_x, new_x, prev_y)
  101.                         self.create_y_tunnel(prev_y, new_y, new_x)
  102.                     else:
  103.                         #first move y then x
  104.                         self.create_y_tunnel(prev_y, new_y, prev_x)
  105.                         self.create_x_tunnel(prev_x, new_x, new_y)
  106.  
  107.                 #append the new room to the list
  108.                 rooms.append(new_room)
  109.                 num_rooms += 1
  110.    
  111.     def is_blocked(self, x, y):
  112.         if self.tiles[x][y].blocked:
  113.             return True
  114.  
  115.         return False
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top