|
|
|
@ -6,6 +6,12 @@ import numpy as np |
|
|
|
|
WALL = '#' |
|
|
|
|
SPACE = '.' |
|
|
|
|
|
|
|
|
|
def compass(x, y, offset=1): |
|
|
|
|
return [[x, y - offset], # North |
|
|
|
|
[x, y + offset], # South |
|
|
|
|
[x + offset, y], # East |
|
|
|
|
[x - offset, y]] # West |
|
|
|
|
|
|
|
|
|
class Map: |
|
|
|
|
def __init__(self, width, height): |
|
|
|
|
self.width = width |
|
|
|
@ -20,7 +26,7 @@ class Map: |
|
|
|
|
while True: |
|
|
|
|
y = random.randrange(0, self.height) |
|
|
|
|
x = random.randrange(0, self.width) |
|
|
|
|
if self.map[y][x] == SPACE: |
|
|
|
|
if self.map[y, x] == SPACE: |
|
|
|
|
return x, y |
|
|
|
|
|
|
|
|
|
def sample_rooms(self, grid, dead_ends, size, count): |
|
|
|
@ -36,16 +42,16 @@ class Map: |
|
|
|
|
def make_room(self, grid, x, y, size): |
|
|
|
|
for row in range(y, y+size): |
|
|
|
|
for col in range(x, x+size): |
|
|
|
|
grid[row][col] = SPACE |
|
|
|
|
grid[row, col] = SPACE |
|
|
|
|
|
|
|
|
|
def find_coord(self, grid): |
|
|
|
|
for y in range(1, self.height, 2): |
|
|
|
|
for x in range(1, self.width, 2): |
|
|
|
|
if grid[y][x] != WALL: continue |
|
|
|
|
if grid[y, x] != WALL: continue |
|
|
|
|
|
|
|
|
|
found = self.neighbors(grid, x, y) |
|
|
|
|
for found_x, found_y in found: |
|
|
|
|
if grid[found_y][found_x] == SPACE: |
|
|
|
|
if grid[found_y, found_x] == SPACE: |
|
|
|
|
return [[x,y],[found_x, found_y]] |
|
|
|
|
return None |
|
|
|
|
|
|
|
|
@ -54,10 +60,7 @@ class Map: |
|
|
|
|
return x >= 0 and x < self.width and y >= 0 and y < self.height |
|
|
|
|
|
|
|
|
|
def neighbors(self, grid, x, y): |
|
|
|
|
points = [[x, y - 2], |
|
|
|
|
[x, y + 2], |
|
|
|
|
[x - 2, y], |
|
|
|
|
[x + 2, y]] |
|
|
|
|
points = compass(x, y, 2) |
|
|
|
|
|
|
|
|
|
result = [] |
|
|
|
|
for x,y in points: |
|
|
|
@ -69,7 +72,7 @@ class Map: |
|
|
|
|
neighbors = self.neighbors(grid, x, y) |
|
|
|
|
result = [] |
|
|
|
|
for x,y in neighbors: |
|
|
|
|
if grid[y][x] == WALL: |
|
|
|
|
if grid[y, x] == WALL: |
|
|
|
|
result.append([x,y]) |
|
|
|
|
return result |
|
|
|
|
|
|
|
|
@ -86,33 +89,29 @@ class Map: |
|
|
|
|
if t == None: break |
|
|
|
|
on_x, on_y = t[0] |
|
|
|
|
found_x, found_y = t[1] |
|
|
|
|
grid[on_y][on_x] = SPACE |
|
|
|
|
grid[on_y, on_x] = SPACE |
|
|
|
|
row = (on_y + found_y) // 2 |
|
|
|
|
col = (on_x + found_x) // 2 |
|
|
|
|
grid[row][col] = SPACE |
|
|
|
|
grid[row, col] = SPACE |
|
|
|
|
else: |
|
|
|
|
nb_x, nb_y = random.choice(n) |
|
|
|
|
grid[nb_y][nb_x] = SPACE |
|
|
|
|
grid[nb_y, nb_x] = SPACE |
|
|
|
|
row = (nb_y + on_y) // 2 |
|
|
|
|
col = (nb_x + on_x) // 2 |
|
|
|
|
grid[row][col] = SPACE |
|
|
|
|
grid[row, col] = SPACE |
|
|
|
|
on_x, on_y = nb_x, nb_y |
|
|
|
|
|
|
|
|
|
return dead_ends |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def render_map(self, grid): |
|
|
|
|
self.map = [] |
|
|
|
|
self.map = np.full((self.height, self.width), '#', dtype=str) |
|
|
|
|
for y, y_line in enumerate(grid): |
|
|
|
|
cur_row = "" |
|
|
|
|
|
|
|
|
|
for x, char in enumerate(y_line): |
|
|
|
|
cur_row += char |
|
|
|
|
self.map.append(cur_row) |
|
|
|
|
|
|
|
|
|
self.map[y, x] = char |
|
|
|
|
|
|
|
|
|
def move_player(self, player, target_x, target_y): |
|
|
|
|
if self.map[target_y][target_x] != '#': |
|
|
|
|
if self.map[target_y, target_x] != '#': |
|
|
|
|
player.y = target_y |
|
|
|
|
player.x = target_x |
|
|
|
|
|
|
|
|
@ -160,7 +159,7 @@ class UI: |
|
|
|
|
self.status.addstr(1, 1, f"PLAYER AT {player.x},{player.y}") |
|
|
|
|
|
|
|
|
|
def draw_actor(self, actor): |
|
|
|
|
assert self.map.map[actor.y][actor.x] != '#', f"WHAT? actor at {actor.x},{actor.y} but that's a wall!" |
|
|
|
|
assert self.map.map[actor.y, actor.x] != '#', f"WHAT? actor at {actor.x},{actor.y} but that's a wall!" |
|
|
|
|
|
|
|
|
|
# actor has to be moved in by 1 for the border |
|
|
|
|
self.win.addstr(actor.y, actor.x, actor.symbol, curses.A_BOLD) |
|
|
|
|