鱼C论坛

 找回密码
 立即注册
查看: 1568|回复: 4

[已解决]python 037乌龟和鱼的游戏 课后习题

[复制链接]
发表于 2019-8-8 17:48:02 | 显示全部楼层 |阅读模式

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

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

x
游戏编程:按以下要求定义一个乌龟类和鱼类并尝试编写游戏。

假设游戏场景为范围(x, y)为0<=x<=10,0<=y<=10
        游戏生成1只乌龟和10条鱼
        它们的移动方向均随机
        乌龟的最大移动能力是2(Ta可以随机选择1还是2移动),鱼儿的最大移动能力是1
        当移动到场景边缘,自动向反方向移动
        乌龟初始化体力为100(上限)
        乌龟每移动一次,体力消耗1
        当乌龟和鱼坐标重叠,乌龟吃掉鱼,乌龟体力增加20
        鱼暂不计算体力
        当乌龟体力值为0(挂掉)或者鱼儿的数量为0游戏结束

  1. import random
  2. class Turtle:
  3.     def __init__(self):
  4.         self.energy=100
  5.         self.x=random.randint(1,10)
  6.         self.y=random.randint(1,10)
  7.         i=random.randint(0,3)
  8.         self.direction=[[1,'x'],[1,'y'],[-1,'x'],[-1,'y']][i]
  9.     def move(self):
  10.         move1=random.randint(1,2)
  11.         for i in range(move1):
  12.             if self.x==10 or self.y==10:
  13.                 self.direction[0]=-1*self.direction[0]
  14.             elif self.direction[1]=='x' and self.direction[0]==1:
  15.                 self.x+=1
  16.             elif self.direction[1]=='x' and self.direction[0]==-1:
  17.                 self.x-=1
  18.             elif self.direction[1]=='y' and self.direction[0]==1:
  19.                 self.y+=1
  20.             elif self.direction[1]=='y' and self.direction[0]==-1:
  21.                 self.y-=1
  22.             self.energy-=1
  23.         
  24. class Fish:
  25.     def __init__(self):
  26.         self.x=random.randint(1,10)
  27.         self.y=random.randint(1,10)
  28.         i=random.randint(0,3)
  29.         self.direction=[[1,'x'],[1,'y'],[-1,'x'],[-1,'y']][i]
  30.     def move(self):
  31.         if self.x==10 or self.y==10:
  32.             self.direction[0]=-1*self.direction[0]
  33.         elif self.direction[1]=='x' and self.direction[0]==1:
  34.             self.x+=1
  35.         elif self.direction[1]=='x' and self.direction[0]==-1:
  36.             self.x-=1
  37.         elif self.direction[1]=='y' and self.direction[0]==1:
  38.             self.y+=1
  39.         elif self.direction[1]=='y' and self.direction[0]==-1:
  40.             self.y-=1
  41.             
  42. def game():
  43.     fish=[]
  44.     wugui=Turtle()
  45.     for i in range(10):
  46.         fish.append(Fish())
  47.     while 1:
  48.         print(wugui.energy)
  49.         wugui.move()
  50.         for each in fish:
  51.             each.move()
  52.         for each in fish:
  53.             if each.x==wugui.x and each.y==wugui.y:
  54.                 fish.remove(each)
  55.                 print('有一条鱼被吃掉了')
  56.                 print(wugui.energy)
  57.                 wugui.energy+=20
  58.         if fish==[]:
  59.             print('wuguiasdsssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss胜利')
  60.             break
  61.         elif wugui.energy<=0:
  62.             print('yu胜利')
  63.             break
  64.             
  65. while 1:
  66.     input('回车继续')
  67.     game()        
复制代码



乌龟就没赢过,而且每次有鱼被吃都是在energy至少80的时候,不知道是不是错了
最佳答案
2019-8-9 15:03:22
你这个代码看似很整齐,想法也很好,但是细节处的逻辑简直了,我都没办法条条框框列出来
首先,你想要做到的限制的活动范围为0-10,0-10都没有做到,不相信你可以自己print出来看看fish的坐标
其次你的if 。。。elif 判断简直毁了你整段代码,移动是这段代码的核心啊
最后,乌龟的体力到底是初始化只有100 还是整个游戏中都不超过100,你要想清楚了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-8-9 18:45:26 | 显示全部楼层
对代码进行修改,现在代码可以调节小鱼数量、乌龟体力、活动范围、游戏开始与结束
代码如下:
import random
      
