Compare commits

...

3 Commits

Author SHA1 Message Date
Kyler Olsen 901225ac21 game.py adjustments 2024-11-10 20:52:34 -07:00
Kyler Olsen 4361021611 Fixed missing chunks errors 2024-11-10 20:51:56 -07:00
Kyler Olsen 327f0d8f65 Fixed maze converting 2024-11-10 20:51:19 -07:00
2 changed files with 49 additions and 19 deletions

56
game.py
View File

@ -28,6 +28,11 @@ class Maze(maze.Maze):
def run(self, i=1_000_000) -> bool: return self.__maze.run(i)
@property
def maze(self) -> maze.Maze:
return self.__maze
def draw_map(
screen: pygame.Surface,
player_pos: pygame.Vector2,
@ -150,13 +155,13 @@ def ray(
not world_map[int(map_pos.x),int(map_pos.y)]
): break
if (
map_pos.x < 0 or
map_pos.y < 0 or
map_pos.x > world_map.width or
map_pos.y > world_map.height or
side_dis.length_squared() > max_dis_squared
): return 0, None
# if (
# map_pos.x < 0 or
# map_pos.y < 0 or
# map_pos.x > world_map.width or
# map_pos.y > world_map.height or
# side_dis.length_squared() > max_dis_squared
# ): return 0, None
# if debug: print(map_pos, side_dis)
@ -171,8 +176,14 @@ def game_loop():
pygame.display.set_caption("Maze Raycaster")
screen = pygame.display.set_mode((1280, 720))
top_view = pygame.Surface((screen.get_width() // 2, screen.get_height()))
game_view = pygame.Surface((screen.get_width() // 2, screen.get_height()))
show_top = False
if show_top:
top_view = pygame.Surface(
(screen.get_width() // 2, screen.get_height()))
game_view = pygame.Surface(
(screen.get_width() // 2, screen.get_height()))
else: game_view = pygame.Surface((screen.get_width(), screen.get_height()))
myfont = pygame.font.SysFont("monospace", 15)
@ -181,23 +192,36 @@ def game_loop():
dt = 0
player_pos = pygame.Vector2(6, 6)
player_dir = pygame.Vector2(-4, 0)
player_dir = pygame.Vector2(4, 0)
# world_map = Maze(maze.RecursiveBacktracker(63), 4)
# world_map = Maze(maze.VectorWrapper(maze.BinaryTree(31)), 4)
# world_map = Maze(maze.Sidewinder(63), 4)
# world_map = Maze(maze.Sidewinder(63, run_param=0.2), 4)
# world_map = Maze(maze.RecursiveDivision(63), 4)
# world_map = Maze(maze.RecursiveDivision(63, binary=True), 4)
# world_map = Maze(maze.VectorWrapper(maze.Prim(31)), 4)
world_map = Maze(maze.VectorWrapper(maze.Wilson(31)), 4)
# world_map = Maze(maze.VectorWrapper(maze.Wilson(7)), 4)
world_map.run()
# world_map = Maze(maze.VectorWrapper.convert(world_map.maze), 4)
s = 0
s_param = 0.35
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
draw_map(top_view, player_pos, player_dir, world_map)
if isinstance(world_map.maze, maze.VectorWrapper) and \
isinstance(world_map.maze.maze, maze.OriginShift) and s > s_param:
world_map = Maze(
maze.VectorWrapper.convert(world_map.maze), 4)
world_map.step()
s -= s_param
if show_top: draw_map(top_view, player_pos, player_dir, world_map)
draw_game(game_view, player_pos, player_dir, world_map)
keys = pygame.key.get_pressed()
@ -217,14 +241,19 @@ def game_loop():
player_dir.scale_to_length(player_dir.length() / 1.01)
if keys[pygame.K_m]:
player_dir.scale_to_length(player_dir.length() * 1.01)
# if keys[pygame.K_t]:
# show_top = not show_top
if keys[pygame.K_ESCAPE]:
running = False
if show_top:
screen.blit(top_view, (0,0))
screen.blit(game_view, (screen.get_width() // 2,0))
else: screen.blit(game_view, (0,0))
label = myfont.render(
f"Player: {player_pos} {player_dir} {round(player_dir.length())}",
f"Player: {player_pos} {player_dir} {round(player_dir.length())}"
f" | FPS: {int(clock.get_fps())} | Change: {round(s, 2)}",
True, (192,192,192)
)
screen.blit(label, (10, 10))
@ -232,6 +261,7 @@ def game_loop():
pygame.display.update()
dt = clock.tick(60) / 1000
s += dt
pygame.quit()

View File

@ -199,16 +199,16 @@ class VectorWrapper(Maze):
while stack:
x, y = stack.pop()
if x - 1 >= 0 and cells[y][x - 1] == VectorEnum.Null and maze[cls.__from_vec(x) - 1, cls.__from_vec(y)]:
if x - 1 >= 0 and cells[y][x - 1] == VectorEnum.Null and not maze[cls.__from_vec(x) - 1, cls.__from_vec(y)]:
stack.append((x - 1, y))
cells[y][x - 1] = VectorEnum.Right
if x + 1 < width and cells[y][x + 1] == VectorEnum.Null and maze[cls.__from_vec(x) + 1, cls.__from_vec(y)]:
if x + 1 < width and cells[y][x + 1] == VectorEnum.Null and not maze[cls.__from_vec(x) + 1, cls.__from_vec(y)]:
stack.append((x + 1, y))
cells[y][x + 1] = VectorEnum.Left
if y - 1 >= 0 and cells[y - 1][x] == VectorEnum.Null and maze[cls.__from_vec(x), cls.__from_vec(y) - 1]:
if y - 1 >= 0 and cells[y - 1][x] == VectorEnum.Null and not maze[cls.__from_vec(x), cls.__from_vec(y) - 1]:
stack.append((x, y - 1))
cells[y - 1][x] = VectorEnum.Down
if y + 1 < height and cells[y + 1][x] == VectorEnum.Null and maze[cls.__from_vec(x), cls.__from_vec(y) + 1]:
if y + 1 < height and cells[y + 1][x] == VectorEnum.Null and not maze[cls.__from_vec(x), cls.__from_vec(y) + 1]:
stack.append((x, y + 1))
cells[y + 1][x] = VectorEnum.Up