import random as r
import math
legal_x = [0, 30]
legal_y = [0, 30]
class Turtle:
#初始数据
def __init__(self):
#定义乌龟的初始位置
self.turtle_x = r.randint(legal_x[0], legal_x[1])
self.turtle_y = r.randint(legal_y[0], legal_y[1])
#定义初始血量
self.turtle_blood = 100
#初始能量
self.power = 10
#普通移动的方向和移动的距离
def move(self):
#随机选择是在 x轴方向移动还是在 y轴方向移动
if r.choice([0, 1]):
move_x = r.choice([1, 2, -1, -2])
self.turtle_x = self.turtle_x + move_x
#print(f'小甲鱼沿着 x 轴走了{move_x}格')
else:
move_y = r.choice([1, 2, -1, -2])
self.turtle_y = self.turtle_y + move_y
#print(f'小甲鱼沿着 y 轴走了{move_y}格')
#边界检测
if self.turtle_x < 0:
self.turtle_x = -self.turtle_x
if self.turtle_x > legal_x[1]:
self.turtle_x = 2*legal_x[1] - self.turtle_x
if self.turtle_y < 0:
self.turtle_y = -self.turtle_y
if self.turtle_y > legal_y[1]:
self.turtle_y = 2*legal_y[1] - self.turtle_y
#每移动一次,体力减 1
self.turtle_blood -= 1
self.power += 1
return self.turtle_x, self.turtle_y
def hungry_move(self):
#随机选择是在 x轴方向移动还是在 y轴方向移动
if r.choice([0, 1]):
move_x = r.choice([-3, 3])
self.turtle_x = self.turtle_x + move_x
#print(f'小甲鱼沿着 x 轴走了{move_x}格')
else:
move_y = r.choice([-3, 3])
self.turtle_y = self.turtle_y + move_y
#print(f'小甲鱼沿着 y 轴走了{move_y}格')
#边界检测
if self.turtle_x < 0:
self.turtle_x = -self.turtle_x
if self.turtle_x > legal_x[1]:
self.turtle_x = 2*legal_x[1] - self.turtle_x
if self.turtle_y < 0:
self.turtle_y = -self.turtle_y
if self.turtle_y > legal_y[1]:
self.turtle_y = 2*legal_y[1] - self.turtle_y
#每移动一次,体力减 1
self.turtle_blood -= 1
return self.turtle_x, self.turtle_y
#吃小鱼
def eat(self):
self.turtle_blood += 20
self.power = 10
#体力上限为 100
if self.turtle_blood > 100:
self.turtle_blood = 100
#小甲鱼的快速移动,智慧的小甲鱼已经学会拐弯了。
def quick_move(self, fish_x, fish_y):
#print('发现一条小鱼!冲冲冲!')
# 当 y坐标差距更大时,先调整 x坐标
if abs(self.turtle_x - fish_x) >= abs(self.turtle_y - fish_y):
if (self.turtle_x - fish_x) >= 3:
self.turtle_x -= 3
# 当小甲鱼和小鱼的 x轴的距离不足 3时,先对 x轴进行调整,则 y轴最小移动距离为 3-move >= 1
elif 0 < (self.turtle_x - fish_x) < 3: # x轴的移动距离可能为 1,2 甲鱼(7, 10) 鱼(13, 17)
move = self.turtle_x - fish_x
self.turtle_x -= move
if (self.turtle_y - fish_y) >= 2:
self.turtle_y -= 3 - move
elif (self.turtle_y - fish_y) == 1:
self.turtle_y -= 1
elif (self.turtle_y - fish_y) == 0:
pass
elif (self.turtle_y - fish_y) == -1:
self.turtle_y += 1
else:
self.turtle_y += 3 - move
elif -3 < (self.turtle_x - fish_x) < 0:
move = self.turtle_x - fish_x
self.turtle_x -= move
if (self.turtle_y - fish_y) >= 2:
self.turtle_y -= 3 - move
elif (self.turtle_y - fish_y) == 1:
self.turtle_y -= 1
elif (self.turtle_y - fish_y) == 0:
pass
elif (self.turtle_y - fish_y) == -1:
self.turtle_y += 1
else:
self.turtle_y += 3 - move
else:
self.turtle_x += 3
else:
if (self.turtle_y - fish_y) >= 3:
self.turtle_y -= 3
elif 0 < (self.turtle_y - fish_y) < 3: # x轴的移动距离可能为 1,2
move = self.turtle_y - fish_y
self.turtle_y -= move
if (self.turtle_x - fish_x) >= 2:
self.turtle_x -= 3 - move
elif (self.turtle_x - fish_x) == 1:
self.turtle_x -= 1
elif (self.turtle_x - fish_x) == 0:
pass
elif (self.turtle_x - fish_x) == -1:
self.turtle_x += 1
else:
self.turtle_x += 3 - move
elif -3 < (self.turtle_y - fish_y) < 0:
move = -(self.turtle_y - fish_y) #注意这个 move > 0
self.turtle_y += move
if (self.turtle_x - fish_x) >= 2:
self.turtle_x -= 3 - move
elif (self.turtle_x - fish_x) == 1:
self.turtle_x -= 1
elif (self.turtle_x - fish_x) == 0:
pass
elif (self.turtle_x - fish_x) == -1:
self.turtle_x += 1
else:
self.turtle_x += 3 - move
else:
self.turtle_y += 3
#每加速一个能量减 2
self.power -= 2
self.turtle_blood -= 1
return self.turtle_x, self.turtle_y
class Fish:
def __init__(self):
#定义初始位置
self.fish_x = r.randint(legal_x[0], legal_x[1])
self.fish_y = r.randint(legal_y[0], legal_y[1])
def move(self):
# 随机选择是在 x轴方向移动还是在 y轴方向移动
if r.choice([0, 1]):
move_x = r.choice([1, -1])
self.fish_x += move_x
#print(f'小鱼沿 x 轴走了{move_x}格')
else:
move_y = r.choice([1, -1])
self.fish_y = self.fish_y + move_y
#print(f'小鱼沿 y 轴走了{move_y}格')
# 边界检测
if self.fish_x < 0:
self.fish_x = -self.fish_x
if self.fish_x > legal_x[1]:
self.fish_x = 2*legal_x[1] - self.fish_x
if self.fish_y < 0:
self.fish_y = -self.fish_y
if self.fish_y > legal_x[1]:
self.fish_y = 2*legal_x[1] - self.fish_y
return self.fish_x, self.fish_y
# 发现小甲鱼,快跑快跑!
def escape(self, turtle_x, turtle_y):
#边界上的小鱼单独考虑
if self.fish_x == 0 or self.fish_x == legal_x[1]:
if self.fish_y > turtle_y:
self.fish_y += 1
elif self.fish_y < turtle_y:
self.fish_y -= 1
else:
move_y = r.choice([1, -1])
self.fish_y = self.fish_y + move_y
elif self.fish_y == 0 or self.fish_y == legal_y[1]:
if self.fish_x > turtle_x:
self.fish_x += 1
elif self.fish_x < turtle_x:
self.fish_x -= 1
else:
move_x = r.choice([1, -1])
self.fish_x = self.fish_x + move_x
#不在边界上的小鱼
else:
if abs(self.fish_x - turtle_x) < abs(self.fish_y - turtle_y):
if self.fish_y > turtle_y:
self.fish_y += 1
else:
self.fish_y -= 1
elif abs(self.fish_x - turtle_x) > abs(self.fish_y - turtle_y):
if self.fish_x > turtle_x:
self.fish_x += 1
else:
self.fish_x -= 1
else:
move = r.choice([1, -1])
if r.choice([0, 1]):
self.fish_x = self.fish_x + move
else:
self.fish_y = self.fish_y + move
# 边界检测
if self.fish_x < 0:
self.fish_x = -self.fish_x
if self.fish_x > legal_x[1]:
self.fish_x = 2 * legal_x[1] - self.fish_x
if self.fish_y < 0:
self.fish_y = -self.fish_y
if self.fish_y > legal_x[1]:
self.fish_y = 2 * legal_x[1] - self.fish_y
return self.fish_x, self.fish_y
#主程序
print('===============game start===============')
turtle = Turtle()
#定义 10条鱼
fish = {i : 10 for i in range(10)}
for i in range(10):
fish[i] = Fish()
#乌龟初始位置
loc_turtle = (turtle.turtle_x, turtle.turtle_y)
print('小甲鱼的初始位置', loc_turtle)
#小鱼的初始位置
loc = {i : (fish[i].fish_x, fish[i].fish_y) for i in fish.keys() }
l = []
for i in loc.keys():
l.append(i)
l.sort()
print(f'鱼的初始位置:{l[0]+1}号小鱼:坐标', loc[l[0]])
for i in range(1, len(l)):
print(f' {i+1}号小鱼:坐标 {loc[l[i]]}')
while True:
print('========================================')
#print('鱼' , fish.keys())
if len(fish) == 0:
print('小甲鱼:win~~~ ヾ(๑╹◡╹)ノ"')
break
if turtle.turtle_blood == 0:
print('小甲鱼:轻轻地我走了,正如我轻轻地来~~~ (╥╯^╰╥)')
break
# 记录下能发现小甲鱼的小鱼的条数
dist_fish = []
for every_loc in loc.keys():
every_dis = math.sqrt((loc[every_loc][0] - loc_turtle[0]) ** 2 + (loc[every_loc][1] - loc_turtle[1]) ** 2)
if every_dis <= 3:
dist_fish.append(every_loc)
#记录每一条小鱼每移动一次到达的位置
for every_fish in fish.keys():
if every_fish in dist_fish:
loc[every_fish] = fish[every_fish].escape(loc_turtle[0], loc_turtle[1])
print(f' 小鱼{every_fish}号:救命啊,我被发现了,快跑快跑!!! (。ŏ_ŏ)')
continue
loc[every_fish] = fish[every_fish].move()
# 好看地打印出每条小鱼的坐标
l = []
for i in loc.keys():
l.append(i)
l.sort()
print(f' 小鱼的位置:{l[0] + 1}号小鱼:坐标', loc[l[0]])
for i in range(1, len(l)):
print(f' {i + 1}号小鱼:坐标 {loc[l[i]]}')
# 记录在小甲鱼能看到的每一条小鱼到达的位置,和到小甲鱼的距离
dist = {}
for every_loc in loc.keys():
every_dis = math.sqrt((loc[every_loc][0] - loc_turtle[0]) ** 2 + (loc[every_loc][1] - loc_turtle[1]) ** 2)
if every_dis <= 10.0:
dist[loc[every_loc]] = every_dis
if len(dist) > 0: #在小甲鱼视野范围中的小鱼,启动快速模式
print(f' 小甲鱼:我发现了{len(dist)}条小鱼!', end='')
min_dist = min(dist.values())
for every_dist in dist.keys():
if dist[every_dist] == min_dist:
if turtle.power >= 2: #如果小甲鱼的能量大于 2,就可以使用超级无敌加速模式
loc_turtle = turtle.quick_move(every_dist[0], every_dist[1])
for z in loc.keys():
if loc[z] == every_dist:
print(f' 开启超级无敌加速模式!追击敌方{z}号小鱼')
break
break
else:
loc_turtle = turtle.move()
print(' 啊,没能量了,小鱼等等我! ヽ(#`Д´)ノ')
else: #没有小鱼在小甲鱼的视野中,保持普通模式
if turtle.turtle_blood >= 20:
loc_turtle = turtle.move()
print(' 小甲鱼:悠哉哉~~~ ( ̄▽ ̄)~*')
else: #饥饿中的小甲鱼,开始迫切地找食物!
loc_turtle = turtle.hungry_move()
print(' 小甲鱼:我得找吃的,好饿呀~~~ (ㄒoㄒ)')
print(' 小甲鱼的位置:坐标', loc_turtle)
loc_copy = loc.copy()
for fish_num in loc_copy.keys():
if loc[fish_num] == loc_turtle:
turtle.eat()
del fish[fish_num]
del loc[fish_num]
print(f' {fish_num}号小鱼:啊,我被吃掉了!')
if len(fish) == 0:
break
print(f' 小甲鱼:好吃好吃!还剩{len(fish)}条小鱼 (๑•؎ •๑)✧')