帮我用python做一个吃豆人小游戏
时间: 2024-02-01 20:03:57 浏览: 144
好的,下面是一个简单的吃豆人游戏的Python代码示例:
```python
import pygame
import random
# 初始化Pygame
pygame.init()
# 游戏窗口大小
screen_width = 500
screen_height = 500
# 创建游戏窗口
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("吃豆人小游戏")
# 设置游戏时钟
clock = pygame.time.Clock()
# 加载游戏素材
player_image = pygame.image.load("player.png")
enemy_image = pygame.image.load("enemy.png")
food_image = pygame.image.load("food.png")
# 设置游戏参数
player_size = 50
enemy_size = 50
food_size = 10
player_speed = 5
enemy_speed = 3
food_points = 10
# 定义游戏对象类
class GameObject:
def __init__(self, x, y, image, size):
self.x = x
self.y = y
self.image = pygame.transform.scale(image, (size, size))
self.rect = self.image.get_rect()
def draw(self):
screen.blit(self.image, (self.x, self.y))
# 定义玩家类
class Player(GameObject):
def __init__(self, x, y, image, size, speed):
super().__init__(x, y, image, size)
self.speed = speed
def move(self, dx, dy):
self.x += dx * self.speed
self.y += dy * self.speed
# 碰撞检测
if self.x < 0:
self.x = 0
elif self.x > screen_width - player_size:
self.x = screen_width - player_size
if self.y < 0:
self.y = 0
elif self.y > screen_height - player_size:
self.y = screen_height - player_size
# 定义敌人类
class Enemy(GameObject):
def __init__(self, x, y, image, size, speed):
super().__init__(x, y, image, size)
self.speed = speed
self.direction = random.choice(["left", "right", "up", "down"])
def move(self):
if self.direction == "left":
self.x -= self.speed
elif self.direction == "right":
self.x += self.speed
elif self.direction == "up":
self.y -= self.speed
elif self.direction == "down":
self.y += self.speed
# 碰撞检测
if self.x < 0:
self.direction = "right"
elif self.x > screen_width - enemy_size:
self.direction = "left"
if self.y < 0:
self.direction = "down"
elif self.y > screen_height - enemy_size:
self.direction = "up"
# 定义食物类
class Food(GameObject):
def __init__(self, x, y, image, size, points):
super().__init__(x, y, image, size)
self.points = points
# 初始化游戏对象
player = Player(screen_width // 2, screen_height // 2, player_image, player_size, player_speed)
enemies = []
for i in range(3):
enemy = Enemy(random.randint(0, screen_width - enemy_size), random.randint(0, screen_height - enemy_size), enemy_image, enemy_size, enemy_speed)
enemies.append(enemy)
foods = []
for i in range(10):
food = Food(random.randint(0, screen_width - food_size), random.randint(0, screen_height - food_size), food_image, food_size, food_points)
foods.append(food)
# 定义游戏循环
game_over = False
score = 0
while not game_over:
# 处理游戏事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.move(-1, 0)
elif event.key == pygame.K_RIGHT:
player.move(1, 0)
elif event.key == pygame.K_UP:
player.move(0, -1)
elif event.key == pygame.K_DOWN:
player.move(0, 1)
# 更新游戏对象状态
for enemy in enemies:
enemy.move()
if player.rect.colliderect(enemy.rect):
game_over = True
for food in foods:
if player.rect.colliderect(food.rect):
score += food.points
foods.remove(food)
# 绘制游戏界面
screen.fill((255, 255, 255))
player.draw()
for enemy in enemies:
enemy.draw()
for food in foods:
food.draw()
pygame.display.update()
# 控制游戏帧率
clock.tick(60)
# 显示游戏结束信息
font = pygame.font.Font(None, 36)
text = font.render("Game Over", True, (255, 0, 0))
screen.blit(text, (screen_width // 2 - text.get_width() // 2, screen_height // 2 - text.get_height() // 2))
text = font.render("Score: " + str(score), True, (255, 0, 0))
screen.blit(text, (screen_width // 2 - text.get_width() // 2, screen_height // 2 - text.get_height() // 2 + 50))
pygame.display.update()
# 等待退出游戏
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
```
在运行代码之前,需要准备一些游戏素材,包括吃豆人、敌人和食物的图片。将这些图片放入代码所在目录下,并将图片文件名更新到代码中的相应位置。
运行代码后,可以使用方向键控制吃豆人的移动,吃掉食物可以得分,撞到敌人就游戏结束。最终游戏得分将显示在游戏结束画面上。
阅读全文