|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
萌新求助,第37课课后作业,乌龟吃鱼小游戏
这个体力显示无法调用,不知道怎么改,谢谢大家啦
------------------------------------------------------------------------------------
报错内容:
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\试试.py", line 65, in <module>
if tt.power ==0:
AttributeError: 'Turtle' object has no attribute 'power'
------------------------------------------------------------------------------------
我的代码:
import random as r
class Turtle:
def __int__(self):
self.power = 100
self.x = r.randint(0,10)
self.y = r.randint(0,10)
def move(self):
new_x = self.x + r.choice([1,-1,2,-2])
new_y = self.y + r.choice([1,-1,2,-2])
if new_x < 0:
self_x = - new_x
elif new_x > 10:
self_x = 20 -new_x
else:
self.x = new_x
if new_y < 0:
self_y = - new_y
elif new_y > 10:
self_y = 20 -new_y
else:
self.y = new_y
self.power -= 1
return (self.x , self.y)
def eat(self):
self.power += 20
if self.power > 100:
self.power = 100
class Fish:
def __int__(self):
self.x = r.randint(0,10)
self.y = r.randint(0,10)
def move(self):
new_x = self.x + r.choice([1,-1])
new_y = self.y + r.choice([1,-1])
if new_x < 0:
self_x = - new_x
elif new_x > 10:
self_x = 20 -new_x
else:
self.x = new_x
if new_y < 0:
self_y = - new_y
elif new_y > 10:
self_y = 20 -new_y
else:
self.y = new_y
return (self.x , self.y)
tt = Turtle()
fish = []
for i in range(10):
new_fish = Fish()
fish.append(new_fish)
while True:
if len(fish) == 0:
print('鱼被吃完啦!')
break
if tt.power ==0:
print('体力没有啦!')
break
pos = tt.move()
for each_fish in fish:
if each_fish.move == pos:
tt.eat()
fish.remove(each_fish)
print('有一条鱼被吃掉了')
def __int__(self):
改为
def __init__(self):
|
|