如何在Pygame中实现点击开始按钮后显示3、2、1倒计时再启动游戏
实现点击Start后3-2-1倒计时的方案
Got it, let's add that countdown feature step by step. Here's how you can implement it smoothly without messing up your existing game logic:
核心思路
- 在
game()函数初始化完成后,先启动一个倒计时循环,显示3、2、1,每个数字停留1秒 - 使用
pygame.time.get_ticks()来精准控制时间(避免用time.sleep(),会导致程序冻结) - 倒计时期间保留基础事件处理,防止窗口无响应
- 倒计时结束后,再进入原有的游戏主循环
修改后的完整game()函数
def game(): running = True import pygame, sys import random import math as m # 提前导入pygame常量,避免循环内冗余导入 from pygame.locals import QUIT, KEYDOWN, KEYUP, K_s, K_w, K_UP, K_DOWN, K_ESCAPE mainClock = pygame.time.Clock() pygame.init() pygame.font.init() pygame.display.set_caption('Ping Pong') SCREEN = width, height = 900,600 white = (255, 255, 255) green = (0, 255, 0) blue = (0, 0, 128) black = (0,0,0) speed = [4,4] font = pygame.font.Font('freesansbold.ttf', 32) score = 0 score_str = str(score) font.size(score_str) font.set_bold(True) text = font.render(score_str, True, white) textRect = text.get_rect() textRect.center = ((width/2 - width / 20), (height/60 + height/20)) screen = pygame.display.set_mode(SCREEN, 0,32) height1 = height/4 score = int(score_str) ball = pygame.Rect(width/2, height/2, 50,50) player = pygame.Rect(0,(height/2),25,height1) player_1 = pygame.Rect(width - 25,(height/2),25,height1) font1 = pygame.font.Font('freesansbold.ttf', 32) score1 = 0 score1_str = str(score1) font1.size(score1_str) font1.set_bold(True) text1 = font1.render(score1_str, True, white) score1 = int(score1_str) textRect1 = text1.get_rect() textRect1.center = ((width/2 + width / 15), (height/60 + height/20)) up = False down = False up_1 = False down_1 = False RED = (255,0,0) particles = [] # -------------------------- 新增倒计时逻辑 -------------------------- # 初始化倒计时变量 countdown_num = 3 start_ticks = pygame.time.get_ticks() # 获取当前时间戳 countdown_font = pygame.font.Font('freesansbold.ttf', 120) # 大字体强化视觉效果 countdown_running = True while countdown_running: # 计算已过去的时间(转换为秒) elapsed_time = (pygame.time.get_ticks() - start_ticks) / 1000 # 处理基础事件,防止窗口卡死 for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if event.type == KEYDOWN: if event.key == K_ESCAPE: countdown_running = False running = False # 每1秒切换一次倒计时数字 if elapsed_time >= 1: countdown_num -= 1 start_ticks = pygame.time.get_ticks() # 重置时间戳 # 倒计时结束,退出倒计时循环 if countdown_num == 0: countdown_running = False # 绘制倒计时界面 screen.fill(black) countdown_text = countdown_font.render(str(countdown_num), True, white) countdown_rect = countdown_text.get_rect(center=(width/2, height/2)) screen.blit(countdown_text, countdown_rect) pygame.display.update() mainClock.tick(60) # -------------------------- 倒计时逻辑结束 -------------------------- # 原游戏主循环 while running: color = (192,192,192) # 清空屏幕 screen.fill((0,0,0)) screen.blit(text, textRect) screen.blit(text1, textRect1) bx = ball.centerx by = ball.centery particles.append([[bx, by],[random.randint(0,20) / 10 - 1, -1], random.randint(4,6)]) for particle in particles: particle[0][0] += particle[1][0] particle[0][1] += particle[1][1] particle[2] -= 0.1 particle[1][1] += 0.1 pygame.draw.circle(screen, color, [int(particle[0][0]), int(particle[0][1])], int(particle[2])) if particle[2] <= 0: particles.remove(particle) if up == True: player.y -= 5 if down == True: player.y += 5 if up_1 == True: player_1.y -= 5 if down_1 == True: player_1.y += 5 if player.top < 0 : player.y = 0 if player.bottom > height: player.bottom = height if player_1.top < 0 : player_1.y = 0 if player_1.bottom > height : player_1.bottom = height for i in range(-90,(height + 130), 120): rectangle = pygame.Rect((width/2), i, 13, 60) pygame.draw.rect(screen, (255,255,255), rectangle) pygame.draw.rect(screen,(191, 224, 255),player) pygame.draw.rect(screen,(191, 224, 255),player_1) color = (192,192,192) r = random.randint(0, 255) g = random.randint(0, 255) b = random.randint(0, 255) pygame.draw.rect(screen,color,ball, 3) ball.x += speed[0] ball.y += speed[1] if ball.top < 0 or ball.bottom > height: speed[1] = -speed[1] r = random.randint(0,255) g = random.randint(0,255) b = random.randint(0,255) pygame.draw.rect(screen,(r,b,g),ball, 3) dab = random.randint(0,1) if ball.left < 0 : score1 += 1 score1_str = str(score1) text1 = font1.render(score1_str, True, white) textRect1 = text1.get_rect() textRect1.center = ((width/2 + width / 15), (height/60 + height/20)) ball.x = width/2 ball.y = height/2 if dab == 1: speed[0] = -speed[0] speed[1] = -speed[1] else: speed[0] = abs(speed[0]) speed[1] = abs(speed[1]) if ball.right > width: score += 1 score_str = str(score) text = font.render(score_str, True, white) textRect = text.get_rect() textRect.center = ((width/2 - width / 20), (height/60 + height/20)) ball.x = width/2 ball.y = height/2 if dab == 1: speed[0] = -speed[0] speed[1] = -speed[1] else: speed[0] = abs(speed[0]) speed[1] = abs(speed[1]) # 事件处理 for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if event.type == KEYDOWN: if event.key == K_s: down = True if event.key == K_w: up = True if event.key == K_UP: up_1 =True if event.key == K_ESCAPE: running = False if event.key == K_DOWN: down_1 = True if event.type == KEYUP: if event.key == K_s: down = False if event.key == K_w: up = False if event.key == K_UP: up_1 = False if event.key == K_DOWN: down_1 = False if ball.colliderect(player_1): dab = random.randint(0,1) if dab == 1: speed[1] = -speed[1] speed[0] = -speed[0] r = random.randint(0,255) g = random.randint(0,255) b = random.randint(0,255) pygame.draw.rect(screen,(r,b,g),ball, 3) if ball.colliderect(player): speed[0] = -speed[0] speed[0] = random.randint(4,6) r = random.randint(0,255) g = random.randint(0,255) b = random.randint(0,255) pygame.draw.rect(screen,(r,b,g),ball, 3) # 更新显示 pygame.display.update() mainClock.tick(60)
关键修改点说明
- 新增独立的倒计时循环:在游戏主逻辑前运行,通过时间戳精准控制每个数字的停留时长
- 使用大字号字体显示倒计时,提升玩家感知度
- 倒计时期间保留事件处理逻辑,避免窗口失去响应
- 优化了原代码中的小问题(比如删除冗余的
time模块导入、简化分数更新逻辑)
自定义调整建议
- 如果需要修改倒计时停留时间,把
elapsed_time >= 1中的1改为对应秒数即可(比如2就是每个数字显示2秒) - 可以修改
countdown_font的字号或颜色,匹配你的游戏风格
内容的提问来源于stack exchange,提问作者AK - 10CA - Glenforest SS 2172
相关产品推荐
相关产品推荐

