Files
maze-game/game.py
2021-09-15 15:14:36 +01:00

83 lines
2.1 KiB
Python

import sys
from itertools import product
import pygame
from pygame.locals import *
from maze import Maze
pygame.init()
vec = pygame.math.Vector2 # 2 for two dimensional
HEIGHT = 450
WIDTH = 400
FPS = 60
FramePerSec = pygame.time.Clock()
displaysurface = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Game")
SCREEN_BUFFER = 10
MAZE_SIZE = 15
GRID_SIZE = 25
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.surf = pygame.Surface((30, 30))
self.surf.fill((0, 255, 0))
self.rect = self.surf.get_rect(center=(10, 420))
class MazeWall(pygame.sprite.Sprite):
def __init__(self, wall):
wall_width = 2
super().__init__()
start_cell, end_cell = wall
x,y = start_cell
vertical = start_cell[1] == end_cell[1]
if vertical:
self.surf = pygame.Surface((wall_width, GRID_SIZE))
else:
self.surf = pygame.Surface((GRID_SIZE, wall_width))
self.surf.fill((0, 0, 255))
if vertical:
self.rect = self.surf.get_rect(midtop=((x+1) * GRID_SIZE + SCREEN_BUFFER, y * GRID_SIZE + SCREEN_BUFFER))
else:
self.rect = self.surf.get_rect(midleft=(x * GRID_SIZE + SCREEN_BUFFER, (y + 1) * GRID_SIZE + SCREEN_BUFFER))
all_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)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
# 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)
for entity in maze_sprites:
displaysurface.blit(entity.surf, entity.rect)
pygame.display.update()
FramePerSec.tick(FPS)