Fixed up everything to use numpy arrays only. Could still do some more refinement but I'll stop there and make the phase 13 part with ECS.

master
Zed A. Shaw 11 months ago
parent 44aa27f9d5
commit 11df4c1a4f
  1. 2
      phase_01.py
  2. 2
      phase_02.py
  3. 6
      phase_03.py
  4. 6
      phase_04.py
  5. 6
      phase_05.py
  6. 66
      phase_06.py
  7. 41
      phase_07.py
  8. 36
      phase_08.py
  9. 34
      phase_09.py
  10. 36
      phase_10.py
  11. 25
      phase_11.py
  12. 24
      phase_12.py

@ -42,7 +42,7 @@ def create_ui(stdscr, width, height, status_height):
return win, status return win, status
def move_player(player_y, player_x, target_y, target_x): def move_player(player_y, player_x, target_y, target_x):
if MAP[target_y][target_x] != '#': if MAP[target_y, target_x] != '#':
return target_y, target_x return target_y, target_x
else: else:
return player_y, player_x return player_y, player_x

@ -67,7 +67,7 @@ class UI:
return y, x return y, x
def move_player(player_y, player_x, target_y, target_x): def move_player(player_y, player_x, target_y, target_x):
if MAP[target_y][target_x] != '#': if MAP[target_y, target_x] != '#':
return target_y, target_x return target_y, target_x
else: else:
return player_y, player_x return player_y, player_x

@ -4,7 +4,7 @@ import numpy as np
class Map: class Map:
def __init__(self): def __init__(self):
self.map = [ self.map = np.array([
list("####################"), list("####################"),
list("###....#############"), list("###....#############"),
list("###....#############"), list("###....#############"),
@ -12,10 +12,10 @@ class Map:
list("#############.######"), list("#############.######"),
list("#############....###"), list("#############....###"),
list("#############....###"), list("#############....###"),
list("####################")] list("####################")], dtype=str)
def move_player(self, player_y, player_x, target_y, target_x): def move_player(self, player_y, player_x, target_y, target_x):
if self.map[target_y][target_x] != '#': if self.map[target_y, target_x] != '#':
return target_y, target_x return target_y, target_x
else: else:
return player_y, player_x return player_y, player_x

@ -4,7 +4,7 @@ import numpy as np
class Map: class Map:
def __init__(self): def __init__(self):
self.map = [ self.map = np.array([
list("####################"), list("####################"),
list("###....#############"), list("###....#############"),
list("###....#############"), list("###....#############"),
@ -12,10 +12,10 @@ class Map:
list("#############.######"), list("#############.######"),
list("#############....###"), list("#############....###"),
list("#############....###"), list("#############....###"),
list("####################")] list("####################")], dtype=str)
def move_player(self, player, target_y, target_x): def move_player(self, player, target_y, target_x):
if self.map[target_y][target_x] != '#': if self.map[target_y, target_x] != '#':
player.y = target_y player.y = target_y
player.x = target_x player.x = target_x

@ -4,7 +4,7 @@ import numpy as np
class Map: class Map:
def __init__(self): def __init__(self):
self.map = [ self.map = np.array([
list("####################"), list("####################"),
list("###....#############"), list("###....#############"),
list("###....#############"), list("###....#############"),
@ -12,10 +12,10 @@ class Map:
list("#############.######"), list("#############.######"),
list("#############....###"), list("#############....###"),
list("#############....###"), list("#############....###"),
list("####################")] list("####################")], dtype=str)
def move_player(self, player, target_y, target_x): def move_player(self, player, target_y, target_x):
if self.map[target_y][target_x] != '#': if self.map[target_y, target_x] != '#':
player.y = target_y player.y = target_y
player.x = target_x player.x = target_x

