鱼C论坛

 找回密码
 立即注册
查看: 2065|回复: 0

[作品展示] 37课作业乌龟吃鱼小游戏

[复制链接]
发表于 2022-11-3 01:50:41 | 显示全部楼层 |阅读模式

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

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

x
111.png
import time
import random
#创建类
maxRange=10#正方形一边格子数
#乌龟类
class Tortoise:
    name=""#名字
    bloodValue=100#血量
    movdistance=1#移动距离
    direct=0#移动方向0向上,1向右,2向下,3向左
    x=0#x坐标
    y=0#y坐标
    def __init__(self,name):
        self.x=random.randint(0,maxRange-1)#随机生成初始坐标
        self.y=random.randint(0,maxRange-1)
        self.name=name
    def run(self):
        if self.bloodValue<=0:
            return
        self.direct=random.randint(0,3)#随机生产移动方向
        self.movdistance=random.randint(1,2)#随机产生移动距离
        if self.direct==0:
            if self.y-self.movdistance<0:
                self.direct=2#碰壁反弹,朝反方向走
        elif self.direct==1:
            if self.x+self.movdistance>maxRange-1:
                self.direct=3#碰壁反弹,朝反方向走
        elif self.direct==2:
            if self.y+self.movdistance>maxRange-1:
                self.direct=0#碰壁反弹,朝反方向走
        elif self.direct==3:
            if self.x-self.movdistance<0:
                self.direct=1#碰壁反弹,朝反方向走
        temp=random.randint(0,2)#随机决定要不要移动temp=0时不移动
        if temp:
            self.mov()#确定可以移动的方向后移动
        
    def mov(self):#移动坐标
        if self.bloodValue<=0:
            return
        if self.direct==0:
            self.y-=self.movdistance
        elif self.direct==1:
            self.x+=self.movdistance
        elif self.direct==2:
            self.y+=self.movdistance
        elif self.direct==3:
            self.x-=self.movdistance
        self.bloodValue-=1#移动后体力消耗1
        
    def getxy(self):#获取乌龟坐标
        return [self.x,self.y]

    def getFish(self):#吃掉一条鱼
        if self.bloodValue<=0:
            return
        self.bloodValue+=20

#鱼类
class Fish:
    name=""#名字
    bloodValue=100#血量
    movdistance=1#移动距离
    direct=0#移动方向0向上,1向右,2向下,3向左
    x=0#x坐标
    y=0#y坐标
    def __init__(self,name):
        self.x=random.randint(0,maxRange-1)#随机生成初始坐标
        self.y=random.randint(0,maxRange-1)
        self.name=name
    def run(self):
        if self.bloodValue<=0:
            return
        self.direct=random.randint(0,3)#随机生产移动方向
        self.movdistance=random.randint(1,2)#随机产生移动距离
        if self.direct==0:
            if self.y-self.movdistance<0:
                self.direct=2#碰壁反弹,朝反方向走
        elif self.direct==1:
            if self.x+self.movdistance>maxRange-1:
                self.direct=3#碰壁反弹,朝反方向走
        elif self.direct==2:
            if self.y+self.movdistance>maxRange-1:
                self.direct=0#碰壁反弹,朝反方向走
        elif self.direct==3:
            if self.x-self.movdistance<0:
                self.direct=1#碰壁反弹,朝反方向走
        temp=random.randint(0,2)#随机决定要不要移动temp=0时不移动
        if temp:
            self.mov()#确定可以移动的方向后移动
        
    def mov(self):#移动坐标
        if self.bloodValue<=0:
            return
        if self.direct==0:
            self.y-=self.movdistance
        elif self.direct==1:
            self.x+=self.movdistance
        elif self.direct==2:
            self.y+=self.movdistance
        elif self.direct==3:
            self.x-=self.movdistance
        
        
    def getxy(self):#获取鱼坐标
        return [self.x,self.y]

    def suicide(self):#鱼被吃
        self.bloodValue=0

#打印每个坐标的乌龟和鱼
def printAllData(listAllData):
    chessList=[]#棋盘数据
    isAnmal=False
    #print(listAllData)
    for eachx in range(maxRange):
        print("\n",end="")
        for eachy in range(maxRange):
            strTemp=""
            isAnmal=False
            for each in listAllData:
                if [eachy,eachx]==each[1]:
                    strTemp+=each[0]+" "
                    isAnmal=True
                    
            if isAnmal==False:     
                strTemp+="[  ]\t"
            else:
                strTemp+="\t"
            print(strTemp,end="")
    print("\n")
               
        

#开始游戏
tor1=Tortoise("龟")#生成一只乌龟
fishList=[]
fishcount=10#生成鱼的数量
for i in range(fishcount):#生成fishcount个鱼
    fishList.append(Fish(str(i+1)))

#游戏开跑
while True:
    if tor1.bloodValue<=0 or len(fishList)<=0:#鱼吃完或者乌龟体力为0时结束游戏
        break
    tor1.run()#乌龟运动
    for each in fishList:#鱼运动
        each.run()
    #判断坐标,吃鱼判断
    for each in fishList:
        if each.getxy()==tor1.getxy():
            each.suicide()#鱼生命值为0
            tor1.getFish()#乌龟生命值+20
        
    #收集所有生物坐标
    listAllData=[]
    listAllData.append((tor1.name,tor1.getxy()))
    for each in fishList:
        listAllData.append((each.name,each.getxy()))
    printAllData(listAllData)#打印每个坐标的乌龟和鱼

    for each in fishList:#删除嗝屁的鱼
        if each.bloodValue<=0:
            fishList.remove(each)

    print("乌龟生命值:",str(tor1.bloodValue),"坐标:",str(tor1.x),":",str(tor1.y))
    print("还剩"+str(len(fishList))+"条鱼")
    #for each in fishList:
        #print(each.name+"号鱼生命值:",str(each.bloodValue),"坐标:",str(each.x),":",str(each.y))
    print("\n")
    time.sleep(2)#等待0.5秒
   
if tor1.bloodValue>0:
    print("乌龟成功生存!")
else:
    print("乌龟生存失败!")
name=input("游戏结束!")
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-25 19:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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