如何更新Pygame乒乓球游戏中P1_score与P2_score的显示文本?
Pong游戏分数文本不更新问题解决方法
基于ClearCode2020年的乒乓球游戏视频开发适配Python3.9的Pong游戏,代码已适配可运行,但存在分数更新后文本不刷新的问题:P1_score或P2_score加1后,屏幕上的分数文本没有变化,重复调用screen.blit也无法解决。
问题原因
你当前的代码中,P1_text和P2_text仅在程序初始化时调用game_font.render()生成了一次文本表面。之后即使P1_score/P2_score数值变化,你依然在主循环中blit最初生成的旧文本表面,所以屏幕上的分数不会更新。
解决方案
需要在每次主循环迭代时,根据当前最新的分数值重新渲染文本表面,再将其blit到屏幕上。这样分数变化时,新的文本会被渲染并显示。
修改后的完整代码
import pygame, sys, random pygame.init() clock = pygame.time.Clock() Width = 1600 Length = 900 Line_Width = 15 Line_Color = (255, 255, 255) Color = (0, 0, 0) NetColor = (150, 150, 150) screen = pygame.display.set_mode((Width, Length)) pygame.display.set_caption('Spy-pong!') screen.fill(Color) ball = pygame.Rect(Width/2 - 15, Length/2 - 15, 30, 30) play_paddle = pygame.Rect(Width - 20, Length/2 - 70, 10, 140) com_paddle = pygame.Rect(10, Length/2 - 70, 10, 140) def ball_animation(): global ball_mspeed_x, ball_mspeed_y, P1_score, P2_score ball.x += ball_mspeed_x ball.y += ball_mspeed_y if ball.top <= 0 or ball.bottom >= Length: ball_mspeed_y *= -1 if ball.left <= 0: P1_score += 1 ball_restart() if ball.right >= Width: P2_score += 1 ball_restart() if ball.colliderect(play_paddle) or ball.colliderect(com_paddle): ball_mspeed_x *= -1 ball_mspeed_x = 15 * random.choice((1, -1)) ball_mspeed_y = 15 * random.choice((1, -1)) player_speed = 0 com_speed = 13 P1_score = 0 P2_score = 0 game_font = pygame.font.Font("freesansbold.ttf",32) def play_paddle_animation(): play_paddle.y += player_speed if play_paddle.top <= 0: play_paddle.top = 0 if play_paddle.bottom >= Length: play_paddle.bottom = Length def com_paddle_ai(): if com_paddle.top < ball.y: com_paddle.top += com_speed if com_paddle.bottom > ball.y: com_paddle.bottom -= com_speed if com_paddle.top <= 0: com_paddle.top = 0 if com_paddle.bottom >= Length: com_paddle.bottom = Length def ball_restart(): global ball_mspeed_x, ball_mspeed_y ball.center = (Width/2, Length/2) ball_mspeed_x *= random.choice((1,-1)) ball_mspeed_y *= random.choice((1,-1)) def draw_rect(): pygame.draw.rect(screen, Line_Color, play_paddle) pygame.draw.rect(screen, Line_Color, com_paddle) def draw_ellipse(): pygame.draw.ellipse(screen, Line_Color, ball) def draw_aaline(): pygame.draw.aaline(screen, NetColor, (Width/2, 0), (Width/2, Length)) while True: screen.fill(Color) # 关键修改:每次循环都根据当前分数重新渲染文本 P1_text = game_font.render(f"{P1_score}", False, Line_Color) screen.blit(P1_text,(835, 450)) P2_text = game_font.render(f"{P2_score}", False, Line_Color) screen.blit(P2_text,(750, 450)) draw_rect() draw_ellipse() draw_aaline() for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_DOWN: player_speed += 15 if event.key == pygame.K_UP: player_speed -= 15 if event.type == pygame.KEYUP: if event.key == pygame.K_DOWN: player_speed -= 15 if event.key == pygame.K_UP: player_speed += 15 ball_animation() play_paddle_animation() com_paddle_ai() pygame.display.update() clock.tick(60)
修改说明
- 删除了初始化阶段的
P1_text/P2_text渲染和blit代码 - 在主循环内,每次填充背景后,先调用
game_font.render()根据当前的P1_score/P2_score生成新的文本表面,再执行screen.blit()将其绘制到屏幕上 - 修正了原代码中的
pygame.QUIT()错误,改为pygame.quit()(小写q)
这样修改后,每次分数变化时,主循环会在下一次迭代中渲染新的分数文本并显示,解决了分数不更新的问题。
内容的提问来源于stack exchange,提问作者Drakenforge42
相关产品推荐
相关产品推荐

