updated agent navigation
This commit is contained in:
86
game.py
86
game.py
@@ -3,7 +3,8 @@ from itertools import product
|
||||
|
||||
import pygame
|
||||
from pygame.locals import *
|
||||
from maze import Maze
|
||||
from maze import Maze, Cell
|
||||
from agent import Agent
|
||||
|
||||
pygame.init()
|
||||
vec = pygame.math.Vector2 # 2 for two dimensional
|
||||
@@ -19,24 +20,30 @@ displaysurface = pygame.display.set_mode((WIDTH, HEIGHT))
|
||||
pygame.display.set_caption("Game")
|
||||
|
||||
SCREEN_BUFFER = 10
|
||||
MAZE_SIZE = 15
|
||||
MAZE_SIZE = 10
|
||||
GRID_SIZE = 25
|
||||
|
||||
|
||||
class Player(pygame.sprite.Sprite):
|
||||
def __init__(self):
|
||||
class VisitedSpot(pygame.sprite.Sprite):
|
||||
def __init__(self, cell: Cell):
|
||||
super().__init__()
|
||||
self.surf = pygame.Surface((30, 30))
|
||||
self.surf.fill((0, 255, 0))
|
||||
self.rect = self.surf.get_rect(center=(10, 420))
|
||||
self.surf = pygame.Surface((GRID_SIZE, GRID_SIZE))
|
||||
self.surf.fill((0, 122, 000))
|
||||
self.rect = self.surf.get_rect(topleft=(cell.x * GRID_SIZE + SCREEN_BUFFER, cell.y * GRID_SIZE + SCREEN_BUFFER))
|
||||
|
||||
class DeadEndSpot(pygame.sprite.Sprite):
|
||||
def __init__(self, cell: Cell):
|
||||
super().__init__()
|
||||
self.surf = pygame.Surface((GRID_SIZE, GRID_SIZE))
|
||||
self.surf.fill((122, 0, 000))
|
||||
self.rect = self.surf.get_rect(topleft=(cell.x * GRID_SIZE + SCREEN_BUFFER, cell.y * GRID_SIZE + SCREEN_BUFFER))
|
||||
|
||||
class MazeWall(pygame.sprite.Sprite):
|
||||
def __init__(self, wall):
|
||||
wall_width = 2
|
||||
super().__init__()
|
||||
wall_width = 2
|
||||
start_cell, end_cell = wall
|
||||
x,y = start_cell
|
||||
x, y = start_cell
|
||||
vertical = start_cell[1] == end_cell[1]
|
||||
if vertical:
|
||||
self.surf = pygame.Surface((wall_width, GRID_SIZE))
|
||||
@@ -48,17 +55,23 @@ class MazeWall(pygame.sprite.Sprite):
|
||||
else:
|
||||
self.rect = self.surf.get_rect(midleft=(x * GRID_SIZE + SCREEN_BUFFER, (y + 1) * GRID_SIZE + SCREEN_BUFFER))
|
||||
|
||||
agents = list()
|
||||
agents.append(Agent(SCREEN_BUFFER, GRID_SIZE))
|
||||
# agents.append(Agent(SCREEN_BUFFER, GRID_SIZE))
|
||||
# agents.append(Agent(SCREEN_BUFFER, GRID_SIZE))
|
||||
# agents.append(Agent(SCREEN_BUFFER, GRID_SIZE))
|
||||
|
||||
|
||||
all_sprites = pygame.sprite.Group()
|
||||
|
||||
|
||||
visited_cells_sprites = pygame.sprite.Group()
|
||||
dead_end_sprites = pygame.sprite.Group()
|
||||
maze_sprites = pygame.sprite.Group()
|
||||
|
||||
maze = Maze(MAZE_SIZE, MAZE_SIZE)
|
||||
|
||||
for wall in maze.walls:
|
||||
wall_sprite = MazeWall(wall)
|
||||
maze_sprites.add(wall_sprite)
|
||||
MOVEEVENT = pygame.USEREVENT+1
|
||||
RESETMAZE = pygame.USEREVENT+2
|
||||
reset_maze_event = pygame.event.Event(RESETMAZE, message="Reset Maze")
|
||||
pygame.event.post(reset_maze_event)
|
||||
pygame.time.set_timer(MOVEEVENT, 1000)
|
||||
|
||||
|
||||
while True:
|
||||
@@ -66,18 +79,47 @@ while True:
|
||||
if event.type == QUIT:
|
||||
pygame.quit()
|
||||
sys.exit()
|
||||
if event.type == MOVEEVENT:
|
||||
for agent in agents:
|
||||
agent.move(maze)
|
||||
if agent.location not in maze.visited:
|
||||
maze.visited.add(agent.location)
|
||||
visited_cells_sprites.add(VisitedSpot(agent.location))
|
||||
if agent.location == max(maze.cells):
|
||||
pygame.event.post(reset_maze_event)
|
||||
if maze.check_dead_end(agent.location):
|
||||
dead_end_sprites.add(DeadEndSpot(agent.location))
|
||||
if event.type == RESETMAZE:
|
||||
for agent in agents:
|
||||
agent.reset()
|
||||
maze = Maze(MAZE_SIZE, MAZE_SIZE)
|
||||
maze_sprites.empty()
|
||||
visited_cells_sprites.empty()
|
||||
dead_end_sprites.empty()
|
||||
for wall in maze.walls:
|
||||
wall_sprite = MazeWall(wall)
|
||||
maze_sprites.add(wall_sprite)
|
||||
# wipe buffer
|
||||
displaysurface.fill((0, 0, 0))
|
||||
# draw border
|
||||
pygame.draw.rect(displaysurface, (255, 0, 0),
|
||||
pygame.Rect((SCREEN_BUFFER, SCREEN_BUFFER, MAZE_SIZE * GRID_SIZE, MAZE_SIZE * GRID_SIZE)), 2)
|
||||
pygame.draw.circle(displaysurface, (0, 255, 0), (25, 25), GRID_SIZE / 3)
|
||||
|
||||
# for entity in all_sprites:
|
||||
# displaysurface.blit(entity.surf, entity.rect)
|
||||
from maze import Cell
|
||||
location = Cell(0, 5)
|
||||
|
||||
for entity in visited_cells_sprites:
|
||||
displaysurface.blit(entity.surf, entity.rect)
|
||||
|
||||
for entity in dead_end_sprites:
|
||||
displaysurface.blit(entity.surf, entity.rect)
|
||||
|
||||
for agent in agents:
|
||||
pygame.draw.circle(displaysurface, (0, 255, 0), (agent.get_x_position(), agent.get_y_position()), GRID_SIZE / 3)
|
||||
|
||||
for entity in maze_sprites:
|
||||
displaysurface.blit(entity.surf, entity.rect)
|
||||
|
||||
# draw border
|
||||
pygame.draw.rect(displaysurface, (255, 0, 0),
|
||||
pygame.Rect((SCREEN_BUFFER, SCREEN_BUFFER, MAZE_SIZE * GRID_SIZE, MAZE_SIZE * GRID_SIZE)), 2)
|
||||
|
||||
pygame.display.update()
|
||||
FramePerSec.tick(FPS)
|
||||
Reference in New Issue
Block a user