我在这里四处寻找这个问题的解决方案但没有用。也许你可以帮忙,我看不出错误。
这是追溯:
File "copy.py", line 171, in <module>
main()
File "copy.py", line 128, in main
game.draw(screen)
File "copy.py", line 59, in draw
snake.draw(screen)
TypeError: draw() missing 1 required positional argument: 'screen'
追溯中引用的代码位于
之下def main():
pygame.init()
winWidth, winHeight = 800, 800
screen = pygame.display.set_mode([winWidth, winHeight])
pygame.display.set_caption("Exploring mouse, keys, and event loops with Pygame")
running = 1
bgcolor = (0,0,0)
screen.fill(bgcolor)
game = SnaketheGame(winWidth, winHeight)
game.draw(screen)
##########
class SnaketheGame:
def __init__(self, winWidth, winHeight):
self.snake = snake
self.snakeMoving = False
self.growingSnake = False
self.food = food
def draw(self, screen):
self.snake.draw(screen)
for food in self.food:
food.draw(screen)
我查看了其他答案并确保我先调用了该类的实例。我无法弄清楚我在这里做错了什么。请帮忙!
答案 0 :(得分:0)
如果我正确地阅读此内容,我相信当您致电game = SnaketehGame(winWidth, winHeight)
时,您实际上并未初始化游戏。由于您尚未定义snake
和food
,因此可能无法正确初始化,因此当您尝试拨打game.draw
时,您实际上并没有还有游戏,因此screen
输入正在用来代替self
。这就是为什么你得到的错误是你没有screen
的位置参数:screen
被用作self
。
要解决此问题,请修复初始化以定义snake
和food
。