spele/ja

215 lines
6.7 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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)
RED = (255, 0, 0)
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) # 奶酪位置
class Villain(pygame.sprite.Sprite):
def __init__(self, x, y, player):
super().__init__()
self.image = pygame.image.load('villain.png').convert_alpha()
self.rect = self.image.get_rect()
self.rect.topleft = (x, y)
self.speed = 8
self.player = player
def move_towards_player(self, walls):
dx = self.player.rect.x - self.rect.x
dy = self.player.rect.y - self.rect.y
distance = max(abs(dx), abs(dy))
dx = dx / distance if distance != 0 else 0
dy = dy / distance if distance != 0 else 0
dx *= self.speed
dy *= self.speed
self.move(dx, dy, walls)
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 draw(self, surface):
surface.blit(self.image, self.rect)
villain = Villain(704, 704, player) # 反派角色的起始位置
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)
villain.move_towards_player(walls)
# 如果玩家与反派角色碰撞,则游戏结束
if player.rect.colliderect(villain.rect):
print("Game Over! You were caught by the villain.")
running = False
# 如果玩家与奶酪碰撞,增加奶酪计数并将奶酪移动到新位置
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) # 移除奶酪
# 如果玩家收集了10个奶酪则结束游戏
if player.cheese_count >= 10:
print("Congratulations! You collected 10 cheeses!")
running = False
screen.fill(BLUE)
draw_maze(maze)
player.draw(screen)
villain.draw(screen)
pygame.display.flip()
clock.tick(10)
pygame.quit()
sys.exit()