学了两个月,来上交我的第一份超级超级长的作业
给上次作业中的小甲鱼吃小鱼的游戏加了点内容:给小甲鱼加了一个视野范围,在视野范围里面的小鱼,小甲鱼会加速去抓他;
给小鱼加了一个逃跑的方法,在视野范围里,看到小甲鱼的时候,会向反方向跑;
又加了一点文字互动{:10_279:}
但是还没学游戏模块,就先用文字表示了。{:10_254:}
import random as r
import math
legal_x =
legal_y =
class Turtle:
#初始数据
def __init__(self):
#定义乌龟的初始位置
self.turtle_x = r.randint(legal_x, legal_x)
self.turtle_y = r.randint(legal_y, legal_y)
#定义初始血量
self.turtle_blood = 100
#初始能量
self.power = 10
#普通移动的方向和移动的距离
def move(self):
#随机选择是在 x轴方向移动还是在 y轴方向移动
if r.choice():
move_x = r.choice()
self.turtle_x = self.turtle_x + move_x
#print(f'小甲鱼沿着 x 轴走了{move_x}格')
else:
move_y = r.choice()
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:
self.turtle_x = 2*legal_x - self.turtle_x
if self.turtle_y < 0:
self.turtle_y = -self.turtle_y
if self.turtle_y > legal_y:
self.turtle_y = 2*legal_y - 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():
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:
self.turtle_x = 2*legal_x - self.turtle_x
if self.turtle_y < 0:
self.turtle_y = -self.turtle_y
if self.turtle_y > legal_y:
self.turtle_y = 2*legal_y - 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, legal_x)
self.fish_y = r.randint(legal_y, legal_y)
def move(self):
# 随机选择是在 x轴方向移动还是在 y轴方向移动
if r.choice():
move_x = r.choice()
self.fish_x += move_x
#print(f'小鱼沿 x 轴走了{move_x}格')
else:
move_y = r.choice()
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:
self.fish_x = 2*legal_x - self.fish_x
if self.fish_y < 0:
self.fish_y = -self.fish_y
if self.fish_y > legal_x:
self.fish_y = 2*legal_x - 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:
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()
self.fish_y = self.fish_y + move_y
elif self.fish_y == 0 or self.fish_y == legal_y:
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()
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()
if r.choice():
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:
self.fish_x = 2 * legal_x - self.fish_x
if self.fish_y < 0:
self.fish_y = -self.fish_y
if self.fish_y > legal_x:
self.fish_y = 2 * legal_x - 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 = Fish()
#乌龟初始位置
loc_turtle = (turtle.turtle_x, turtle.turtle_y)
print('小甲鱼的初始位置', loc_turtle)
#小鱼的初始位置
loc = {i : (fish.fish_x, fish.fish_y) for i in fish.keys() }
l = []
for i in loc.keys():
l.append(i)
l.sort()
print(f'鱼的初始位置:{l+1}号小鱼:坐标', loc])
for i in range(1, len(l)):
print(f' {i+1}号小鱼:坐标 {loc]}')
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 - loc_turtle) ** 2 + (loc - loc_turtle) ** 2)
if every_dis <= 3:
dist_fish.append(every_loc)
#记录每一条小鱼每移动一次到达的位置
for every_fish in fish.keys():
if every_fish in dist_fish:
loc = fish.escape(loc_turtle, loc_turtle)
print(f' 小鱼{every_fish}号:救命啊,我被发现了,快跑快跑!!! (。ŏ_ŏ)')
continue
loc = fish.move()
# 好看地打印出每条小鱼的坐标
l = []
for i in loc.keys():
l.append(i)
l.sort()
print(f' 小鱼的位置:{l + 1}号小鱼:坐标', loc])
for i in range(1, len(l)):
print(f' {i + 1}号小鱼:坐标 {loc]}')
# 记录在小甲鱼能看到的每一条小鱼到达的位置,和到小甲鱼的距离
dist = {}
for every_loc in loc.keys():
every_dis = math.sqrt((loc - loc_turtle) ** 2 + (loc - loc_turtle) ** 2)
if every_dis <= 10.0:
dist] = every_dis
if len(dist) > 0: #在小甲鱼视野范围中的小鱼,启动快速模式
print(f' 小甲鱼:我发现了{len(dist)}条小鱼!', end='')
min_dist = min(dist.values())
for every_dist in dist.keys():
if dist == min_dist:
if turtle.power >= 2:#如果小甲鱼的能量大于 2,就可以使用超级无敌加速模式
loc_turtle = turtle.quick_move(every_dist, every_dist)
for z in loc.keys():
if loc == 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 == loc_turtle:
turtle.eat()
del fish
del loc
print(f' {fish_num}号小鱼:啊,我被吃掉了!')
if len(fish) == 0:
break
print(f' 小甲鱼:好吃好吃!还剩{len(fish)}条小鱼 (๑•؎ •๑)✧') 沙发{:10_297:} zxynb 发表于 2023-6-8 19:34
沙发
可恶!自己抢 本帖最后由 yinda_peng 于 2023-6-8 20:23 编辑
厉害的厉害的 厉害 yinda_peng 发表于 2023-6-8 20:16
可恶!自己抢
O(∩_∩)O哈哈~ 啊,文字互动里面的下标处理的不太好,发的时候都没发现,哈哈。打代码打的头晕了{:10_247:}
我吃了个晚饭才发现{:10_282:} 厉害了小伙子,学两个月就能做游戏开发了 {:5_106:}
页:
[1]