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
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
else:
return player_y, player_x

@ -67,7 +67,7 @@ class UI:
return y, 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
else:
return player_y, player_x

@ -4,7 +4,7 @@ import numpy as np
class Map:
def __init__(self):
self.map = [
self.map = np.array([
list("####################"),
list("###....#############"),
list("###....#############"),
@ -12,10 +12,10 @@ class Map:
list("#############.######"),
list("#############....###"),
list("#############....###"),
list("####################")]
list("####################")], dtype=str)
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
else:
return player_y, player_x

@ -4,7 +4,7 @@ import numpy as np
class Map:
def __init__(self):
self.map = [
self.map = np.array([
list("####################"),
list("###....#############"),
list("###....#############"),
@ -12,10 +12,10 @@ class Map:
list("#############.######"),
list("#############....###"),
list("#############....###"),
list("####################")]
list("####################")], dtype=str)
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.x = target_x

@ -4,7 +4,7 @@ import numpy as np
class Map:
def __init__(self):
self.map = [
self.map = np.array([
list("####################"),
list("###....#############"),
list("###....#############"),
@ -12,10 +12,10 @@ class Map:
list("#############.######"),
list("#############....###"),
list("#############....###"),
list("####################")]
list("####################")], dtype=str)
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.x = target_x

@ -3,8 +3,14 @@ import sys
import random
import numpy as np
WALL = 1
SPACE = 0
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):
@ -24,25 +30,21 @@ class Map:
return grid
def make_grid(self):
grid = []
for y in range(0, self.height):
grid.append([WALL] * self.width)
return grid
return np.full((self.height, self.width), WALL, dtype=str)
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.neighborsAB(grid, x, y)
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
@ -50,11 +52,8 @@ class Map:
def inbounds(self, x, y):
return x >= 0 and x < self.width and y >= 0 and y < self.height
def neighborsAB(self, grid, x, y):
points = [[x, y - 2],
[x, y + 2],
[x - 2, y],
[x + 2, y]]
def neighbors(self, grid, x, y):
points = compass(x, y, 2)
result = []
for x,y in points:
@ -62,15 +61,11 @@ class Map:
result.append([x,y])
return result
def neighbors(self, grid, x, y):
points = [[x, y - 2],
[x, y + 2],
[x - 2, y],
[x + 2, y]]
def neighbor_walls(self, grid, x, y):
neighbors = self.neighbors(grid, x, y)
result = []
for x,y in points:
if self.inbounds(x, y) and grid[y][x] == WALL:
for x,y in neighbors:
if grid[y, x] == WALL:
result.append([x,y])
return result
@ -80,43 +75,36 @@ class Map:
dead_ends = []
while True:
n = self.neighbors(grid, on_x, on_y)
n = self.neighbor_walls(grid, on_x, on_y)
if len(n) == 0:
dead_ends.append([on_x, on_y])
t = self.find_coord(grid)
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):
if char == 0:
cur_row += '.'
else:
cur_row += '#'
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

@ -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)

@ -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
@ -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,30 @@ 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 collision(self, target_x, target_y):
# 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):
for y, row in enumerate(self.map):

@ -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 Player:
def __init__(self, x, y):
self.x = x
@ -54,12 +60,12 @@ 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:
@ -72,10 +78,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:
@ -87,7 +90,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
@ -104,33 +107,30 @@ 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 collision(self, target_x, target_y):
# 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):
for y, row in enumerate(self.map):

@ -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 Player:
def __init__(self, x, y):
self.x = x
@ -54,16 +60,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
@ -72,10 +78,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:
@ -87,7 +90,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
@ -104,33 +107,30 @@ 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 collision(self, target_x, target_y):
# 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):
for y, row in enumerate(self.map):

@ -61,16 +61,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
@ -91,7 +91,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
@ -108,33 +108,30 @@ 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 collision(self, target_x, target_y):
# 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):
for y, row in enumerate(self.map):

@ -62,16 +62,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
@ -92,7 +92,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
@ -109,29 +109,25 @@ 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 collision(self, target_x, target_y):
# remember this is True==COLLIDE WITH WALL, False=CAN MOVE THERE

Loading…
Cancel
Save