class Move_step:
    def __init__(self,right,left,top,bottom):
        self.right = right
        self.left = left
        self.top = top        
        self.bottom = bottom
            
    def move(self,breed):
        i=random.randint(0,3)   
        self.direction=[1,2,3,4][i] #代表移动的方向,分别对应 +x,+y,-x,-y
        if breed == 'wugui':
            move_distance=random.randint(1,2)
            self.energy -= 1 *  move_distance
        else:
            move_distance=1   #代表移动的大小
        for i in range(move_distance):
            if self.left <= self.x <= self.right or self.bottom <= self.y <= self.top:            
                if self.direction == 1:
                    if self.x == self.right:
                        self.x = self.left + 1
                    else:
                        self.x += 1
                elif self.direction == 2:
                    if self.y == self.top:
                        self.y = self.bottom + 1
                    else:
                        self.y += 1               
                elif self.direction == 3:
                    if self.x == self.left:
                        self.x == self.right - 1
                    else:
                        self.x -= 1
                elif self.direction == 4:
                    if self.y == self.bottom:
                        self.y = self.top - 1
                    else:
                        self.y -= 1
            else:
                print('报错',(self.x,self.y))                  
               
class Turtle(Move_step):
    def __init__(self,energy,right,left,top,bottom):
        super().__init__(right,left,top,bottom)
        self.energy = energy
        self.x=random.randint(self.left,self.right)
        self.y=random.randint(self.top,self.bottom)  #乌龟坐标值初始化
        
class Fish(Move_step):
    def __init__(self,right,left,top,bottom):
        super().__init__(right,left,top,bottom)
        self.x=random.randint(self.left,self.right)
        self.y=random.randint(self.top,self.bottom)  #鱼坐标值初始化  
            
def game():      
    a,b,c,d = map(int,input('请输入游戏范围的上、下、左、右【整数】,并以空格隔开:').split())
    if a < b or d < c:
        print("哎呀,范围出错啦")
        return      
    energy = int(input('你希望乌龟的体力是:'))        
    wugui=Turtle(energy,a,b,c,d)
    fish=[]         
    fish_num = input('小鱼的数量:')
    for i in range(int(fish_num)):
        fish.append(Fish(a,b,c,d))                           
    while 1:
        wugui.move('wugui')
        for each in fish:
            each.move('fish')            
        for each in fish:         
            if each.x==wugui.x and each.y==wugui.y:
                fish.remove(each)               
                print('有一条鱼被吃掉了')
                wugui.energy+=20               
        if fish==[]:
            print('wuguiasdsssssssssssssssssssssssssss胜利')
            break
        if wugui.energy<=0:
            print('yu胜利')
            break
           

while 1:
    print("开始游戏请输入1,结束游戏请输入0")
    start = input('请输入:')
    if start == '1':
       game()
    else:
        print("游戏结束!")
        break
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2019-8-9 16:25:58 | 显示全部楼层
这是修改后的代码:
import random
      
class Move:
    def __init__(self):
        i=random.randint(0,3)   
        self.direction=[1,2,3,4][i] #代表移动的方向,分别对应 +x,+y,-x,-y
              
    def move(self,breed):
        i=random.randint(0,3)   
        self.direction=[1,2,3,4][i] #代表移动的方向,分别对应 +x,+y,-x,-y
        if breed == 'wugui':
            move_distance=random.randint(1,2)
            self.energy -= 1 *  move_distance
        else:
            move_distance=1   #代表移动的大小
        for i in range(move_distance):
            if 0 <= self.x <= 10 or 0 <= self.y <= 10:            
                if self.direction == 1:
                    if self.x == 10:
                        self.x = 1
                    else:
                        self.x += 1
                elif self.direction == 2:
                    if self.y == 10:
                        self.y = 1
                    else:
                        self.y += 1               
                elif self.direction == 3:
                    if self.x == 0:
                        self.x == 9
                    else:
                        self.x -= 1
                elif self.direction == 4:
                    if self.y == 0:
                        self.y = 9
                    else:
                        self.y -= 1
            else:
                print('报错',(self.x,self.y))                  
               
class Turtle(Move):
    def __init__(self):
        self.energy=30
        self.x=random.randint(1,10)
        self.y=random.randint(1,10)  #乌龟坐标值初始化
        
class Fish(Move):
    def __init__(self):
        self.x=random.randint(1,10)
        self.y=random.randint(1,10) #鱼坐标值初始化  
            
