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

Snake游戏报错:'str' object is not callable 问题求助

问题分析与解决

嘿,我来帮你搞定这个报错!你的TypeError: 'str' object is not callable问题根源其实很直白——你在Snake类的初始化方法里,把类的方法给覆盖成字符串了。

先看这段关键代码:

class Snake():
    def __init__(self):
        self.x=400
        self.y=590
        self.direction = "RIGHT"
        self.changeDirectionTo = self.direction  # 这行就是错误的源头!

你在这里把原本是方法的changeDirectionTo,直接赋值成了字符串"RIGHT",完全覆盖了后面定义的同名方法。所以当你按下方向键调用snake.changeDirectionTo(3)时,本质是在尝试调用一个字符串,这肯定会触发类型错误。


具体修复步骤

1. 修复Snake类的方法覆盖问题

删掉__init__里的self.changeDirectionTo = self.direction这行代码就行。如果是想记录初始方向,换个变量名(比如self.lastDirection),别和方法名冲突。修正后的Snake类:

class Snake():
    def __init__(self):
        self.x=400
        self.y=590
        self.direction = "RIGHT"
        # 删掉那行错误赋值,或者改成其他变量名(可选):
        # self.lastDirection = self.direction

    def changeDirectionTo(self,dir):
        if dir == 1 and not self.direction == 2:
            self.direction = "RIGHT"
        if dir == 2 and not self.direction == 1:
            self.direction = "LEFT"
        if dir == 3 and not self.direction == 4:
            self.direction = "UP"
        if dir == 4 and not self.direction == 3:
            self.direction = "DOWN"

    def move(self, foodPos):
        if self.direction == "RIGHT":
            self.x += 100
        if self.direction == "LEFT":
            self.x -= 100
        if self.direction == "UP":
            self.y -= 100
        if self.direction == "DOWN":
            self.y += 100

2. 修正其他潜在小问题

除了核心错误,你的代码还有几个小细节会导致后续运行异常,一起改了:

  • 类名定义是Appli,但最后实例化写的是theApp = App(),统一改成App类;
  • on_event里的QUIT要改成pygame.QUIT,否则会报未定义错误;
  • on_render里的self._display_surf是笔误,应该是self._show_surf(和on_init里初始化的变量一致);
  • on_execute里不需要重新实例化snake,直接用self.snake即可,且on_cleanup要放在while循环外面,不然游戏刚启动就退出。

修正后的App类:

class App:
    windowX = 800
    windowY = 600
    def __init__(self):
        self._running = True
        self._show_surf = None
        self._image_surf = None
        self.snake = Snake()

    def on_init(self):
        pygame.init()
        self._show_surf = pygame.display.set_mode((self.windowX,self.windowY), pygame.HWSURFACE)
        pygame.display.set_caption('Snake V2.7')
        self._running = True
        self._image_surf = pygame.image.load("pygame.png").convert()

    def on_event(self, event):
        if event.type == pygame.QUIT:
            self._running = False

    def on_loop(self):
        pass

    def on_render(self):
        self._show_surf.fill((0,0,0))
        self._show_surf.blit(self._image_surf,(self.snake.x,self.snake.y))
        pygame.display.flip()

    def on_cleanup(self):
        pygame.quit()

    def on_execute(self):
        if self.on_init() == False:
            self._running = False
        while( self._running ):
            pygame.event.pump()
            action = pygame.key.get_pressed()
            if action[pygame.K_RIGHT]:
                self.snake.changeDirectionTo(1)
            if action[pygame.K_LEFT]:
                self.snake.changeDirectionTo(2)
            if action[pygame.K_UP]:
                self.snake.changeDirectionTo(3)
            if action[pygame.K_DOWN]:
                self.snake.changeDirectionTo(4)
            self.on_loop()
            self.on_render()
        self.on_cleanup()

总结

核心问题就是方法名被字符串意外覆盖,删掉那行错误赋值就能解决报错,再修正几个小细节,你的蛇游戏就能正常响应方向键了!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:24:01