大鱼吃小鱼
目前还没没学到做图像,想学到最后,能实现插入大鱼小鱼图像,电脑控制的小鱼能随时间移动,玩家可以控制自己选择的小鱼类型移动代码如下:
class Fish:
def __init__(self,limit1,limit2):
self.x = r.randint(0,limit1)
self.y = r.randint(0,limit2)
self.hungry = 1000
self.weight = 1
self.cost = 1 * self.weight
self.length = 1 * self.weight
def eat(self):
self.weight += 1
self.hungry += 200
self.cost += 1
self.length += 1
if self.hungry > 1000:
self.hungry = 1000
def move(self,limit1,limit2):
self.list1 = []
for i in range(-self.length,self.length + 1):
self.list1.append(i)
self.x += r.choice(self.list1)
self.y += r.choice(self.list1)
if self.x > 50:
self.x = 2 * limit1 - self.x
elif self.y > 50:
self.y = 2 * limit2 - self.y
elif self.x < 0:
self.x = -self.x
elif self.y < 0:
self.y = -self.y
self.hungry -= self.cost
return (self.x,self.y,self.hungry,self.weight)
def win(self):
print('小咸鱼获胜了,现在的体型是%s,饥饿度是%s' % (self.weight,self.hungry))
class Goldfish(Fish):
def __init__(self,limit1,limit2):
super().__init__(limit1,limit2)
self.weight = 2
def win(self):
print('金鱼获胜了,现在的体型是%s,饥饿度是%s' % (self.weight,self.hungry))
class Carp(Fish):
def __init__(self,limit1,limit2):
super().__init__(limit1,limit2)
self.weight = 3
def win(self):
print('鲤鱼获胜了,现在的体型是%s,饥饿度是%s' % (self.weight,self.hungry))
class Salmon(Fish):
def __init__(self,limit1,limit2):
super().__init__(limit1,limit2)
self.weight = 4
def win(self):
print('三文鱼获胜了,现在的体型是%s,饥饿度是%s' % (self.weight,self.hungry))
class Shark(Fish):
def __init__(self,limit1,limit2):
super().__init__(limit1,limit2)
self.weight = 5
def win(self):
print('鲨鱼获胜了,现在的体型是%s,饥饿度是%s' % (self.weight,self.hungry))
import random as r
import easygui as g
fish = [] #水池
eat_fish = {} #吃鱼与被吃鱼
n = 1 #回合数
list1 = ['小咸鱼','金鱼','鲤鱼','三文鱼','鲨鱼','长','宽']
list2 = ['100','50','30','10','5','50','50']
value = g.multenterbox('需要进入比赛的小鱼','大鱼吃小鱼',list1,list2)
for i in range(int(value)): #生成鱼
each_Fish = Fish(int(value),int(value))
fish.append(each_Fish)
for i in range(int(value)):
each_Goldfish = Goldfish(int(value),int(value))
fish.append(each_Goldfish)
for i in range(int(value)):
each_Carp = Carp(int(value),int(value))
fish.append(each_Carp)
for i in range(int(value)):
each_Salmon = Salmon(int(value),int(value))
fish.append(each_Salmon)
for i in range(int(value)):
each_Shark = Shark(int(value),int(value))
fish.append(each_Shark)
while True:
old = len(fish) # 判断鱼数量是否减少的参数
position = [] #(x,y,饥饿度,体型)
for each in fish:
pos = each.move(int(value),int(value))
position.append(pos)
for each in position:
target = position[:]
target.remove(each)
for i in target:
if each == i and each == i:
if each > i: #比较体型
a = position.index(each)
b = target.index(i)
if a <= b:
b += 1 #避免因删除元素导致的序号错误
eat_fish = b
elif each < i:
a = position.index(each)
b = target.index(i)
if a <= b:
b += 1 #避免因删除元素导致的序号错误
eat_fish = a
for i in eat_fish.keys():
fish.eat()
d = 0 #计数参数,防止因删除元素导致的序号错误
for i in eat_fish.values():
for each in eat_fish.values():
if i > each:
d += 1
elif i == each:
break
fish.remove(fish)
d = 0
for each in fish: #判断饥饿致死
if each.hungry <= 0:
fish.remove(each)
eat_fish = {} #清空吃鱼与被吃鱼的元组
if len(fish) == 1: #判定获胜
print(fish.win())
break
if len(fish) != old: # 判断鱼数量是否减少
print('经过了%s回合,现在还剩下%s条鱼在角逐冠军' % (n,len(fish)))
n += 1
学以致用,厉害{:5_106:}{:5_106:}{:5_106:} 傻眼貓咪 发表于 2021-10-24 09:25
学以致用,厉害
谢谢大佬
页:
[1]