Pygame计分文本不显示求助:乒乓球游戏无报错但无文字
Pygame乒乓球游戏计分不显示问题修复
核心问题及修复步骤
- 绘制顺序错误:你在填充背景色之前调用了
draw_playscore(),背景色会直接覆盖已绘制的文字。需将文字绘制操作移到所有图形绘制完成之后、pygame.display.update()之前。 - 文字渲染颜色参数错误:
pygame.font.Font.render()的第三个参数要求是RGB颜色元组,你传入了三个单独的255,导致文字颜色异常(实际为灰色而非白色)。正确写法是(255, 255, 255)。 - 事件处理缩进错误:键盘按键的
KEYDOWN/KEYUP判断不在事件循环内部,会导致按键控制失效,需将这部分代码缩进至for event in pygame.event.get():循环内。 - 额外优化:
ball_restart()函数仅重置了ball_speed_y的方向,得分后球会一直向同一方向移动,建议同时随机化ball_speed_x的方向。
修正后的完整代码
import pygame, random pygame.font.init() opponent_score = 0 player_score = 0 smallfont = pygame.font.Font(None, 50) def draw_playscore(): # 修正颜色参数为RGB元组,同时添加对手分数显示 player_text = smallfont.render(f"Player: {player_score}", True, (255, 255, 255)) opponent_text = smallfont.render(f"Opponent: {opponent_score}", True, (255, 255, 255)) # 分位置绘制分数,避免重叠 screen.blit(player_text, (SCREEN_WIDTH - 200, 20)) screen.blit(opponent_text, (20, 20)) def ball_animation(): global opponent_score global player_score global ball_speed_x, ball_speed_y ball.x += ball_speed_x ball.y += ball_speed_y if ball.top <= 0 or ball.bottom >= SCREEN_HEIGHT: ball_speed_y *= -1 if ball.left <=0: ball_restart() player_score +=1 elif ball.right >= SCREEN_WIDTH: ball_restart() opponent_score +=1 if ball.colliderect(player) or ball.colliderect(opponent): ball_speed_x *= -1 def player_animation(): player.y += player_speed if player.top <= 0: player.top = 0 if player.bottom >= SCREEN_HEIGHT: player.bottom = SCREEN_HEIGHT def opponent_animation(): if opponent.top < ball.y: opponent.top += opponent_speed if opponent.bottom > ball.y: opponent.bottom -= opponent_speed if opponent.top <= 0: opponent.top = 0 if opponent.bottom >= SCREEN_HEIGHT: opponent.bottom = SCREEN_HEIGHT def ball_restart(): global ball_speed_x, ball_speed_y ball.center = (SCREEN_WIDTH/2, SCREEN_HEIGHT/2) # 同时随机化x和y方向的速度 ball_speed_y *= random.choice((1,-1)) ball_speed_x *= random.choice((1,-1)) pygame.init() clock = pygame.time.Clock() SCREEN_WIDTH = 800 SCREEN_HEIGHT = 600 screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) ball = pygame.Rect(SCREEN_WIDTH/2 - 15, SCREEN_HEIGHT/2 -15, 30, 30) player = pygame.Rect(SCREEN_WIDTH -20, SCREEN_HEIGHT/2 -70, 10, 140) opponent = pygame.Rect(10, SCREEN_HEIGHT/2 -70, 10, 140) bg_color = pygame.Color('grey12') light_grey = (200,200,200) ball_speed_x = 5 ball_speed_y = 5 player_speed = 0 opponent_speed = 7 run = True while run: for event in pygame.event.get(): if event.type == pygame.QUIT: run = False # 修正缩进:将按键处理移到事件循环内 if event.type == pygame.KEYDOWN: if event.key == pygame.K_DOWN: player_speed +=2 if event.key == pygame.K_UP: player_speed -=2 if event.type == pygame.KEYUP: if event.key == pygame.K_DOWN: player_speed =0 if event.key == pygame.K_UP: player_speed =0 ball_animation() player_animation() opponent_animation() # 先绘制背景和游戏元素,再绘制文字避免被覆盖 screen.fill(bg_color) pygame.draw.rect(screen, light_grey, player) pygame.draw.rect(screen, light_grey, opponent) pygame.draw.ellipse(screen, light_grey, ball) pygame.draw.aaline(screen, light_grey, (SCREEN_WIDTH/2,0), (SCREEN_WIDTH/2, SCREEN_HEIGHT)) # 绘制分数 draw_playscore() pygame.display.update() clock.tick(60) if opponent_score > player_score: print(f"The opponent won with {opponent_score - player_score} more points than you!") elif player_score > opponent_score: print(f"You Won With a {player_score - opponent_score} point difference! Congrats!") else: print("It's a tie!") print(f"You got {player_score} points!") print(f"The Opponent got {opponent_score} points!") pygame.quit()
内容的提问来源于stack exchange,提问作者BokoTriesToCode
相关产品推荐
相关产品推荐

