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

Pygame吃豆人游戏:分数无法递增超1且不显示问题求助

Pygame吃豆人游戏分数更新问题修复

问题

用Pygame制作简化版吃豆人(吃水果玩法)时遇到两个问题:

  • 在类中修改分数后,游戏窗口无法显示更新后的分数
  • 分数仅能递增到1后停止变化,主游戏循环中修改分数正常,但碰撞检测类里处理吃水果加1分时始终卡在1

原代码

class player(object):
    def randomize(self,win,score):
        self.fx = random.randint(0,1250)
        self.fy = random.randint(0,550)
        score += 1
        print("score is",score)
    def check_collisions(score,win):
        pacdraw = pygame.Rect(pac.x+10, pac.y+10, 75, 75)
        pygame.draw.rect(win,(100,100,100),pacdraw,-1)
        fruit = pygame.Rect(pac.fx+10,pac.fy+10, 40, 30)
        pygame.draw.rect(win,(100,100,100),fruit,-1)
        collide = pacdraw.colliderect(fruit)
        if collide:
            pac.randomize(win,score)

# score = 0
scorefont = pygame.font.Font("freesansbold.ttf", 32)
def display_score(score,win):
    display = scorefont.render(f'score: {score}', True, (225, 225, 225))
    win.blit(display, (10, 10))
def Redrawgamewindow():
    win.blit(bg, (-300,-200))
    win.blit(cherry, (pac.fx, pac.fy))
    check_collisions(score,win)
    display_score(score,win)
    pac.draw(win)

问题根源与修复方案

1. 整数的不可变性导致分数无法同步

Python中整数是不可变类型,randomize方法里的score += 1只是创建了一个新的局部整数,完全不会影响全局的score变量。就算把全局score传给方法,修改的也只是方法内部的副本,外部的score根本不会变。

2. 类方法定义错误

check_collisions作为player类的方法,没有定义self参数,会导致调用时抛出参数不匹配的错误;同时方法里直接引用全局的pac变量,破坏了类的封装性。

修复后的完整代码

import pygame
import random

# 初始化Pygame
pygame.init()
win = pygame.display.set_mode((1280, 600))
bg = pygame.Surface((1280, 600))
bg.fill((0, 0, 0))
cherry = pygame.Surface((60, 50))
cherry.fill((255, 0, 0))

class Player(object):
    def __init__(self):
        self.x = 600
        self.y = 300
        # 初始化水果位置
        self.fx = random.randint(0, 1250)
        self.fy = random.randint(0, 550)
    
    # 只负责刷新水果位置,不处理分数
    def randomize_fruit(self):
        self.fx = random.randint(0, 1250)
        self.fy = random.randint(0, 550)
    
    # 带self的实例方法,返回碰撞结果
    def check_collision(self):
        pac_rect = pygame.Rect(self.x + 10, self.y + 10, 75, 75)
        fruit_rect = pygame.Rect(self.fx + 10, self.fy + 10, 40, 30)
        # 调试用的碰撞框可保留,正式发布可注释
        # pygame.draw.rect(win, (100, 100, 100), pac_rect, -1)
        # pygame.draw.rect(win, (100, 100, 100), fruit_rect, -1)
        return pac_rect.colliderect(fruit_rect)
    
    def draw(self, win):
        # 绘制吃豆人(示例图形,可替换成自定义资源)
        pygame.draw.circle(win, (255, 255, 0), (self.x + 47, self.y + 47), 37)

# 全局分数变量
score = 0
score_font = pygame.font.Font("freesansbold.ttf", 32)

def display_score(score, win):
    score_surface = score_font.render(f'score: {score}', True, (225, 225, 225))
    win.blit(score_surface, (10, 10))

def redraw_game_window(win, player):
    global score
    win.blit(bg, (-300, -200))
    win.blit(cherry, (player.fx, player.fy))
    
    # 检测碰撞,更新分数和水果位置
    if player.check_collision():
        player.randomize_fruit()
        score += 1
        print("score is", score)
    
    display_score(score, win)
    player.draw(win)
    pygame.display.update()

# 创建玩家实例
pac = Player()
run = True

# 主游戏循环
while run:
    pygame.time.delay(50)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    
    # 控制吃豆人移动
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT] and pac.x > 0:
        pac.x -= 10
    if keys[pygame.K_RIGHT] and pac.x < 1280 - 95:
        pac.x += 10
    if keys[pygame.K_UP] and pac.y > 0:
        pac.y -= 10
    if keys[pygame.K_DOWN] and pac.y < 600 - 95:
        pac.y += 10
    
    redraw_game_window(win, pac)

pygame.quit()

核心修复点

  • 将分数更新逻辑移到全局作用域的渲染函数中,直接操作全局score变量,确保修改能同步到显示
  • 重构Player类的方法:check_collision改为带self的实例方法,返回碰撞结果;randomize_fruit只负责刷新水果位置,不处理分数
  • 修复类方法的参数错误,确保所有实例方法都包含self参数,避免依赖全局变量

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 07:25:32