124 lines
3.7 KiB
Python
124 lines
3.7 KiB
Python
import pygame
|
|
import random
|
|
|
|
pygame.init()
|
|
|
|
SCREEN_WIDTH = 800
|
|
SCREEN_HEIGHT = 600
|
|
WHITE = (255, 255, 255)
|
|
RED = (255, 0, 0)
|
|
BLACK = (0, 0, 0)
|
|
CELL_SIZE = 40
|
|
|
|
maze = [
|
|
"XXXXXXXXXXXXXXXXXXXXX",
|
|
"X X X X X X",
|
|
"X XXXXX XXX X X X X X",
|
|
"X X X X X X X X",
|
|
"X XXX X X X X X X X X",
|
|
"X X X X X X X X",
|
|
"X X XXXXXX XXX XXX X X",
|
|
"X X X X X X",
|
|
"X XXX X XXXXXXXXX X X",
|
|
"X X X X X X",
|
|
"X X X XXXXXXXXX X X X",
|
|
"X X X X X X",
|
|
"X X XXX XXXXX X XXX X",
|
|
"X X X X X X",
|
|
"X XXXXX XXXXXXXXX X X",
|
|
"X X X X",
|
|
"XXXXX XXXXXXXXXXXXX X",
|
|
"X X X",
|
|
"XXXXXXXXXXXXXXXXXXXXX",
|
|
]
|
|
|
|
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
|
|
pygame.display.set_caption("Cheese Maze")
|
|
|
|
class Player(pygame.sprite.Sprite):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.image = pygame.Surface((CELL_SIZE, CELL_SIZE))
|
|
self.image.fill(RED)
|
|
self.rect = self.image.get_rect()
|
|
self.rect.topleft = (CELL_SIZE, CELL_SIZE)
|
|
|
|
def move(self, dx, dy):
|
|
if self.is_valid_move(dx, dy):
|
|
self.rect.x += dx
|
|
self.rect.y += dy
|
|
|
|
def is_valid_move(self, dx, dy):
|
|
new_rect = self.rect.move(dx, dy)
|
|
for y, row in enumerate(maze):
|
|
for x, cell in enumerate(row):
|
|
if cell == "X" and new_rect.colliderect(pygame.Rect(x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE)):
|
|
return False
|
|
return True
|
|
|
|
class Cheese(pygame.sprite.Sprite):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.image = pygame.Surface((CELL_SIZE // 2, CELL_SIZE // 2))
|
|
self.image.fill((255, 255, 0))
|
|
self.rect = self.image.get_rect()
|
|
self.generate_random_position()
|
|
|
|
def generate_random_position(self):
|
|
while True:
|
|
x = random.randint(1, 18)
|
|
y = random.randint(1, 13)
|
|
if maze[y][x] != "X":
|
|
self.rect.topleft = (x * CELL_SIZE, y * CELL_SIZE)
|
|
break
|
|
|
|
all_sprites = pygame.sprite.Group()
|
|
player = Player()
|
|
all_sprites.add(player)
|
|
cheeses = pygame.sprite.Group()
|
|
for _ in range(10):
|
|
cheese = Cheese()
|
|
all_sprites.add(cheese)
|
|
cheeses.add(cheese)
|
|
|
|
running = True
|
|
clock = pygame.time.Clock()
|
|
while running:
|
|
|
|
for event in pygame.event.get():
|
|
if event.type == pygame.QUIT:
|
|
running = False
|
|
|
|
keys = pygame.key.get_pressed()
|
|
dx, dy = 0, 0
|
|
if keys[pygame.K_LEFT]:
|
|
dx = -CELL_SIZE
|
|
if keys[pygame.K_RIGHT]:
|
|
dx = CELL_SIZE
|
|
if keys[pygame.K_UP]:
|
|
dy = -CELL_SIZE
|
|
if keys[pygame.K_DOWN]:
|
|
dy = CELL_SIZE
|
|
player.move(dx, dy)
|
|
|
|
collisions = pygame.sprite.spritecollide(player, cheeses, True)
|
|
for cheese in collisions:
|
|
print("You collected a cheese!")
|
|
|
|
screen_x = max(0, min(player.rect.x - SCREEN_WIDTH // 2, (len(maze[0]) - 1) * CELL_SIZE - SCREEN_WIDTH))
|
|
screen_y = max(0, min(player.rect.y - SCREEN_HEIGHT // 2, (len(maze) - 1) * CELL_SIZE - SCREEN_HEIGHT))
|
|
screen_rect = pygame.Rect(screen_x, screen_y, SCREEN_WIDTH, SCREEN_HEIGHT)
|
|
|
|
screen.fill(BLACK)
|
|
for y, row in enumerate(maze):
|
|
for x, cell in enumerate(row):
|
|
if cell == "X":
|
|
pygame.draw.rect(screen, WHITE, (x * CELL_SIZE - screen_x, y * CELL_SIZE - screen_y, CELL_SIZE, CELL_SIZE))
|
|
for sprite in all_sprites:
|
|
screen.blit(sprite.image, sprite.rect.move(-screen_x, -screen_y))
|
|
pygame.display.flip()
|
|
|
|
clock.tick(10)
|
|
|
|
pygame.quit()
|