@ -3,8 +3,14 @@ import sys
import random import random
import numpy as np import numpy as np
WALL = 1 WALL = '#'
SPACE = 0 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: class Map:
def __init__(self, width, height): def __init__(self, width, height):
@ -24,25 +30,21 @@ class Map:
return grid return grid
def make_grid(self): def make_grid(self):
grid = [] return np.full((self.height, self.width), WALL, dtype=str)
for y in range(0, self.height):
grid.append([WALL] * self.width)
return grid
def make_room(self, grid, x, y, size): def make_room(self, grid, x, y, size):
for row in range(y, y+size): for row in range(y, y+size):
for col in range(x, x+size): for col in range(x, x+size):
grid[row][col] = SPACE grid[row, col] = SPACE
def find_coord(self, grid): def find_coord(self, grid):
for y in range(1, self.height, 2): for y in range(1, self.height, 2):
for x in range(1, self.width, 2): for x in range(1, self.width, 2):
if grid[y][x] != WALL: continue if grid[y, x] != WALL: continue
found = self.neighborsAB(grid, x, y) found = self.neighbors(grid, x, y)
for found_x, found_y in found: 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 [[x,y],[found_x, found_y]]
return None return None
@ -50,11 +52,8 @@ class Map:
def inbounds(self, x, y): def inbounds(self, x, y):
return x >= 0 and x < self.width and y >= 0 and y < self.height return x >= 0 and x < self.width and y >= 0 and y < self.height
def neighborsAB(self, grid, x, y): def neighbors(self, grid, x, y):
points = [[x, y - 2], points = compass(x, y, 2)
[x, y + 2],
[x - 2, y],
[x + 2, y]]
result = [] result = []
for x,y in points: for x,y in points:
@ -62,15 +61,11 @@ class Map:
result.append([x,y]) result.append([x,y])
return result return result
def neighbors(self, grid, x, y): def neighbor_walls(self, grid, x, y):
points = [[x, y - 2], neighbors = self.neighbors(grid, x, y)
[x, y + 2],
[x - 2, y],
[x + 2, y]]
result = [] result = []
for x,y in points: for x,y in neighbors:
if self.inbounds(x, y) and grid[y][x] == WALL: if grid[y, x] == WALL:
result.append([x,y]) result.append([x,y])
return result return result
@ -80,43 +75,36 @@ class Map:
dead_ends = [] dead_ends = []
while True: while True:
n = self.neighbors(grid, on_x, on_y) n = self.neighbor_walls(grid, on_x, on_y)
if len(n) == 0: if len(n) == 0:
dead_ends.append([on_x, on_y]) dead_ends.append([on_x, on_y])
t = self.find_coord(grid) t = self.find_coord(grid)
if t == None: break if t == None: break
on_x, on_y = t[0] on_x, on_y = t[0]
found_x, found_y = t[1] found_x, found_y = t[1]
grid[on_y][on_x] = SPACE grid[on_y, on_x] = SPACE
row = (on_y + found_y) // 2 row = (on_y + found_y) // 2
col = (on_x + found_x) // 2 col = (on_x + found_x) // 2
grid[row][col] = SPACE grid[row, col] = SPACE
else: else:
nb_x, nb_y = random.choice(n) 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 row = (nb_y + on_y) // 2
col = (nb_x + on_x) // 2 col = (nb_x + on_x) // 2
grid[row][col] = SPACE grid[row, col] = SPACE
on_x, on_y = nb_x, nb_y on_x, on_y = nb_x, nb_y
return dead_ends return dead_ends
def render_map(self, grid): def render_map(self, grid):
self.map = [] self.map = np.full((self.height, self.width), '#', dtype=str)
for y, y_line in enumerate(grid): for y, y_line in enumerate(grid):
cur_row = ""
for x, char in enumerate(y_line): for x, char in enumerate(y_line):
if char == 0: self.map[y, x] = char
cur_row += '.'
else:
cur_row += '#'
self.map.append(cur_row)
def move_player(self, player, target_x, target_y): 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.y = target_y
player.x = target_x player.x = target_x

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

