我正在尝试运行pygame程序,但继续获取AttributeError。这是错误消息;错误发生在Snake类的第30行:
Traceback (most recent call last):
File "\\LPSD-FILE03\Students$\LCHS\arron.mackenzie\SYSTEM\My Documents\Final Project\Final Snake
Game.py", line 119, in <module>
if(snake.move(food_position)==1):
File "\\LPSD-FILE03\Students$\LCHS\arron.mackenzie\SYSTEM\My Documents\Final Project\Final Snake
Game.py", line 30, in move
self.position[0] += 10
AttributeError: 'Snake' object has no attribute 'position'
这是使用的代码,如果我是pygame的新手,请提供详细的答案
class Snake(object):
def __init__(self):
self.positiion = [100,50] #sets initial position of the snake
self.body = [[100,50], [90,50], [80,50]] #sets initial length of the snakes body to 3 segments
self.direction = "RIGHT"
self.change_direction_to = self.direction
#---------------------------- Makes it posible to change directions but you canot go directly bacwards
def change_direction_to(self,dir):
if dir=="RIGHT" and not self.direction == "LEFT":
self.direction = "RIGHT"
elif dir=="LEFT" and not self.direction == "RIGHT":
self.direction = "LEFT"
elif dir=="UP" and not self.direction == "DOWN":
self.direction = "UP"
elif dir=="DOWN" and not self.direction == "UP":
self.direction = "DOWN"
将“位置”更改为“位置”:)