You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pygame游戏分数无法显示问题求助(附完整代码)

问题排查与解决方案

核心问题

你的分数无法正常显示是因为分数文本被后续绘制的游戏元素(草地、车道线等)覆盖。当前代码中你在屏幕填充黑色后先调用了show_score,但之后绘制的草地、车道等元素会把分数区域盖住;虽然后续又调用了一次show_score,但逻辑上更合理的做法是将分数绘制放在所有元素的最后,确保它处于视觉层级最上层。

修复步骤

  1. 调整show_score调用位置:移除循环前半部分的show_score(textX, textY),只保留最后一次调用,且确保它在所有绘图代码之后、pygame.display.update()之前。
  2. 优化分数增长逻辑:原代码每帧(每秒60次)让Score_value +=1,分数增长过快,建议改为每成功躲避一辆车再加分,更符合游戏逻辑。
  3. 修复遍历删除bug:遍历cars列表时直接删除元素会引发运行错误,改为遍历列表副本避免该问题。

修复后的完整代码

import pygame
import random
from pygame.locals import (
    K_UP,
    K_DOWN,
    KEYDOWN,
    QUIT,
)

pygame.init()

LANE_WIDTH = 150
NUM_LANES = 3

Score_value = 0
textX = 10
textY = 10

CAR_WIDTH = 160
CAR_HEIGHT = 100
CAR_SPEED = 5

SCREEN_WIDTH = 1920
SCREEN_HEIGHT = 1080

lane_positions = [(SCREEN_HEIGHT - NUM_LANES * LANE_WIDTH) // 2 + x * LANE_WIDTH for x in range(NUM_LANES)]
middle_line_positions = [lane + LANE_WIDTH // 2 for lane in lane_positions]

player = pygame.Rect(600, (SCREEN_HEIGHT - CAR_HEIGHT) // 2, 35, 50)

wall = pygame.Rect(-100, 0, 100, SCREEN_HEIGHT)
cars = []

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

font = pygame.font.Font('freesansbold.ttf', 32)

run = True

clock = pygame.time.Clock()

def show_score(textX, textY):
    score_text = font.render("Score: " + str(Score_value), True, (255, 255, 255))
    screen.blit(score_text, (textX, textY))

def spawn_car():
    lane_index = random.randint(0, NUM_LANES - 1)
    lane_y = lane_positions[lane_index]
    new_car = pygame.Rect(SCREEN_WIDTH, lane_y + (LANE_WIDTH - CAR_HEIGHT) // 2, CAR_WIDTH, CAR_HEIGHT)
    for car in cars:
        if car.colliderect(new_car):
            return  # Don't spawn if there's a collision
    cars.append(new_car)
    return

while run:
    clock.tick(60)  # Limit the frame rate to 60 FPS

    for event in pygame.event.get():
        if event.type == QUIT:
            run = False

    screen.fill((0,0,0))

    # Draw the lanes
    for lane_y, middle_line_y in zip(lane_positions, middle_line_positions):
        pygame.draw.line(screen, (255, 255, 255), (0, lane_y), (SCREEN_WIDTH, lane_y), 4)

    # Draw white line for the bottom lane
    bottom_lane_y = lane_positions[NUM_LANES - 1]
    pygame.draw.line(screen, (255, 255, 255), (0, bottom_lane_y + LANE_WIDTH), (SCREEN_WIDTH, bottom_lane_y + LANE_WIDTH), 4)

    pygame.draw.rect(screen, (0, 255, 0), player)

    # Draw grass
    pygame.draw.rect(screen, (0, 128, 0), pygame.Rect(0, 0, SCREEN_WIDTH, lane_positions[0]))
    pygame.draw.rect(screen, (0, 128, 0), pygame.Rect(0, lane_positions[NUM_LANES - 1] + LANE_WIDTH, SCREEN_WIDTH, SCREEN_HEIGHT))

    # Move and draw each car
    for car in cars[:]:  # 遍历列表副本,避免删除元素导致的遍历错误
        car.move_ip(-CAR_SPEED, 0)
        pygame.draw.rect(screen, (255, 0, 0), car)

        # Check if the car collides with the player
        if car.colliderect(player):
            run = False

        # 车辆超出屏幕时加分并移除
        if car.colliderect(wall):
            cars.remove(car)
            Score_value += 10  # 每躲避一辆车加10分

    # Spawn a new car randomly
    if random.random() < 0.02:
        spawn_car()

    key = pygame.key.get_pressed()
    if key[K_UP]:
        player.move_ip(0, -5)
    elif key[K_DOWN]:
        player.move_ip(0, 5)

    # 最后绘制分数,确保不被其他元素覆盖
    show_score(textX, textY)
    pygame.display.update()

pygame.quit()

内容的提问来源于stack exchange,提问作者emilianhaugland

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 00:55:28