🎹 Python钢琴节奏大师 · 第9章
系列:Python钢琴节奏大师:从零到游戏开发实战[1]章节:第9章 / 共12章标题:游戏系统篇-状态管理与生命值机制设计上一章:第8章-节奏游戏篇[2]下一章:第10章-视觉美化篇[3]
计分与结束——真正的游戏
今日目标:完善计分系统,添加游戏结束判定,实现重新开始功能,完成游戏状态管理。
💡 本章核心:实现完整的游戏系统,让游戏有始有终
🎹 效果预览
完成本篇学习后,你的游戏会有:
┌────────────────────────────────────┐│ ││ 🎹 钢琴节奏大师 🎹 ││ ││ 最高分:5000 ││ ││ [按空格键开始游戏] ││ │└────────────────────────────────────┘ ↓ 游戏进行中 ↓┌────────────────────────────────────┐│ 分数:3250 生命:❤️❤️❤️💔💔 ││ ││ 游戏结束! ││ ││ 最终得分:3250 ││ 最高连击:45 ││ 准确率:87% ││ ││ [按R重新开始] [按Q退出] ││ │└────────────────────────────────────┘
🤔 什么是游戏状态?
游戏通常有多个"状态":
| | |
|---|
| MENU | | |
| PLAYING | | |
| PAUSED | | |
| GAME_OVER | | |
为什么要分状态?
📝 第一步:定义游戏状态
# 游戏状态常量STATE_MENU = 0STATE_PLAYING = 1STATE_PAUSED = 2STATE_GAME_OVER = 3# 当前状态current_state = STATE_MENU
使用枚举(更专业)
from enum import EnumclassGameState(Enum): MENU = 0 PLAYING = 1 PAUSED = 2 GAME_OVER = 3current_state = GameState.MENU
📝 第二步:添加生命值系统
生命值的实现
# 游戏参数MAX_LIVES = 5# 最大生命值lives = MAX_LIVES # 当前生命值# 当错过音符时if note.y > JUDGMENT_LINE + JUDGMENT_RANGE andnot note.hit: lives -= 1# 扣一滴血 combo = 0# 连击中断if lives <= 0: current_state = STATE_GAME_OVER # 生命为0,游戏结束
显示生命值
defdraw_lives(screen, lives):"""绘制生命值""" heart_full = "❤️"# 满血 heart_empty = "💔"# 空血 lives_text = ""for i inrange(MAX_LIVES):if i < lives: lives_text += heart_fullelse: lives_text += heart_empty text = font.render(f"生命:{lives_text}", True, WHITE) screen.blit(text, (20, 100))
📝 第三步:菜单界面
菜单显示
defdraw_menu(screen):"""绘制菜单界面""" screen.fill((30, 30, 50)) # 深蓝紫色背景# 标题 title = big_font.render("🎹 钢琴节奏大师 🎹", True, (255, 200, 100)) screen.blit(title, (SCREEN_WIDTH//2 - title.get_width()//2, 150))# 最高分 high_score_text = font.render(f"最高分:{high_score}", True, WHITE) screen.blit(high_score_text, (SCREEN_WIDTH//2 - high_score_text.get_width()//2, 250))# 操作说明 instructions = ["操作说明:","A S D F G H J - 白键(do re mi fa sol la si)","W E T Y U - 黑键(#do #re #fa #sol #la)","","按空格键开始游戏" ] y = 350for line in instructions: text = small_font.render(line, True, (200, 200, 200)) screen.blit(text, (SCREEN_WIDTH//2 - text.get_width()//2, y)) y += 30
菜单事件处理
if current_state == STATE_MENU:if event.type == pygame.KEYDOWN:if event.key == pygame.K_SPACE: reset_game() # 重置游戏 current_state = STATE_PLAYING
📝 第四步:游戏结束界面
显示最终成绩
defdraw_game_over(screen):"""绘制游戏结束界面"""# 半透明黑色遮罩 overlay = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT)) overlay.fill(BLACK) overlay.set_alpha(200) # 透明度 screen.blit(overlay, (0, 0))# 游戏结束标题 title = big_font.render("游戏结束!", True, (255, 100, 100)) screen.blit(title, (SCREEN_WIDTH//2 - title.get_width()//2, 150))# 成绩统计 stats = [f"最终得分:{score}",f"最高连击:{max_combo}",f"击中次数:{hits}",f"错过次数:{misses}",f"准确率:{hits/(hits+misses)*100:.1f}%"if (hits+misses) > 0else"准确率:0%" ] y = 250for stat in stats: text = font.render(stat, True, WHITE) screen.blit(text, (SCREEN_WIDTH//2 - text.get_width()//2, y)) y += 40# 操作提示 hint = small_font.render("按 R 重新开始 按 Q 退出", True, (200, 200, 200)) screen.blit(hint, (SCREEN_WIDTH//2 - hint.get_width()//2, 500))
更新最高分
# 最高分记录(保存在内存中,游戏重启会重置)high_score = 0defupdate_high_score():"""更新最高分"""global high_scoreif score > high_score: high_score = score
📝 第五步:重置游戏
defreset_game():"""重置游戏状态"""global score, combo, max_combo, hits, misses, livesglobal notes, spawn_timer, white_key_pressed score = 0 combo = 0 max_combo = 0 hits = 0 misses = 0 lives = MAX_LIVES notes = [] spawn_timer = 0 white_key_pressed = [False] * WHITE_KEY_COUNT
🎯 完整代码
import pygameimport randompygame.init()pygame.mixer.init()# 窗口SCREEN_WIDTH, SCREEN_HEIGHT = 800, 600screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))pygame.display.set_caption("钢琴节奏大师 - 完整版")clock = pygame.time.Clock()# 颜色BLACK, WHITE, GRAY, DARK_GRAY = (0,0,0), (255,255,255), (200,200,200), (50,50,50)YELLOW, NOTE_COLOR, RED = (255,255,0), (255,100,100), (255,100,100)# 游戏状态STATE_MENU = 0STATE_PLAYING = 1STATE_PAUSED = 2STATE_GAME_OVER = 3current_state = STATE_MENU# 钢琴键参数WHITE_KEY_WIDTH, WHITE_KEY_HEIGHT = 80, 300WHITE_KEY_COUNT = 7START_X, START_Y = 100, 350JUDGMENT_LINE = START_YJUDGMENT_RANGE = 30# 键盘映射WHITE_KEY_MAPPING = { pygame.K_a: 0, pygame.K_s: 1, pygame.K_d: 2, pygame.K_f: 3, pygame.K_g: 4, pygame.K_h: 5, pygame.K_j: 6,}WHITE_KEY_NAMES = ["do", "re", "mi", "fa", "sol", "la", "si"]WHITE_KEY_CHARS = ["A", "S", "D", "F", "G", "H", "J"]# 音符类classNote:def__init__(self, lane):self.lane = laneself.y = -50self.speed = 5self.active = Trueself.hit = Falsedefupdate(self):self.y += self.speedifself.y > SCREEN_HEIGHT + 50:self.active = Falsedefdraw(self, screen):ifself.active: x = START_X + self.lane * WHITE_KEY_WIDTH + WHITE_KEY_WIDTH // 2 pygame.draw.circle(screen, NOTE_COLOR, (int(x), int(self.y)), 15) pygame.draw.line(screen, NOTE_COLOR, (int(x), int(self.y)), (int(x), int(self.y)-40), 4)# 字体font = pygame.font.SysFont(None, 36)big_font = pygame.font.SysFont(None, 72)small_font = pygame.font.SysFont(None, 24)# 游戏状态变量score = combo = max_combo = hits = misses = 0lives = 5MAX_LIVES = 5high_score = 0notes = []spawn_timer = 0SPAWN_INTERVAL = 45white_key_pressed = [False] * WHITE_KEY_COUNT# 辅助函数defreset_game():global score, combo, max_combo, hits, misses, lives, notes, spawn_timer, white_key_pressed score = combo = max_combo = hits = misses = 0 lives = MAX_LIVES notes = [] spawn_timer = 0 white_key_pressed = [False] * WHITE_KEY_COUNTdefcheck_hit(lane):for note in notes:if note.active andnot note.hit and note.lane == lane: distance = abs(note.y - JUDGMENT_LINE)if distance <= JUDGMENT_RANGE: note.hit, note.active = True, FalsereturnTrue, distancereturnFalse, Nonedefadd_score(distance):global score, combo, hits, max_comboif distance <= 10: points, combo = 100, combo + 1 judgment = "Perfect!"elif distance <= 20: points, combo = 80, combo + 1 judgment = "Great!"elif distance <= 30: points, combo = 50, combo + 1 judgment = "Good"else: points, combo = 0, 0 judgment = "Miss" score += points + combo * 5 hits += 1 max_combo = max(max_combo, combo)return judgment, pointsdefspawn_note(): notes.append(Note(random.randint(0, WHITE_KEY_COUNT - 1)))defdraw_menu(): screen.fill((30, 30, 50)) title = big_font.render("钢琴节奏大师", True, (255, 200, 100)) screen.blit(title, (SCREEN_WIDTH//2 - title.get_width()//2, 150)) high = font.render(f"最高分:{high_score}", True, WHITE) screen.blit(high, (SCREEN_WIDTH//2 - high.get_width()//2, 250)) hint = small_font.render("按空格键开始游戏", True, (200, 200, 200)) screen.blit(hint, (SCREEN_WIDTH//2 - hint.get_width()//2, 350))defdraw_game_over(): overlay = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT)) overlay.fill(BLACK) overlay.set_alpha(200) screen.blit(overlay, (0, 0)) title = big_font.render("游戏结束!", True, RED) screen.blit(title, (SCREEN_WIDTH//2 - title.get_width()//2, 150)) stats = [f"最终得分:{score}",f"最高连击:{max_combo}",f"准确率:{hits/(hits+misses)*100:.1f}%"if (hits+misses) > 0else"准确率:0%" ] y = 250for stat in stats: text = font.render(stat, True, WHITE) screen.blit(text, (SCREEN_WIDTH//2 - text.get_width()//2, y)) y += 50 hint = small_font.render("按 R 重新开始 按 Q 退出", True, (200, 200, 200)) screen.blit(hint, (SCREEN_WIDTH//2 - hint.get_width()//2, 500))defdraw_lives(): lives_text = f"生命:{'❤️' * lives}{'💔' * (MAX_LIVES - lives)}" text = font.render(lives_text, True, WHITE) screen.blit(text, (20, 100))# 游戏循环running = Truejudgment_text = ""judgment_timer = 0while running: clock.tick(60)# 事件处理for event in pygame.event.get():if event.type == pygame.QUIT: running = Falseif current_state == STATE_MENU:if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: reset_game() current_state = STATE_PLAYINGelif current_state == STATE_PLAYING:if event.type == pygame.KEYDOWN:if event.key == pygame.K_ESCAPE: current_state = STATE_MENUelif event.key in WHITE_KEY_MAPPING: idx = WHITE_KEY_MAPPING[event.key] white_key_pressed[idx] = True hit, dist = check_hit(idx)if hit: j, p = add_score(dist) judgment_text, judgment_timer = f"{j} +{p}", 30else: judgment_text, judgment_timer = "空按", 20elif event.type == pygame.KEYUP:if event.key in WHITE_KEY_MAPPING: white_key_pressed[WHITE_KEY_MAPPING[event.key]] = Falseelif current_state == STATE_GAME_OVER:if event.type == pygame.KEYDOWN:if event.key == pygame.K_r: reset_game() current_state = STATE_PLAYINGelif event.key == pygame.K_q: running = False# 更新if current_state == STATE_PLAYING: spawn_timer += 1if spawn_timer >= SPAWN_INTERVAL: spawn_note() spawn_timer = 0for note in notes: note.update()if note.active and note.y > JUDGMENT_LINE + JUDGMENT_RANGE andnot note.hit: lives -= 1 combo = 0 misses += 1 judgment_text, judgment_timer = "Miss!", 30 note.active = Falseif lives <= 0: current_state = STATE_GAME_OVERif score > high_score: high_score = score notes = [n for n in notes if n.active] judgment_timer = max(0, judgment_timer - 1)if judgment_timer == 0: judgment_text = ""# 绘制 screen.fill(DARK_GRAY)if current_state == STATE_MENU: draw_menu()elif current_state == STATE_PLAYING:# 判定线 pygame.draw.line(screen, YELLOW, (START_X, JUDGMENT_LINE), (START_X + WHITE_KEY_COUNT * WHITE_KEY_WIDTH, JUDGMENT_LINE), 3)# 音符for note in notes: note.draw(screen)# 白键for i inrange(WHITE_KEY_COUNT): x = START_X + i * WHITE_KEY_WIDTH color = YELLOW if white_key_pressed[i] else WHITE pygame.draw.rect(screen, color, (x, START_Y, WHITE_KEY_WIDTH, WHITE_KEY_HEIGHT)) pygame.draw.rect(screen, GRAY, (x, START_Y, WHITE_KEY_WIDTH, WHITE_KEY_HEIGHT), 1) screen.blit(small_font.render(WHITE_KEY_CHARS[i], True, DARK_GRAY), (x + 35, START_Y + 10)) screen.blit(font.render(WHITE_KEY_NAMES[i], True, BLACK), (x + 25, START_Y + 250))# UI screen.blit(font.render(f"分数:{score}", True, WHITE), (20, 20)) screen.blit(font.render(f"连击:{combo}", True, WHITE), (20, 60)) draw_lives()if combo > 0: screen.blit(big_font.render(str(combo), True, YELLOW), (SCREEN_WIDTH//2 - 20, 80))if judgment_text: screen.blit(big_font.render(judgment_text, True, YELLOW), (SCREEN_WIDTH//2 - 100, 150))elif current_state == STATE_GAME_OVER:# 先绘制游戏画面 pygame.draw.line(screen, YELLOW, (START_X, JUDGMENT_LINE), (START_X + WHITE_KEY_COUNT * WHITE_KEY_WIDTH, JUDGMENT_LINE), 3)for note in notes: note.draw(screen)for i inrange(WHITE_KEY_COUNT): x = START_X + i * WHITE_KEY_WIDTH pygame.draw.rect(screen, WHITE, (x, START_Y, WHITE_KEY_WIDTH, WHITE_KEY_HEIGHT))# 再绘制结束界面 draw_game_over() pygame.display.flip()pygame.quit()
🔍 核心概念
1. 游戏状态管理
if current_state == STATE_MENU:# 显示菜单elif current_state == STATE_PLAYING:# 游戏进行中elif current_state == STATE_GAME_OVER:# 游戏结束
2. 生命值系统
lives = MAX_LIVES # 初始满血# 受伤lives -= 1# 检查死亡if lives <= 0: game_over()
3. 最高分记录
if score > high_score: high_score = score # 更新最高分
🎨 动手改造
改造1:添加暂停功能
if event.key == pygame.K_SPACE:if current_state == STATE_PLAYING: current_state = STATE_PAUSEDelif current_state == STATE_PAUSED: current_state = STATE_PLAYING
改造2:难度递增
# 随着分数增加,速度加快note.speed = 5 + score // 1000
改造3:保存最高分到文件
# 保存withopen("highscore.txt", "w") as f: f.write(str(high_score))# 读取try:withopen("highscore.txt", "r") as f: high_score = int(f.read())except: high_score = 0
❓ 常见问题
Q1:游戏结束后无法重新开始
检查:reset_game() 函数是否正确重置所有变量?
Q2:生命值显示异常
检查:lives 是否可能变成负数?添加保护:lives = max(0, lives)
Q3:状态切换没反应
检查:事件处理是否在正确的状态分支中?
Q4:最高分没保存
说明:当前版本最高分只在内存中,重启游戏会重置。
🎉 恭喜你完成第9课!
今天你已经:
你的钢琴游戏现在是一个完整的游戏了!
本系列文章面向零基础中小学生,每篇都有详细步骤和配图说明。遇到问题欢迎在评论区留言!关注回复“钢琴”免费获取源代码下载地址。