@ -6,6 +6,12 @@ import numpy as np
WALL = '#' WALL = '#'
SPACE = '.' 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: class Map:
def __init__(self, width, height): def __init__(self, width, height):
self.width = width self.width = width
@ -36,16 +42,16 @@ class Map:
def make_room(self, grid, x, y, size): def make_room(self, grid, x, y, size):
for row in range(y, y+size): for row in range(y, y+size):
for col in range(x, x+size): for col in range(x, x+size):
grid[row][col] = SPACE grid[row, col] = SPACE
def find_coord(self, grid): def find_coord(self, grid):
for y in range(1, self.height, 2): for y in range(1, self.height, 2):
for x in range(1, self.width, 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) found = self.neighbors(grid, x, y)
for found_x, found_y in found: 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 [[x,y],[found_x, found_y]]
return None return None
@ -54,10 +60,7 @@ class Map:
return x >= 0 and x < self.width and y >= 0 and y < self.height return x >= 0 and x < self.width and y >= 0 and y < self.height
def neighbors(self, grid, x, y): def neighbors(self, grid, x, y):
points = [[x, y - 2], points = compass(x, y, 2)
[x, y + 2],
[x - 2, y],
[x + 2, y]]
result = [] result = []
for x,y in points: for x,y in points:
@ -69,7 +72,7 @@ class Map:
neighbors = self.neighbors(grid, x, y) neighbors = self.neighbors(grid, x, y)
result = [] result = []
for x,y in neighbors: for x,y in neighbors:
if grid[y][x] == WALL: if grid[y, x] == WALL:
result.append([x,y]) result.append([x,y])
return result return result
@ -86,33 +89,30 @@ class Map:
if t == None: break if t == None: break
on_x, on_y = t[0] on_x, on_y = t[0]
found_x, found_y = t[1] found_x, found_y = t[1]
grid[on_y][on_x] = SPACE grid[on_y, on_x] = SPACE
row = (on_y + found_y) // 2 row = (on_y + found_y) // 2
col = (on_x + found_x) // 2 col = (on_x + found_x) // 2
grid[row][col] = SPACE grid[row, col] = SPACE
else: else:
nb_x, nb_y = random.choice(n) 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 row = (nb_y + on_y) // 2
col = (nb_x + on_x) // 2 col = (nb_x + on_x) // 2
grid[row][col] = SPACE grid[row, col] = SPACE
on_x, on_y = nb_x, nb_y on_x, on_y = nb_x, nb_y
return dead_ends return dead_ends
def render_map(self, grid): def render_map(self, grid):
self.map = [] self.map = np.full((self.height, self.width), '#', dtype=str)
for y, y_line in enumerate(grid): for y, y_line in enumerate(grid):
cur_row = ""
for x, char in enumerate(y_line): for x, char in enumerate(y_line):
cur_row += char self.map[y, x] = char
self.map.append(cur_row)
def collision(self, target_x, target_y): def collision(self, target_x, target_y):
# remember this is True==COLLIDE WITH WALL, False=CAN MOVE THERE # remember this is True==COLLIDE WITH WALL, False=CAN MOVE THERE
return self.map[target_y][target_x] == '#' return self.map[target_y, target_x] == '#'
def draw(self, win): def draw(self, win):
for y, row in enumerate(self.map): for y, row in enumerate(self.map):

@ -6,6 +6,12 @@ import numpy as np
WALL = '#' WALL = '#'
SPACE = '.' 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 Player: class Player:
def __init__(self, x, y): def __init__(self, x, y):
self.x = x self.x = x
@ -54,12 +60,12 @@ class Map:
def make_room(self, grid, x, y, size): def make_room(self, grid, x, y, size):
for row in range(y, y+size): for row in range(y, y+size):
for col in range(x, x+size): for col in range(x, x+size):
grid[row][col] = SPACE grid[row, col] = SPACE
def find_coord(self, grid): def find_coord(self, grid):
for y in range(1, self.height, 2): for y in range(1, self.height, 2):
for x in range(1, self.width, 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) found = self.neighbors(grid, x, y)
for found_x, found_y in found: for found_x, found_y in found:
@ -72,10 +78,7 @@ class Map:
return x >= 0 and x < self.width and y >= 0 and y < self.height return x >= 0 and x < self.width and y >= 0 and y < self.height
def neighbors(self, grid, x, y): def neighbors(self, grid, x, y):
points = [[x, y - 2], points = compass(x, y, 2)
[x, y + 2],
[x - 2, y],
[x + 2, y]]
result = [] result = []
for x,y in points: for x,y in points:
@ -87,7 +90,7 @@ class Map:
neighbors = self.neighbors(grid, x, y) neighbors = self.neighbors(grid, x, y)
result = [] result = []
for x,y in neighbors: for x,y in neighbors:
if grid[y][x] == WALL: if grid[y, x] == WALL:
result.append([x,y]) result.append([x,y])
return result return result
@ -104,33 +107,30 @@ class Map:
if t == None: break if t == None: break
on_x, on_y = t[0] on_x, on_y = t[0]
found_x, found_y = t[1] found_x, found_y = t[1]
grid[on_y][on_x] = SPACE grid[on_y, on_x] = SPACE
row = (on_y + found_y) // 2 row = (on_y + found_y) // 2
col = (on_x + found_x) // 2 col = (on_x + found_x) // 2
grid[row][col] = SPACE grid[row, col] = SPACE
else: else:
nb_x, nb_y = random.choice(n) 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 row = (nb_y + on_y) // 2
col = (nb_x + on_x) // 2 col = (nb_x + on_x) // 2
grid[row][col] = SPACE grid[row, col] = SPACE
on_x, on_y = nb_x, nb_y on_x, on_y = nb_x, nb_y
return dead_ends return dead_ends
def render_map(self, grid): def render_map(self, grid):
self.map = [] self.map = np.full((self.height, self.width), '#', dtype=str)
for y, y_line in enumerate(grid): for y, y_line in enumerate(grid):
cur_row = ""
for x, char in enumerate(y_line): for x, char in enumerate(y_line):
cur_row += char self.map[y, x] = char
self.map.append(cur_row)
def collision(self, target_x, target_y): def collision(self, target_x, target_y):
# remember this is True==COLLIDE WITH WALL, False=CAN MOVE THERE # remember this is True==COLLIDE WITH WALL, False=CAN MOVE THERE
return self.map[target_y][target_x] == '#' return self.map[target_y, target_x] == '#'
def draw(self, win): def draw(self, win):
for y, row in enumerate(self.map): for y, row in enumerate(self.map):

