鱼C论坛

 找回密码
 立即注册
查看: 1493|回复: 1

[技术交流] 小游戏

[复制链接]
发表于 2017-7-8 13:40:23 | 显示全部楼层 |阅读模式

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

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

x
############################
#乌龟吃鱼小游戏
#
############################
import random as rd
#边缘检测函数
def check_boundary(x,y):
    if x>10:
        x=10-x%10
    elif y>10:
        y=10-y%10
    elif x<0:
        x=-x
    elif y<0:
        y=-y
    else:
        pass
    return (x,y)
class Tortoise:
    x=rd.randint(0,10)
    y=rd.randint(0,10)
    direct='h'
    step=0
    tortoise_energy=100 #乌龟能量

    def move(self):     
        #选择移动方向
        self.direct = rd.choice('xy')
        #选择移动步长
        self.step = rd.choice((-2,-1,1,2))
        #移动
        if 'x' == self.direct:
            self.x+=self.step
        else:
            self.y+=self.step

        #边缘检测
        (self.x,self.y)=check_boundary(self.x,self.y)
        #体力减1
        self.tortoise_energy-=1
        print('乌龟的位置为%d,%d'%(self.x,self.y))
        return (self.x,self.y)
    def eat(self):
        self.tortoise_energy+=20
        print('乌龟吃了一条小鱼 T_T')
        
   
class Fish:
    x=rd.randint(0,10)
    y=rd.randint(0,10)
    direct='h'
    step=0   

    def move(self):
        #选择移动方向
        self.direct = rd.choice('xy')
        #选择移动步长
        self.step = rd.choice((-1,1))
        #移动
        if 'x' == self.direct:
            self.x+=self.step
        else:
            self.y+=self.step

        #边缘检测
        (self.x,self.y)=check_boundary(self.x,self.y)
        print('鱼的位置为%d,%d'%(self.x,self.y))
        return (self.x,self.y)
tortoise = Tortoise()
fish=[]
for i in range(10):
    new_fish=Fish()
    fish.append(new_fish)
while True:
    if 0==len(fish):
        print('鱼被吃光了')
        break
    if 0>=tortoise.tortoise_energy:
        print('乌龟没体力了')
        break
   
    pos = tortoise.move()
    for i in fish[:]:
        if i.move()==pos:
            fish.remove(i)
            tortoise.eat()
            
            
        
   

   

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

使用道具 举报

发表于 2017-7-8 15:30:12 | 显示全部楼层
挺好的,不过代码记得使用代码格式包裹起来哦~

点击编辑框的 <> 标识插入代码即可。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-26 13:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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