AttributeError: 'Snake' object has no attribute 'body'
我实在找不到两个代码之间到底哪里不同,求助各位大佬报错信息:
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "D:\PyCharm Community Edition 2020.2\plugins\python-ce\helpers\pydev\pydevd.py", line 1448, in _exec
pydev_imports.execfile(file, globals, locals)# execute the script
File "D:\PyCharm Community Edition 2020.2\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "D:/Python/snake/snake/snake.py", line 162, in <module>
main()
File "D:/Python/snake/snake/snake.py", line 133, in main
snake.move()
File "D:/Python/snake/snake/snake.py", line 54, in move
self.addnode()
File "D:/Python/snake/snake/snake.py", line 23, in addnode
if self.body:
AttributeError: 'Snake' object has no attribute 'body'
报错代码:
import pygame
import sys
import random
# 全局定义
SCREEN_X = 600
SCREEN_Y = 600
# 蛇类
# 点以25为单位
class Snake(object):
# 初始化各种需要的属性[开始时默认向右,身体块x5]
def __int__(self):
self.dirction = pygame.K_RIGHT
self.body = []
for x in range(5):
self.addnode()
# 无论何时 都在前端增加蛇块
def addnode(self):
left, top = (0, 0)
if self.body:
left, top = (self.body.left, self.body.top)
node = pygame.Rect(left, top, 25, 25)
if self.dirction == pygame.K_LEFT:
node.left -= 25
elif self.dirction == pygame.K_RIGHT:
node.left += 25
elif self.dirction == pygame.K_UP:
node.top -= 25
elif self.dirction == pygame.K_DOWN:
node.top += 25
self.body.insert(0, node)
# 删除最后一个方块
def delonde(self):
self.body.pop()
# 死亡判断
def isdead(self):
# 撞墙
if self.body.x not in range(SCREEN_X):
return True
if self.body.y not in range(SCREEN_Y):
return True
# 撞自己
if self.body in self.body:
return True
return False
# 移动
def move(self):
self.addnode()
self.delonde()
# 改变方向,但左右上下不能被逆向改变
def changedirection(self, curkey):
LR =
UD =
if curkey in LR + UD:
if (curkey in LR) and (self.dirction in LR):
return
if (curkey in UD) and (self.dirction in UD):
return
self.dirction = curkey
# 食物类
# 方法:放置/移除
# 点以25为单位
class Food:
def __int__(self):
self.rect = pygame.Rect(-25, 0, 25, 25)
def remove(self):
self.rect.x = -25
def set(self):
if self.rect.x == -25:
allpos = []
# 不要靠墙太近 25~SCREEN_X-25 之间
for pos in range(25, SCREEN_X - 25, 25):
allpos.append(pos)
self.rect.left = random.choice(allpos)
self.rect.top = random.choice(allpos)
print(self.rect)
def show_text(screen, pos, text, color, font_bold=False, font_size=60, font_italic=False):
# 获取系统字体,并设置文字大小
cur_font = pygame.font.SysFont("宋体", font_size)
# 设置是否加粗属性
cur_font.set_bold(font_bold)
# 设置是否斜体属性
cur_font.set_italic(font_italic)
# 设置文字内容
text_fmt = cur_font.render(text, 1, color)
# 绘制文字
screen.blit(text_fmt, pos)
def main():
pygame.init()
screen_size = (SCREEN_X, SCREEN_Y)
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption('Snake')
clock = pygame.time.Clock()
scores = 0
isdead = False
# 蛇/食物
snake = Snake()
food = Food()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
snake.changedirection(event.key)
# 死后按space重开
if event.key == pygame.K_SPACE and isdead:
return main()
screen.fill((255, 255, 255))
# 画蛇身 / 每一步+1分
if not isdead:
scores += 1
snake.move()
for rect in snake.body:
pygame.draw.rect(screen, (20, 200, 39), rect, 0)
# 显示死亡汉字
isdead = snake.isdead()
if isdead:
show_text(screen, (100, 200), 'YOU DEAD!', (227, 29, 18), False, 100)
show_text(screen, (150, 260), 'press space to try again...', (0, 0, 22), False, 30)
# 食物处理 / 吃到加50分
# 当食物rect与蛇头重合,吃掉-> Snake增加一个Node
if food.rect == snake.body:
scores += 50
food.remove()
snake.addnode()
# 食物投递
food.set()
pygame.draw.rect(screen, (136, 0, 21), food.rect, 0)
# 显示分数文字
show_text(screen, (50, 500), 'Scores: ' + str(scores), (223, 223, 223))
pygame.display.update()
clock.tick(10)
if __name__ == '__main__':
main()
-----------------------------------------------------------分--------------------割---------------------线---------------------------------------------------------------------------
可以正常运行的代码:
import pygame
import sys
import random
# 全局定义
SCREEN_X = 600
SCREEN_Y = 600
# 蛇类
# 点以25为单位
class Snake(object):
# 初始化各种需要的属性 [开始时默认向右/身体块x5]
def __init__(self):
self.dirction = pygame.K_RIGHT
self.body = []
for x in range(5):
self.addnode()
# 无论何时 都在前端增加蛇块
def addnode(self):
left,top = (0,0)
if self.body:
left,top = (self.body.left,self.body.top)
node = pygame.Rect(left,top,25,25)
if self.dirction == pygame.K_LEFT:
node.left -= 25
elif self.dirction == pygame.K_RIGHT:
node.left += 25
elif self.dirction == pygame.K_UP:
node.top -= 25
elif self.dirction == pygame.K_DOWN:
node.top += 25
self.body.insert(0,node)
# 删除最后一个块
def delnode(self):
self.body.pop()
# 死亡判断
def isdead(self):
# 撞墙
if self.body.xnot in range(SCREEN_X):
return True
if self.body.ynot in range(SCREEN_Y):
return True
# 撞自己
if self.body in self.body:
return True
return False
# 移动!
def move(self):
self.addnode()
self.delnode()
# 改变方向 但是左右、上下不能被逆向改变
def changedirection(self,curkey):
LR =
UD =
if curkey in LR+UD:
if (curkey in LR) and (self.dirction in LR):
return
if (curkey in UD) and (self.dirction in UD):
return
self.dirction = curkey
# 食物类
# 方法: 放置/移除
# 点以25为单位
class Food:
def __init__(self):
self.rect = pygame.Rect(-25,0,25,25)
def remove(self):
self.rect.x=-25
def set(self):
if self.rect.x == -25:
allpos = []
# 不靠墙太近 25 ~ SCREEN_X-25 之间
for pos in range(25,SCREEN_X-25,25):
allpos.append(pos)
self.rect.left = random.choice(allpos)
self.rect.top= random.choice(allpos)
print(self.rect)
def show_text(screen, pos, text, color, font_bold = False, font_size = 60, font_italic = False):
#获取系统字体,并设置文字大小
cur_font = pygame.font.SysFont("宋体", font_size)
#设置是否加粗属性
cur_font.set_bold(font_bold)
#设置是否斜体属性
cur_font.set_italic(font_italic)
#设置文字内容
text_fmt = cur_font.render(text, 1, color)
#绘制文字
screen.blit(text_fmt, pos)
def main():
pygame.init()
screen_size = (SCREEN_X,SCREEN_Y)
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption('Snake')
clock = pygame.time.Clock()
scores = 0
isdead = False
# 蛇/食物
snake = Snake()
food = Food()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
snake.changedirection(event.key)
# 死后按space重新
if event.key == pygame.K_SPACE and isdead:
return main()
screen.fill((255,255,255))
# 画蛇身 / 每一步+1分
if not isdead:
scores+=1
snake.move()
for rect in snake.body:
pygame.draw.rect(screen,(20,220,39),rect,0)
# 显示死亡文字
isdead = snake.isdead()
if isdead:
show_text(screen,(100,200),'YOU DEAD!',(227,29,18),False,100)
show_text(screen,(150,260),'press space to try again...',(0,0,22),False,30)
# 食物处理 / 吃到+50分
# 当食物rect与蛇头重合,吃掉 -> Snake增加一个Node
if food.rect == snake.body:
scores+=50
food.remove()
snake.addnode()
# 食物投递
food.set()
pygame.draw.rect(screen,(136,0,21),food.rect,0)
# 显示分数文字
show_text(screen,(50,500),'Scores: '+str(scores),(223,223,223))
pygame.display.update()
clock.tick(10)
if __name__ == '__main__':
main() def __int__(self):
这里应该写成 __init__ import pygame
import sys
import random
# 全局定义
SCREEN_X = 600
SCREEN_Y = 600
# 蛇类
# 点以25为单位
class Snake(object):
# 初始化各种需要的属性[开始时默认向右,身体块x5]
def __int__(self):
self.dirction = pygame.K_RIGHT
self.body = []
for x in range(5):
self.addnode()
# 无论何时 都在前端增加蛇块
def addnode(self):
left, top = (0, 0)
if self.body:
left, top = (self.body.left, self.body.top)
node = pygame.Rect(left, top, 25, 25)
if self.dirction == pygame.K_LEFT:
node.left -= 25
elif self.dirction == pygame.K_RIGHT:
node.left += 25
elif self.dirction == pygame.K_UP:
node.top -= 25
elif self.dirction == pygame.K_DOWN:
node.top += 25
self.body.insert(0, node)
# 删除最后一个方块
def delonde(self):
self.body.pop()
# 死亡判断
def isdead(self):
# 撞墙
if self.body.x not in range(SCREEN_X):
return True
if self.body.y not in range(SCREEN_Y):
return True
# 撞自己
if self.body in self.body:
return True
return False
# 移动
def move(self):
self.addnode()
self.delonde()
# 改变方向,但左右上下不能被逆向改变
def changedirection(self, curkey):
LR =
UD =
if curkey in LR + UD:
if (curkey in LR) and (self.dirction in LR):
return
if (curkey in UD) and (self.dirction in UD):
return
self.dirction = curkey
# 食物类
# 方法:放置/移除
# 点以25为单位
class Food:
def __int__(self):
self.rect = pygame.Rect(-25, 0, 25, 25)
def remove(self):
self.rect.x = -25
def set(self):
if self.rect.x == -25:
allpos = []
# 不要靠墙太近 25~SCREEN_X-25 之间
for pos in range(25, SCREEN_X - 25, 25):
allpos.append(pos)
self.rect.left = random.choice(allpos)
self.rect.top = random.choice(allpos)
print(self.rect)
def show_text(screen, pos, text, color, font_bold=False, font_size=60, font_italic=False):
# 获取系统字体,并设置文字大小
cur_font = pygame.font.SysFont("宋体", font_size)
# 设置是否加粗属性
cur_font.set_bold(font_bold)
# 设置是否斜体属性
cur_font.set_italic(font_italic)
# 设置文字内容
text_fmt = cur_font.render(text, 1, color)
# 绘制文字
screen.blit(text_fmt, pos)
def main():
pygame.init()
screen_size = (SCREEN_X, SCREEN_Y)
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption('Snake')
clock = pygame.time.Clock()
scores = 0
isdead = False
# 蛇/食物
snake = Snake()
food = Food()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
snake.changedirection(event.key)
# 死后按space重开
if event.key == pygame.K_SPACE and isdead:
return main()
screen.fill((255, 255, 255))
# 画蛇身 / 每一步+1分
if not isdead:
scores += 1
snake.move()
for rect in snake.body:
pygame.draw.rect(screen, (20, 200, 39), rect, 0)
# 显示死亡汉字
isdead = snake.isdead()
if isdead:
show_text(screen, (100, 200), 'YOU DEAD!', (227, 29, 18), False, 100)
show_text(screen, (150, 260), 'press space to try again...', (0, 0, 22), False, 30)
# 食物处理 / 吃到加50分
# 当食物rect与蛇头重合,吃掉-> Snake增加一个Node
if food.rect == snake.body:
scores += 50
food.remove()
snake.addnode()
# 食物投递
food.set()
pygame.draw.rect(screen, (136, 0, 21), food.rect, 0)
# 显示分数文字
show_text(screen, (50, 500), 'Scores: ' + str(scores), (223, 223, 223))
pygame.display.update()
clock.tick(10)
if __name__ == '__main__':
main() zltzlt 发表于 2020-8-17 17:36
这里应该写成 __init__
这个应该没错的
李青山 发表于 2020-8-17 17:50
这个应该没错的
debug时24行提示错误 李青山 发表于 2020-8-17 17:50
这个应该没错的
你觉得拼错个单词会没错? 李青山 发表于 2020-8-17 17:50
这个应该没错的
找个代码对比工具对照一下也能发现是那里拼错呀 __init__拼错了,造成的影响就会非常大。__init__方法是在类被实例化时调用的,而你拼成了__int__,python就会认为这是另一个方法,在实例化时就不会调用__int__方法,self.body也就不会被定义 李青山 发表于 2020-8-17 17:50
这个应该没错的
__init__和__int__是完全不同的方法好吗?你去查一下__init__是什么 鱼cpython学习者 发表于 2020-8-17 18:33
__init__和__int__是完全不同的方法好吗?你去查一下__init__是什么
貌似没有__int__这个方法 陈尚涵 发表于 2020-8-17 18:43
貌似没有__int__这个方法
的确没有,我想说完全不同来着 确实是__init__这个拼错了,刚刚用代码工具对比了一下,发现是一个缩进导致的问题,疏忽了,感谢各位的回答
页:
[1]