@ -6,6 +6,12 @@ import numpy as np
WALL = '#' WALL = '#'
SPACE = '.' 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 Player: class Player:
def __init__(self, x, y): def __init__(self, x, y):
self.x = x self.x = x
@ -54,16 +60,16 @@ class Map:
def make_room(self, grid, x, y, size): def make_room(self, grid, x, y, size):
for row in range(y, y+size): for row in range(y, y+size):
for col in range(x, x+size): for col in range(x, x+size):
grid[row][col] = SPACE grid[row, col] = SPACE
def find_coord(self, grid): def find_coord(self, grid):
for y in range(1, self.height, 2): for y in range(1, self.height, 2):
for x in range(1, self.width, 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) found = self.neighbors(grid, x, y)
for found_x, found_y in found: 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 [[x,y],[found_x, found_y]]
return None return None
@ -72,10 +78,7 @@ class Map:
return x >= 0 and x < self.width and y >= 0 and y < self.height return x >= 0 and x < self.width and y >= 0 and y < self.height
def neighbors(self, grid, x, y): def neighbors(self, grid, x, y):
points = [[x, y - 2], points = compass(x, y, 2)
[x, y + 2],
[x - 2, y],
[x + 2, y]]
result = [] result = []
for x,y in points: for x,y in points:
@ -87,7 +90,7 @@ class Map:
neighbors = self.neighbors(grid, x, y) neighbors = self.neighbors(grid, x, y)
result = [] result = []
for x,y in neighbors: for x,y in neighbors:
if grid[y][x] == WALL: if grid[y, x] == WALL:
result.append([x,y]) result.append([x,y])
return result return result
@ -104,33 +107,30 @@ class Map:
if t == None: break if t == None: break
on_x, on_y = t[0] on_x, on_y = t[0]
found_x, found_y = t[1] found_x, found_y = t[1]
grid[on_y][on_x] = SPACE grid[on_y, on_x] = SPACE
row = (on_y + found_y) // 2 row = (on_y + found_y) // 2
col = (on_x + found_x) // 2 col = (on_x + found_x) // 2
grid[row][col] = SPACE grid[row, col] = SPACE
else: else:
nb_x, nb_y = random.choice(n) 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 row = (nb_y + on_y) // 2
col = (nb_x + on_x) // 2 col = (nb_x + on_x) // 2
grid[row][col] = SPACE grid[row, col] = SPACE
on_x, on_y = nb_x, nb_y on_x, on_y = nb_x, nb_y
return dead_ends return dead_ends
def render_map(self, grid): def render_map(self, grid):
self.map = [] self.map = np.full((self.height, self.width), '#', dtype=str)
for y, y_line in enumerate(grid): for y, y_line in enumerate(grid):
cur_row = ""
for x, char in enumerate(y_line): for x, char in enumerate(y_line):
cur_row += char self.map[y, x] = char
self.map.append(cur_row)
def collision(self, target_x, target_y): def collision(self, target_x, target_y):
# remember this is True==COLLIDE WITH WALL, False=CAN MOVE THERE # remember this is True==COLLIDE WITH WALL, False=CAN MOVE THERE
return self.map[target_y][target_x] == '#' return self.map[target_y, target_x] == '#'
def draw(self, win): def draw(self, win):
for y, row in enumerate(self.map): for y, row in enumerate(self.map):

