鱼C论坛

 找回密码
 立即注册
查看: 1598|回复: 2

[作品展示] 大鱼吃小鱼

[复制链接]
发表于 2021-10-23 20:23:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
目前还没没学到做图像,想学到最后,能实现插入大鱼小鱼图像,电脑控制的小鱼能随时间移动,玩家可以控制自己选择的小鱼类型移动
代码如下:

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[0])):              #生成鱼
    each_Fish = Fish(int(value[5]),int(value[6]))
    fish.append(each_Fish)
for i in range(int(value[1])):
    each_Goldfish = Goldfish(int(value[5]),int(value[6]))
    fish.append(each_Goldfish)   
for i in range(int(value[2])):
    each_Carp = Carp(int(value[5]),int(value[6]))
    fish.append(each_Carp)   
for i in range(int(value[3])):
    each_Salmon = Salmon(int(value[5]),int(value[6]))
    fish.append(each_Salmon)   
for i in range(int(value[4])):
    each_Shark = Shark(int(value[5]),int(value[6]))
    fish.append(each_Shark)
   
while True:
    old = len(fish)         # 判断鱼数量是否减少的参数
    position = []           #(x,y,饥饿度,体型)   
    for each in fish:
        pos = each.move(int(value[5]),int(value[6]))
        position.append(pos)     
    for each in position:
        target = position[:]
        target.remove(each)
        for i in target:
            if each[0] == i[0] and each[1] == i[1]:
                if each[3] > i[3]:          #比较体型
                    a = position.index(each)
                    b = target.index(i)
                    if a <= b:
                        b += 1              #避免因删除元素导致的序号错误
                    eat_fish[a] = b               
                elif each[3] < i[3]:
                    a = position.index(each)
                    b = target.index(i)
                    if a <= b:
                        b += 1              #避免因删除元素导致的序号错误                    
                    eat_fish[b] = a      
    for i in eat_fish.keys():
        fish[i].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[i-d])
        d = 0
    for each in fish:               #判断饥饿致死
        if each.hungry <= 0:
            fish.remove(each)
    eat_fish = {}                   #清空吃鱼与被吃鱼的元组
    if len(fish) == 1:              #判定获胜
        print(fish[0].win())
        break
    if len(fish) != old:            # 判断鱼数量是否减少
        print('经过了%s回合,现在还剩下%s条鱼在角逐冠军' % (n,len(fish)))
    n += 1
        
        
            
            
   


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-10-24 09:25:19 | 显示全部楼层
学以致用,厉害
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-24 16:25:13 | 显示全部楼层

谢谢大佬
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-24 01:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表