162 lines
5.0 KiB
Python
162 lines
5.0 KiB
Python
import pygame
|
|
import sys
|
|
|
|
pygame.init()
|
|
|
|
width, height = 800, 800
|
|
screen = pygame.display.set_mode((width, height))
|
|
pygame.display.set_caption('Labirints')
|
|
|
|
BLACK = (0, 0, 0)
|
|
BLUE = (0, 0, 255)
|
|
|
|
maze = [
|
|
"#########################",
|
|
"#......#..........#.....#",
|
|
"######.########.#.#.#####",
|
|
"#...##.#........#.#.....#",
|
|
"#.#.##...########.#####.#",
|
|
"#.#.#########.....#...#.#",
|
|
"#.#.......#.#.#####.#.#.#",
|
|
"#.##..###.#.#.#.....#...#",
|
|
"#.....###.#.#.#.#######.#",
|
|
"#.##....#...#...#.#.....#",
|
|
"#.##.##.###.#####.#.##.##",
|
|
"#.....#...#...#...#.#...#",
|
|
"#.#.#.###.###.#.###.#.#.#",
|
|
"#.#.#...........#...#.#.#",
|
|
"#.#.##.###.####.#.###.#.#",
|
|
"#.#.#...##.##...#.#...#.#",
|
|
"#.#.#.#.##....###.#.###.#",
|
|
"#.#.#.#....##.#...#.#...#",
|
|
"#.#.#.#.##..###.###.#.###",
|
|
"#.......................#",
|
|
"#########################"
|
|
]
|
|
|
|
class Player(pygame.sprite.Sprite):
|
|
def __init__(self, x, y):
|
|
super().__init__()
|
|
self.images = {
|
|
"up": [pygame.image.load('up.png').convert_alpha(),
|
|
pygame.image.load('up1.png').convert_alpha()],
|
|
"down": [pygame.image.load('down.png').convert_alpha(),
|
|
pygame.image.load('down1.png').convert_alpha()],
|
|
"left": [pygame.image.load('left.png').convert_alpha(),
|
|
pygame.image.load('left1.png').convert_alpha()],
|
|
"right": [pygame.image.load('right.png').convert_alpha(),
|
|
pygame.image.load('right1.png').convert_alpha()]
|
|
}
|
|
self.direction = "down" # Initial direction
|
|
self.image_index = 0 # Index of the current image in the animation
|
|
self.image = self.images[self.direction][self.image_index]
|
|
self.rect = self.image.get_rect()
|
|
self.rect.topleft = (x, y)
|
|
self.speed = 10
|
|
self.cheese_count = 0 # 奶酪计数
|
|
|
|
def move(self, dx, dy, walls):
|
|
old_rect = self.rect.copy()
|
|
self.rect.x += dx
|
|
self.rect.y += dy
|
|
for wall in walls:
|
|
if self.rect.colliderect(wall):
|
|
if dx > 0:
|
|
self.rect.right = wall.left
|
|
elif dx < 0:
|
|
self.rect.left = wall.right
|
|
elif dy > 0:
|
|
self.rect.bottom = wall.top
|
|
elif dy < 0:
|
|
self.rect.top = wall.bottom
|
|
if self.rect.collidelist(walls) != -1:
|
|
self.rect = old_rect
|
|
|
|
def update_image(self, dx, dy):
|
|
if dx > 0:
|
|
self.direction = "right"
|
|
elif dx < 0:
|
|
self.direction = "left"
|
|
elif dy > 0:
|
|
self.direction = "down"
|
|
elif dy < 0:
|
|
self.direction = "up"
|
|
|
|
# Update the image index for animation
|
|
self.image_index = (self.image_index + 1) % len(self.images[self.direction])
|
|
self.image = self.images[self.direction][self.image_index]
|
|
|
|
def draw(self, surface):
|
|
surface.blit(self.image, self.rect)
|
|
|
|
class Cheese(pygame.sprite.Sprite):
|
|
def __init__(self, x, y):
|
|
super().__init__()
|
|
self.image = pygame.image.load('cheese.png').convert_alpha()
|
|
self.rect = self.image.get_rect()
|
|
self.rect.topleft = (x, y)
|
|
|
|
def draw_maze(maze):
|
|
walls = []
|
|
for y, row in enumerate(maze):
|
|
for x, char in enumerate(row):
|
|
if char == '#':
|
|
pygame.draw.rect(screen, BLACK, (x * 32, y * 32, 32, 32))
|
|
walls.append(pygame.Rect(x * 32, y * 32, 32, 32))
|
|
elif char == 'C': # 奶酪
|
|
screen.blit(pygame.image.load('cheese.png'), (x * 32, y * 32))
|
|
return walls
|
|
|
|
player = Player(32, 32)
|
|
cheese = Cheese(416, 416) # 奶酪位置
|
|
|
|
clock = pygame.time.Clock()
|
|
running = True
|
|
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 = -player.speed
|
|
elif keys[pygame.K_RIGHT]:
|
|
dx = player.speed
|
|
|
|
if keys[pygame.K_UP]:
|
|
dy = -player.speed
|
|
elif keys[pygame.K_DOWN]:
|
|
dy = player.speed
|
|
|
|
if dx != 0 and dy != 0:
|
|
if keys[pygame.K_LEFT]:
|
|
dy = 0
|
|
elif keys[pygame.K_RIGHT]:
|
|
dy = 0
|
|
elif keys[pygame.K_UP]:
|
|
dx = 0
|
|
elif keys[pygame.K_DOWN]:
|
|
dx = 0
|
|
|
|
walls = draw_maze(maze)
|
|
player.move(dx, dy, walls)
|
|
player.update_image(dx, dy)
|
|
|
|
# 检查玩家是否与奶酪碰撞
|
|
if player.rect.colliderect(cheese.rect):
|
|
player.cheese_count += 1
|
|
print("You collected a cheese! Total cheese count:", player.cheese_count)
|
|
cheese.rect.topleft = (-32, -32) # 移除奶酪
|
|
|
|
screen.fill(BLUE)
|
|
draw_maze(maze)
|
|
player.draw(screen)
|
|
pygame.display.flip()
|
|
clock.tick(10)
|
|
|
|
pygame.quit()
|
|
sys.exit()
|
|
|