你的位置设置的有问题
我也没改出来,我要出去了,下次写个注释吧。
不然很难懂你的思路的import random
class Tortoise:
tortoise_coor = [random.randint(0, 10), random.randint(0, 10)]
hp = 100
def move(self):
orientation = random.randint(1, 4)
speed = random.randint(1, 2)
if orientation == 1:
self.tortoise_coor[1] += speed
elif orientation == 2:
self.tortoise_coor[0] += speed
elif orientation == 3:
self.tortoise_coor[1] -= speed
else:
self.tortoise_coor[0] -= speed
if self.tortoise_coor[0] < 0 or self.tortoise_coor[1] < 0:
self.tortoise_coor[0] = abs(self.tortoise_coor[0])
self.tortoise_coor[1] = self.tortoise_coor[1]
elif self.tortoise_coor[0] > 10:
self.tortoise_coor[0] = 20 - self.tortoise_coor[0]
elif self.tortoise_coor[1] > 10:
self.tortoise_coor[1] = 20 - self.tortoise_coor[1]
self.hp -= 1
return self.tortoise_coor
class Fish:
fish_coor = [random.randint(0, 10), random.randint(0, 10)]
sums = 9
def move(self):
orientation = random.randint(1, 4)
if orientation == 1:
self.fish_coor[1] += 1
elif orientation == 2:
self.fish_coor[0] += 1
elif orientation == 3:
self.fish_coor[1] -= 1
else:
self.fish_coor[0] -= 1
if self.fish_coor[0] < 0 or self.fish_coor[1] < 0:
self.fish_coor[0] = abs(self.fish_coor[0])
elif self.fish_coor[0] > 10:
self.fish_coor[0] = 20 - self.fish_coor[0]
elif self.fish_coor[1] > 10:
self.fish_coor[1] = 20 - self.fish_coor[1]
return self.fish_coor
while True:
if Tortoise.hp < 0:
print('乌龟的体力耗尽了,游戏结束!')
break
if Fish.sums < 0:
print('小鱼被吃光了,游戏结束!')
break
tortoise = Tortoise()
fish = Fish()
if tortoise.move() == fish.move():
Fish.sums -= 1
print('小鱼和龟相遇了, 小鱼被吃掉一只')
print('龟龟坐标:',tortoise.move())
print('鱼儿坐标:',fish.move())
fish_coor = [random.randint(0, 10), random.randint(0, 10)]
Tortoise.hp += 20
if Tortoise.hp > 100:
Tortoise.hp = 100
=
小鱼和龟相遇了, 小鱼被吃掉一只
龟龟坐标: [10, 5]
鱼儿坐标: [8, 5]
小鱼和龟相遇了, 小鱼被吃掉一只
龟龟坐标: [1, -11]
鱼儿坐标: [1, -13]
小鱼和龟相遇了, 小鱼被吃掉一只
龟龟坐标: [2, -8]
鱼儿坐标: [1, -9]
小鱼和龟相遇了, 小鱼被吃掉一只
龟龟坐标: [34, -167]
鱼儿坐标: [35, -167]
小鱼和龟相遇了, 小鱼被吃掉一只
龟龟坐标: [221, -425]
鱼儿坐标: [222, -425]
小鱼和龟相遇了, 小鱼被吃掉一只
龟龟坐标: [23, -536]
鱼儿坐标: [24, -535]
小鱼和龟相遇了, 小鱼被吃掉一只
龟龟坐标: [26, -536]
鱼儿坐标: [23, -536]
小鱼和龟相遇了, 小鱼被吃掉一只
龟龟坐标: [475, -826]
鱼儿坐标: [474, -825]
小鱼和龟相遇了, 小鱼被吃掉一只
龟龟坐标: [475, -824]
鱼儿坐标: [475, -826]
小鱼和龟相遇了, 小鱼被吃掉一只
龟龟坐标: [483, -832]
鱼儿坐标: [480, -832]
小鱼被吃光了,游戏结束!
>>>
|