求助
import random as rlegal_x =
legal_y =
class Turtle:
def __init__(self):
self.power = 100
self.x = r.randint(leagl_x,legal_x)
self.y = r.randint(legal_y,legal_y)
def move(self):
new_x = self.x + r.chioce()
new_y = self.y + r.choice()
if new_x < legal_x:
self.x = legal_x-(new_x-legal_x)
elif new_x > legal_x:
self.x = legal_x - (new_x-legal_x)
else:
self.x = new_x
if new_y < legal_y:
self.y = legal_y - (new_y-legal_y)
elif new_y > legal_y:
self.y = legal_y - (new_y-legal_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 __init__(self):
self.x = r.randint(leagl_x,legal_x)
self.y = r.randint(legal_y,legal_y)
def move(self):
new_x =self.x + r.chioce()
new_y =self.y + r.choice()
if new_x < legal_x:
self.x = legal_x-(new_x-legal_x)
elif new_x > legal_x:
self.x = legal_x - (new_x-legal_x)
else:
self.x = new_x
if new_y < legal_y:
self.y = legal_y-(new_y-legal_y)
elif new_y > legal_y:
self.y = legal_y - (new_y-legal_y)
else:
self.y = new_y
return (self.x,self.y)
turtle = Turtle()
fish = []
for i in range(10):
new_fish = Fish()
fish.append(new_fish)
while True:
if not len(fish):
print('鱼儿被吃光了')
break
if not turtle.power:
print('小甲鱼体力用光啦')
break
pos = turtle.move()
for each_fish in fish[:]:
if each_fish.move == pos :
turtle.eat()
fish.remove(each_fish)
print('一条小鱼被吃掉了')
小甲鱼作业37 讲,这个为什么会报错啊
Traceback (most recent call last):
File "E:/python作业/4.py", line 53, in <module>
turtle = Turtle()
File "E:/python作业/4.py", line 9, in __init__
self.x = r.randint(leagl_x,legal_x)
NameError: name 'leagl_x' is not defined 拼写错误,legal,不是leagl 妈呀,看了好几遍都没看出来,真是当局者迷了呀 迟早要暴富 发表于 2021-10-28 21:37
妈呀,看了好几遍都没看出来,真是当局者迷了呀
问题解决了就赶紧设置最佳答案
页:
[1]