Finished Part 3. Turned off tunnels for now. Will probably rework it a little before continuing to Part 4. The way they're handled now is messy.
This commit is contained in:
@@ -11,6 +11,10 @@ def main():
|
||||
map_width = 80
|
||||
map_height = 50
|
||||
|
||||
room_max_size = 10
|
||||
room_min_size = 6
|
||||
max_rooms = 30
|
||||
|
||||
colors = {
|
||||
'dark_wall': tc.Color(0, 0, 100),
|
||||
'dark_ground': tc.Color(50, 50, 150)
|
||||
@@ -26,7 +30,8 @@ def main():
|
||||
con = tc.console_new(screen_width, screen_height)
|
||||
|
||||
game_map = GameMap(map_width, map_height)
|
||||
|
||||
#game_map.make_map()
|
||||
game_map.make_map(max_rooms, room_min_size, room_max_size, map_width, map_height, player)
|
||||
key = tc.Key()
|
||||
mouse = tc.Mouse()
|
||||
|
||||
|
||||
@@ -11,3 +11,21 @@ class Tile:
|
||||
block_sight = blocked
|
||||
|
||||
self.block_sight = block_sight
|
||||
|
||||
|
||||
class Rect:
|
||||
def __init__(self, x, y, w, h):
|
||||
self.x1 = x
|
||||
self.y1 = y
|
||||
self.x2 = x + w
|
||||
self.y2 = y + h
|
||||
|
||||
def center(self):
|
||||
center_x = int((self.x1 + self.x2) /2 )
|
||||
center_y = int((self.y1 + self.y2) /2 )
|
||||
return (center_x, center_y)
|
||||
|
||||
def intersect(self, other):
|
||||
# returns true if this room overlaps another
|
||||
return (self.x1 <= other.x2 and self.x2 >= other.x1 and
|
||||
self.y1 <= other.y2 and self.y2 >= other.y1)
|
||||
117
venv/game_map.py
117
venv/game_map.py
@@ -1,4 +1,5 @@
|
||||
from map_objects import *
|
||||
from random import randint
|
||||
|
||||
class GameMap:
|
||||
def __init__(self, width, height):
|
||||
@@ -7,14 +8,14 @@ class GameMap:
|
||||
self.tiles = self.initialize_tiles()
|
||||
|
||||
def initialize_tiles(self):
|
||||
tiles = [[Tile(False) for y in range(self.height)] for x in range(self.width)]
|
||||
tiles = [[Tile(True) for y in range(self.height)] for x in range(self.width)]
|
||||
|
||||
tiles[30][22].blocked = True
|
||||
tiles[30][22].block_sight = True
|
||||
tiles[31][22].blocked = True
|
||||
tiles[31][22].block_sight = True
|
||||
tiles[32][22].blocked = True
|
||||
tiles[32][22].block_sight = True
|
||||
# tiles[30][22].blocked = True
|
||||
# tiles[30][22].block_sight = True
|
||||
# tiles[31][22].blocked = True
|
||||
# tiles[31][22].block_sight = True
|
||||
# tiles[32][22].blocked = True
|
||||
# tiles[32][22].block_sight = True
|
||||
|
||||
return tiles
|
||||
|
||||
@@ -22,3 +23,105 @@ class GameMap:
|
||||
if self.tiles[x][y].blocked:
|
||||
return True
|
||||
return False
|
||||
|
||||
def create_room(self, room):
|
||||
# go through the tiles in the rectabgle 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_h_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].block_sight = False
|
||||
|
||||
def create_v_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].block_sight = False
|
||||
|
||||
# def make_map(self):
|
||||
# # create two rooms for demo
|
||||
# room1 = Rect(20, 15, 10, 15)
|
||||
# room2 = Rect(35,15, 10, 15)
|
||||
#
|
||||
# self.create_room(room1)
|
||||
# self.create_room(room2)
|
||||
#
|
||||
# self.create_h_tunnel(25, 40, 23)
|
||||
|
||||
def make_map(self, max_rooms, room_min_size, room_max_size, map_width, map_height, player):
|
||||
rooms = []
|
||||
num_rooms = 0
|
||||
|
||||
# # print(rooms)
|
||||
# # print(num_rooms)
|
||||
# # random width and height
|
||||
# w = randint(room_min_size, room_max_size)
|
||||
# h = randint(room_min_size, room_max_size)
|
||||
# # random position without going out of bounds
|
||||
# x = randint(0, map_width - w - 1)
|
||||
# y = randint(0, map_height - h - 1)
|
||||
#
|
||||
# new_room = Rect(x, y, w, h)
|
||||
# rooms.append(new_room)
|
||||
# (new_x, new_y) = new_room.center()
|
||||
# player_x = new_x
|
||||
# player_y = new_y
|
||||
# num_rooms += 1
|
||||
|
||||
|
||||
for r in range(max_rooms):
|
||||
# print(rooms)
|
||||
# print(num_rooms)
|
||||
# random width and height
|
||||
w = randint(room_min_size, room_max_size)
|
||||
h = randint(room_min_size, room_max_size)
|
||||
# random position without going out of bounds
|
||||
x = randint(0, map_width - w - 1)
|
||||
y = randint(0, map_height - h - 1)
|
||||
|
||||
#call Rect Class
|
||||
new_room = Rect(x, y, w, h)
|
||||
# rooms.append(new_room)
|
||||
# (new_x, new_y) = new_room.center()
|
||||
# player_x = new_x
|
||||
# player_y = new_y
|
||||
# num_rooms = 1
|
||||
|
||||
#run through other rooms to check for overlap
|
||||
for other_room in rooms:
|
||||
if new_room.intersect(other_room):
|
||||
break
|
||||
else:
|
||||
# no overlap, room is valid
|
||||
# paint to map's tiles
|
||||
self.create_room(new_room)
|
||||
|
||||
#center coords of new room
|
||||
(new_x, new_y) = new_room.center()
|
||||
|
||||
if num_rooms == 1:
|
||||
print("first Room")
|
||||
#first room where player starts
|
||||
player.x = new_x
|
||||
player.y = new_y
|
||||
else:
|
||||
# all rooms after the first are connected
|
||||
# center coords of previous room
|
||||
(prev_x, prev_y) = rooms[num_rooms - 1].center()
|
||||
|
||||
if randint(0, 1) == 1:
|
||||
print("1")
|
||||
#first 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 vertically then horizontally
|
||||
print("2")
|
||||
# self.create_v_tunnel(prev_y, new_y, prev_x)
|
||||
# self.create_h_tunnel(prev_x, new_x, new_y)
|
||||
rooms.append(new_room)
|
||||
num_rooms += 1
|
||||
print(num_rooms)
|
||||
Reference in New Issue
Block a user