鱼C论坛

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

[作品展示] 乌龟和鱼的游戏故事

[复制链接]
发表于 2020-8-31 08:45:23 | 显示全部楼层 |阅读模式

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

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

x
[b]乌龟和鱼的游戏故事[/b]
     假设游戏场景为范围(x, y)为0<=x<=10,0<=y<=10
     游戏生成1只乌龟和10条鱼
    它们的移动方向均随机
    乌龟的最大移动能力是2(Ta可以随机选择1还是2移动),鱼儿的最大移动能力是1
   游戏界面: 037-01游戏界面.jpg
      
   
  1. import random
  2. import time
  3. import winsound

  4. class Turtle:
  5.     def __init__(self):
  6.         self.x =random.randint(0,9)
  7.         self.y =random.randint(0,9)
  8.         self.worth = 100
  9.     def move(self):
  10.         direction =random.randint(1,4)#1 代表向左;2代表向右;3代表向上;4代表向下
  11.         step =random.randint(1,2) #移动距离
  12.         if direction == 1:
  13.             self.x -=step
  14.             if self.x<0:
  15.                 self.x *=-1
  16.         elif direction ==2:
  17.             self.x +=step
  18.             if self.x>=10:
  19.                 self.x -=10
  20.         elif direction ==3:
  21.             self.y +=step
  22.             if self.y>=10:
  23.                 self.y -=10
  24.         else:
  25.             self.y -=step
  26.             if self.y<0:
  27.                 self.y *=-1
  28.         self.worth -=1
  29.         
  30.    
  31.    
  32.     def eatfish(self):
  33.         self.worth += 20
  34.         return self.worth

  35.     def getposition(self):
  36.         #print(self.x,self.y,"in Turtle")
  37.         return [self.x,self.y]
  38.    

  39. class Fish:
  40.     def __init__(self):
  41.         self.x =random.randint(0,9)
  42.         self.y =random.randint(0,9)
  43.         self.worth = 20

  44.     def move(self):
  45.         direction =random.randint(1,4)#1 代表向左;2代表向右;3代表向上;4代表向下
  46.         step = 1 #移动距离
  47.         if direction == 1:
  48.             self.x -=step
  49.             if self.x<0:
  50.                 self.x *=-1
  51.         elif direction ==2:
  52.             self.x +=step
  53.             if self.x>=10:
  54.                 self.x -=10
  55.         elif direction ==3:
  56.             self.y +=step
  57.             if self.y>=10:
  58.                 self.y -=10
  59.         else:
  60.             self.y -=step
  61.             if self.y<0:
  62.                 self.y *=-1
  63.                
  64.     def getposition(self):
  65.         #print(self.x,self.y,"in Fish")
  66.         return [self.x,self.y]
  67.    
  68.     def destroyed(self):
  69.         self.worth = 0

  70. class Pool:
  71.     #turtle1 =Turtle()#生成乌龟1只
  72.     def __init__(self):
  73.         self.turtle1 = Turtle()
  74.         self.listFish = [] #生成鱼群---10只
  75.         for i in range(10):
  76.             self.listFish.append(Fish())            
  77.         self.countTur =1
  78.         self.countFish =10
  79.         self.lines = [] #记录输出界面
  80.         

  81.         



  82.     def judge(self): #游戏结束返回False,否则返回True
  83.         for fish in self.listFish:  
  84.             
  85.             if self.turtle1.getposition() == fish.getposition() and fish.worth>0:
  86.                 self.turtle1.eatfish()
  87.                 fish.destroyed()
  88.                 self.countFish -=1
  89.                 winsound.Beep(600,50)#吃上鱼发出告警声 50毫秒时间
  90.             
  91.         if (self.countFish == 0 or self.countTur == 0):
  92.             return False
  93.         else:
  94.             return True
  95.         
  96.     def initPanel(self):
  97.         self.lines = []
  98.         self.lines.append("   0  1  2  3  4  5  6  7  8  9  ")#行号0
  99.         self.lines.append(" |------------------------------")#行号1
  100.         self.lines.append("0|  |  |  |  |  |  |  |  |  |  | 0")#行号2
  101.         self.lines.append(" |-----------------------------|")#行号3
  102.         self.lines.append("1|  |  |  |  |  |  |  |  |  |  | 1")#行号4
  103.         self.lines.append(" |-----------------------------|")#行号5
  104.         self.lines.append("2|  |  |  |  |  |  |  |  |  |  | 2")#行号6
  105.         self.lines.append(" |-----------------------------|")#行号7
  106.         self.lines.append("3|  |  |  |  |  |  |  |  |  |  | 3")#行号8
  107.         self.lines.append(" |-----------------------------|")#行号9
  108.         self.lines.append("4|  |  |  |  |  |  |  |  |  |  | 4")#行号10
  109.         self.lines.append(" |-----------------------------|")#行号11
  110.         self.lines.append("5|  |  |  |  |  |  |  |  |  |  | 5")#行号12
  111.         self.lines.append(" |-----------------------------|")#行号13
  112.         self.lines.append("6|  |  |  |  |  |  |  |  |  |  | 6")#行号14
  113.         self.lines.append(" |-----------------------------|")#行号15
  114.         self.lines.append("7|  |  |  |  |  |  |  |  |  |  | 7")#行号16
  115.         self.lines.append(" |-----------------------------|")#行号17
  116.         self.lines.append("8|  |  |  |  |  |  |  |  |  |  | 8")#行号18
  117.         self.lines.append(" |-----------------------------|")#行号19
  118.         self.lines.append("9|  |  |  |  |  |  |  |  |  |  | 9")#行号20
  119.         self.lines.append("1|--------------------------------------------------------------------------------|")#行号21
  120.         self.lines.append("2|鱼      |鱼     |鱼     |鱼     |鱼     |鱼     |鱼     |鱼     |鱼     |鱼     |")#行号22
  121.         self.lines.append("3|1       |2      |3      |4      |5      |6      |7      |8      |9      |10     |")#行号23
  122.         self.lines.append("4|--------------------------------------------------------------------------------|")#行号24
  123.         self.lines.append("5|        |       |       |       |       |       |       |       |       |       |  ")#行号25 鱼的生命值
  124.         self.lines.append("6|--------------------------------------------------------------------------------| ")#行号26
  125.         self.lines.append("7|        |       |       |       |       |       |       |       |       |       |  ")#行号27 鱼的位置
  126.         self.lines.append("8|--------------------------------------------------------------------------------|")#行号28
  127.         self.lines.append("9|乌龟X生命值及位置:                                                             |")#行号29
  128.         self.lines.append("0|--------------------------------------------------------------------------------|")#行号30
  129.         self.lines.append("9|活乌龟数量:                    |活鱼数量:                                     |")#行号31
  130.                
  131.     def display(self):
  132.         
  133.         self.lines = []
  134.         self.initPanel()
  135.         
  136.         #显示鱼的位置,及生命值,活鱼用#表示,死鱼用.表示
  137.         for i in range(10):
  138.             self.lines[25]=self.lines[25][0:3+8*i]+str(self.listFish[i].worth)+self.lines[25][3+8*i+len(str(self.listFish[i].worth)):]#生命值
  139.             posXy=str(self.listFish[i].x)+":"+str(self.listFish[i].y)
  140.             self.lines[27]=self.lines[27][0:3+8*i]+posXy+self.lines[27][3+8*i+len(posXy):]#X:Y坐标
  141.             
  142.             #显示鱼的生命值
  143.             if self.listFish[i].worth>0:
  144.                 self.lines[self.listFish[i].x*2+2]=self.lines[self.listFish[i].x*2+2][0:self.listFish[i].y*3+2]+"="+self.lines[self.listFish[i].x*2+2][self.listFish[i].y*3+3:]
  145.                 #显示活鱼的位置
  146.             else:
  147.                 self.lines[self.listFish[i].x*2+2]=self.lines[self.listFish[i].x*2+2][0:self.listFish[i].y*3+2]+"."+self.lines[self.listFish[i].x*2+2][self.listFish[i].y*3+3:]
  148.                 #显示死鱼位置
  149.         ########################################################################
  150.                
  151.         self.lines[self.turtle1.x*2+2]=self.lines[self.turtle1.x*2+2][0:self.turtle1.y*3+2]+"X"+self.lines[self.turtle1.x*2+2][self.turtle1.y*3+3:]
  152.         self.lines[29]=self.lines[29][0:14]+str(self.turtle1.worth)+" "+str(self.turtle1.x)+":"+str(self.turtle1.y)+self.lines[29][18+len(str(self.turtle1.worth)):]
  153.         #显示乌龟位置及生命值

  154.         #############################################################################
  155.         self.lines[31]=self.lines[31][0:12]+str(self.countTur)+self.lines[31][13:42]+str(self.countFish)+self.lines[31][42+len(str(self.countFish)):]

  156.         #####统一显示
  157.         for each in self.lines:
  158.             print(each)
  159.                
  160.                
  161.         
  162.       

  163.     def act(self):
  164.         if self.turtle1.worth>0:
  165.             self.turtle1.move()#乌龟移动            
  166.             if self.turtle1.worth<=0:#如果乌龟生命值为零 则调整池子中乌龟的数量
  167.                 self.countTur=0
  168.         for fish in self.listFish:#鱼群移动
  169.             if fish.worth>0:
  170.                 fish.move()

  171.         
  172.                
  173.         result = self.judge() #判断一下乌龟有无吃到鱼群中的鱼,并由此乌龟调整生命值和存活鱼的数量
  174.                               #池子中如果鱼,或龟的数量有一个为零,则返回False游戏结束,否则返回True
  175.         
  176.       
  177.         self.display()#显示移动和判定后所有动物(乌龟和鱼)的位置及新的生命值
  178.       
  179.         return result
  180.         
  181.         
  182.         
  183.    
  184. myPool =Pool()   
  185. while True:
  186.       
  187.       
  188.       
  189.        result =myPool.act()
  190.        if result==False:
  191.            break
  192.         
  193.       
  194.         
  195.    
复制代码
当移动到场景边缘,自动向反方向移动
乌龟初始化体力为100(上限)
乌龟每移动一次,体力消耗1
当乌龟和鱼坐标重叠,乌龟吃掉鱼,乌龟体力增加20
鱼暂不计算体力
当乌龟体力值为0(挂掉)或者鱼儿的数量为0游戏结束
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-8-31 23:12:18 From FishC Mobile | 显示全部楼层
你的那个打印出来的代码有点重复麻烦了,可以在改进一下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-5 05:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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