@ -61,16 +61,16 @@ class Map:
def make_room(self, grid, x, y, size): def make_room(self, grid, x, y, size):
for row in range(y, y+size): for row in range(y, y+size):
for col in range(x, x+size): for col in range(x, x+size):
grid[row][col] = SPACE grid[row, col] = SPACE
def find_coord(self, grid): def find_coord(self, grid):
for y in range(1, self.height, 2): for y in range(1, self.height, 2):
for x in range(1, self.width, 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) found = self.neighbors(grid, x, y)
for found_x, found_y in found: 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 [[x,y],[found_x, found_y]]
return None return None
@ -91,7 +91,7 @@ class Map:
neighbors = self.neighbors(grid, x, y) neighbors = self.neighbors(grid, x, y)
result = [] result = []
for x,y in neighbors: for x,y in neighbors:
if grid[y][x] == WALL: if grid[y, x] == WALL:
result.append([x,y]) result.append([x,y])
return result return result
@ -108,33 +108,30 @@ class Map:
if t == None: break if t == None: break
on_x, on_y = t[0] on_x, on_y = t[0]
found_x, found_y = t[1] found_x, found_y = t[1]
grid[on_y][on_x] = SPACE grid[on_y, on_x] = SPACE
row = (on_y + found_y) // 2 row = (on_y + found_y) // 2
col = (on_x + found_x) // 2 col = (on_x + found_x) // 2
grid[row][col] = SPACE grid[row, col] = SPACE
else: else:
nb_x, nb_y = random.choice(n) 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 row = (nb_y + on_y) // 2
col = (nb_x + on_x) // 2 col = (nb_x + on_x) // 2
grid[row][col] = SPACE grid[row, col] = SPACE
on_x, on_y = nb_x, nb_y on_x, on_y = nb_x, nb_y
return dead_ends return dead_ends
def render_map(self, grid): def render_map(self, grid):
self.map = [] self.map = np.full((self.height, self.width), '#', dtype=str)
for y, y_line in enumerate(grid): for y, y_line in enumerate(grid):
cur_row = ""
for x, char in enumerate(y_line): for x, char in enumerate(y_line):
cur_row += char self.map[y, x] = char
self.map.append(cur_row)
def collision(self, target_x, target_y): def collision(self, target_x, target_y):
# remember this is True==COLLIDE WITH WALL, False=CAN MOVE THERE # remember this is True==COLLIDE WITH WALL, False=CAN MOVE THERE
return self.map[target_y][target_x] == WALL return self.map[target_y, target_x] == WALL
def draw(self, win): def draw(self, win):
for y, row in enumerate(self.map): for y, row in enumerate(self.map):

@ -62,16 +62,16 @@ class Map:
def make_room(self, grid, x, y, size): def make_room(self, grid, x, y, size):
for row in range(y, y+size): for row in range(y, y+size):
for col in range(x, x+size): for col in range(x, x+size):
grid[row][col] = SPACE grid[row, col] = SPACE
def find_coord(self, grid): def find_coord(self, grid):
for y in range(1, self.height, 2): for y in range(1, self.height, 2):
for x in range(1, self.width, 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) found = self.neighbors(grid, x, y)
for found_x, found_y in found: 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 [[x,y],[found_x, found_y]]
return None return None
@ -92,7 +92,7 @@ class Map:
neighbors = self.neighbors(grid, x, y) neighbors = self.neighbors(grid, x, y)
result = [] result = []
for x,y in neighbors: for x,y in neighbors:
if grid[y][x] == WALL: if grid[y, x] == WALL:
result.append([x,y]) result.append([x,y])
return result return result
@ -109,29 +109,25 @@ class Map:
if t == None: break if t == None: break
on_x, on_y = t[0] on_x, on_y = t[0]
found_x, found_y = t[1] found_x, found_y = t[1]
grid[on_y][on_x] = SPACE grid[on_y, on_x] = SPACE
row = (on_y + found_y) // 2 row = (on_y + found_y) // 2
col = (on_x + found_x) // 2 col = (on_x + found_x) // 2
grid[row][col] = SPACE grid[row, col] = SPACE
else: else:
nb_x, nb_y = random.choice(n) 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 row = (nb_y + on_y) // 2
col = (nb_x + on_x) // 2 col = (nb_x + on_x) // 2
grid[row][col] = SPACE grid[row, col] = SPACE
on_x, on_y = nb_x, nb_y on_x, on_y = nb_x, nb_y
return dead_ends return dead_ends
def render_map(self, grid): def render_map(self, grid):
self.map = [] self.map = np.full((self.height, self.width), '#', dtype=str)
for y, y_line in enumerate(grid): for y, y_line in enumerate(grid):
cur_row = ""
for x, char in enumerate(y_line): for x, char in enumerate(y_line):
cur_row += char self.map[y, x] = char
self.map.append(cur_row)
def collision(self, target_x, target_y): def collision(self, target_x, target_y):
# remember this is True==COLLIDE WITH WALL, False=CAN MOVE THERE # remember this is True==COLLIDE WITH WALL, False=CAN MOVE THERE

Loading…
Cancel
Save