帮我用python写一个具有关卡具有积分具有游戏状态和界面的小游戏
时间: 2024-02-18 18:05:06 浏览: 144
好的,可以使用Python的Pygame库来创建游戏界面和游戏逻辑。以下是一个简单的示例游戏代码,具有三个关卡,积分系统和游戏状态。
```python
import pygame
# 初始化Pygame
pygame.init()
# 游戏窗口大小
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
# 颜色定义
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# 创建游戏窗口
window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("小游戏")
# 加载游戏资源
font = pygame.font.Font(None, 36)
bg_image = pygame.image.load("bg.png").convert()
player_image = pygame.image.load("player.png").convert_alpha()
# 游戏状态
GAME_STATE_MENU = 0
GAME_STATE_PLAYING = 1
GAME_STATE_GAMEOVER = 2
# 关卡数据
LEVELS = [
{
"background": bg_image,
"player_pos": (50, 50),
"enemy_pos": [(400, 300)],
"enemy_speed": 2,
},
{
"background": bg_image,
"player_pos": (100, 100),
"enemy_pos": [(200, 200), (400, 400)],
"enemy_speed": 3,
},
{
"background": bg_image,
"player_pos": (200, 200),
"enemy_pos": [(100, 100), (300, 300), (500, 500)],
"enemy_speed": 4,
},
]
# 当前关卡
current_level = 0
# 玩家位置
player_x, player_y = LEVELS[current_level]["player_pos"]
# 敌人位置和速度
enemies = []
for enemy_pos in LEVELS[current_level]["enemy_pos"]:
enemies.append([enemy_pos[0], enemy_pos[1], LEVELS[current_level]["enemy_speed"]])
# 积分
score = 0
# 游戏状态
game_state = GAME_STATE_MENU
# 游戏循环
while True:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
# 处理键盘事件
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player_x -= 10
elif event.key == pygame.K_RIGHT:
player_x += 10
elif event.key == pygame.K_UP:
player_y -= 10
elif event.key == pygame.K_DOWN:
player_y += 10
# 游戏逻辑
if game_state == GAME_STATE_MENU:
# 显示菜单
text = font.render("按空格键开始游戏", True, WHITE)
window.blit(text, (WINDOW_WIDTH // 2 - text.get_width() // 2, WINDOW_HEIGHT // 2 - text.get_height() // 2))
# 检查空格键
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
game_state = GAME_STATE_PLAYING
elif game_state == GAME_STATE_PLAYING:
# 游戏进行中
# 绘制背景
window.blit(LEVELS[current_level]["background"], (0, 0))
# 绘制玩家
window.blit(player_image, (player_x, player_y))
# 移动敌人
for enemy in enemies:
enemy[0] += enemy[2]
if enemy[0] > WINDOW_WIDTH:
enemy[0] = -50
# 检查碰撞
if abs(enemy[0] - player_x) < 50 and abs(enemy[1] - player_y) < 50:
game_state = GAME_STATE_GAMEOVER
# 绘制敌人
pygame.draw.circle(window, RED, (enemy[0], enemy[1]), 25)
# 更新积分
score += 1
# 检查通关
if score > 1000:
current_level += 1
if current_level >= len(LEVELS):
game_state = GAME_STATE_GAMEOVER
else:
player_x, player_y = LEVELS[current_level]["player_pos"]
enemies = []
for enemy_pos in LEVELS[current_level]["enemy_pos"]:
enemies.append([enemy_pos[0], enemy_pos[1], LEVELS[current_level]["enemy_speed"]])
score = 0
# 显示积分
text = font.render("积分: " + str(score), True, WHITE)
window.blit(text, (10, 10))
elif game_state == GAME_STATE_GAMEOVER:
# 游戏结束
text = font.render("游戏结束,按空格键重新开始", True, WHITE)
window.blit(text, (WINDOW_WIDTH // 2 - text.get_width() // 2, WINDOW_HEIGHT // 2 - text.get_height() // 2))
# 检查空格键
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
current_level = 0
player_x, player_y = LEVELS[current_level]["player_pos"]
enemies = []
for enemy_pos in LEVELS[current_level]["enemy_pos"]:
enemies.append([enemy_pos[0], enemy_pos[1], LEVELS[current_level]["enemy_speed"]])
score = 0
game_state = GAME_STATE_PLAYING
# 更新屏幕
pygame.display.update()
```
在这个示例代码中,我们创建了一个简单的游戏,具有三个关卡,玩家可以移动和躲避敌人,每个关卡都有一个目标积分,达到目标积分后进入下一关。如果玩家与敌人碰撞,则游戏结束。游戏界面使用Pygame库创建,包括游戏背景、玩家和敌人的图像,以及积分和游戏状态的文本。
阅读全文