def game():
    fish=[]
    wugui=Turtle()
    for i in range(10):
        fish.append(Fish())
        
    while 1:
        wugui.move('wugui')
        for each in fish:
            each.move('fish')            
        for each in fish:         
            if each.x==wugui.x and each.y==wugui.y:
                fish.remove(each)               
                print('有一条鱼被吃掉了')
                wugui.energy+=20               
        if fish==[]:
            print('wuguiasdsssssssssssssssssssssssssss胜利')
            break
        if wugui.energy<=0:
            print('yu胜利')
            break
         
while 1:
    print("开始游戏请输入1,结束游戏请输入0")
    a = input('请输入:')
    if a == '1':
       game()
    else:
        print("游戏结束!")
        break
   
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-8-9 16:10:44 | 显示全部楼层
¥叶紫¥ 发表于 2019-8-9 15:03
你这个代码看似很整齐,想法也很好,但是细节处的逻辑简直了,我都没办法条条框框列出来
首先,你想要做到 ...

你说的体力问题其实我都知道,现在问题大部分都已经解决了,不过if。。elif那个移动确实有点冗杂,答案还是很清楚的
  1. import random
  2. class Turtle:
  3.     def __init__(self):
  4.         self.energy=999
  5.         self.x=random.randint(1,10)
  6.         self.y=random.randint(1,10)
  7.         i=random.randint(0,3)
  8.         self.direction=[[1,'x'],[1,'y'],[-1,'x'],[-1,'y']][i]
  9.     def move(self):
  10.         move1=random.randint(1,2)
  11.         for i in range(move1):
  12.             if self.x==10 or self.y==10 or self.x==0 or self.y==0:
  13.                 self.direction[0]=-1*self.direction[0]
  14.             if self.direction[1]=='x' and self.direction[0]==1:
  15.                 self.x+=1
  16.             elif self.direction[1]=='x' and self.direction[0]==-1:
  17.                 self.x-=1
  18.             elif self.direction[1]=='y' and self.direction[0]==1:
  19.                 self.y+=1
  20.             elif self.direction[1]=='y' and self.direction[0]==-1:
  21.                 self.y-=1
  22.             self.energy-=1
  23.         
  24. class Fish:
  25.     def __init__(self):
  26.         self.x=random.randint(1,10)
  27.         self.y=random.randint(1,10)
  28.         i=random.randint(0,3)
  29.         self.direction=[[1,'x'],[1,'y'],[-1,'x'],[-1,'y']][i]
  30.     def move(self):
  31.         if self.x==10 or self.y==10 or self.x==0 or self.y==0:
  32.             self.direction[0]=-1*self.direction[0]
  33.         if self.direction[1]=='x' and self.direction[0]==1:
  34.             self.x+=1
  35.         elif self.direction[1]=='x' and self.direction[0]==-1:
  36.             self.x-=1
  37.         elif self.direction[1]=='y' and self.direction[0]==1:
  38.             self.y+=1
  39.         elif self.direction[1]=='y' and self.direction[0]==-1:
  40.             self.y-=1
  41.             
  42. def game():
  43.     fish=[]
  44.     wugui=Turtle()
  45.     for i in range(10):
  46.         fish.append(Fish())
  47.     while 1:
  48.         wugui.move()
  49.         print(wugui.x,wugui.y)
  50.         for each in fish:
  51.             each.move()
  52.         for each in fish:
  53.             if each.x==wugui.x and each.y==wugui.y:
  54.                 fish.remove(each)
  55.                 print('有一条鱼被吃掉了')
  56.                 wugui.energy+=20
  57.         if fish==[]:
  58.             print('wuguiasdsssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss胜利')
  59.             break
  60.         elif wugui.energy<=0:
  61.             print('yu胜利')
  62.             break
  63.             
  64. while 1:
  65.     input('回车继续')
  66.     game()        

复制代码

这是我改了后的,
这题有个bug就是可能有鱼和乌龟一直都平行跑。乌龟就不可能赢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-9 15:03:22 | 显示全部楼层    本楼为最佳答案   
你这个代码看似很整齐,想法也很好,但是细节处的逻辑简直了,我都没办法条条框框列出来
首先,你想要做到的限制的活动范围为0-10,0-10都没有做到,不相信你可以自己print出来看看fish的坐标
其次你的if 。。。elif 判断简直毁了你整段代码,移动是这段代码的核心啊
最后,乌龟的体力到底是初始化只有100 还是整个游戏中都不超过100,你要想清楚了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-10-12 03:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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