用PYTHON写一个吃豆人
时间: 2023-12-26 20:05:44 浏览: 165
Python turtle吃豆人
很高兴回答你的问题!下面是一个简单的吃豆人游戏,使用 Python 编写。
```
import pygame
import random
# 游戏初始化
pygame.init()
width, height = 640, 480
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("吃豆人游戏")
# 颜色定义
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
blue = (0, 0, 255)
yellow = (255, 255, 0)
# 游戏设置
block_size = 20
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 25)
# 游戏数据
score = 0
def draw_score(score):
score_text = font.render("Score: {}".format(score), True, white)
screen.blit(score_text, [0, 0])
def draw_block(color, position):
block = pygame.Rect(position[0], position[1], block_size, block_size)
pygame.draw.rect(screen, color, block)
def game_over():
message("Game Over", red)
pygame.display.update()
pygame.time.delay(2000)
pygame.quit()
quit()
def message(text, color):
text = font.render(text, True, color)
screen.blit(text, [width / 2 - text.get_rect().width / 2, height / 2 - text.get_rect().height / 2])
# 主程序循环
def game_loop():
global score
game_exit = False
# 初始化吃豆人和豆子
player_position = [width / 2, height / 2]
player_movement = [0, 0]
enemy_position = [random.randint(0, width // block_size) * block_size, random.randint(0, height // block_size) * block_size]
enemy_movement = [0, 0]
foods = []
for i in range(10):
food_position = [random.randint(0, width // block_size) * block_size, random.randint(0, height // block_size) * block_size]
foods.append(food_position)
# 游戏循环
while not game_exit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_exit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player_movement[0] = -block_size
player_movement[1] = 0
elif event.key == pygame.K_RIGHT:
player_movement[0] = block_size
player_movement[1] = 0
elif event.key == pygame.K_UP:
player_movement[0] = 0
player_movement[1] = -block_size
elif event.key == pygame.K_DOWN:
player_movement[0] = 0
player_movement[1] = block_size
# 更新吃豆人和豆子位置
player_position[0] += player_movement[0]
player_position[1] += player_movement[1]
enemy_position[0] += enemy_movement[0]
enemy_position[1] += enemy_movement[1]
# 检查是否撞墙
if player_position[0] < 0 or player_position[0] > width - block_size or player_position[1] < 0 or player_position[1] > height - block_size:
game_over()
# 检查是否碰到敌人
if player_position == enemy_position:
game_over()
# 检查是否吃到豆子
for food_position in foods:
if player_position == food_position:
score += 10
foods.remove(food_position)
# 生成新的豆子
while len(foods) < 10:
food_position = [random.randint(0, width // block_size) * block_size, random.randint(0, height // block_size) * block_size]
if food_position not in foods and food_position != player_position and food_position != enemy_position:
foods.append(food_position)
# 绘制游戏界面
screen.fill(black)
draw_block(yellow, player_position)
draw_block(blue, enemy_position)
for food_position in foods:
draw_block(red, food_position)
draw_score(score)
pygame.display.update()
# 控制游戏速度
clock.tick(20)
pygame.quit()
quit()
game_loop()
```
以上是一个简单的吃豆人游戏实现,希望能够帮到你!
阅读全文