|
发表于 2018-7-27 15:29:39
|
显示全部楼层
本帖最后由 无符号整形 于 2018-7-27 15:33 编辑
1.你的理解差不多是对滴~~~ ,但是不会发生这种情况(只要有鱼被抓住就会gameover)
2.不好意思,是我看错了~~没仔细看move实现
假如要让全部鱼实现死掉再gameover的话要用not and,那么这个代码还少了一个就是鱼不会死亡,导致鱼被抓住后还会移动。
顺便帮你改进了下:
- import random as r
- class Fish:
- def __init__ (self):
- self.x=r.randint(0,0)
- self.y=r.randint(0,10)
- self.dead = False
- def move(self):
- if not self.dead:
- i=r.choice([0,1])
- if i==0:
- self.x=self.x+r.choice([1,-1])
- elif i==1:
- self.y=self.y+r.choice([1,-1])
- if self.x > 5.5:
- self.x = 5
- elif self.y > 5.5:
- self.y = 5
- elif self.y < -5.5:
- self.y = -5
- elif self.y < -5.5:
- self.y = -5
- def kill(self):
- self.dead = True
- fish=[Fish(),Fish(),Fish()]
- turtle=Fish()
- while not (fish[0].dead and fish[1].dead and fish[2].dead):
- fish[0].move()
- fish[1].move()
- fish[2].move()
- turtle.move()
- count = 0 #送代次数
- print('fish1说:我在('+str(fish[0].x)+','+str(fish[0].y)+')') if not fish[0].dead else 0
- print('fish2说:我在('+str(fish[1].x)+','+str(fish[1].y)+')') if not fish[1].dead else 0
- print('fish3说:我在('+str(fish[2].x)+','+str(fish[2].y)+')') if not fish[2].dead else 0
- print('turtle说:我在('+str(turtle.x)+','+str(turtle.y)+')')
- for eachfish in fish:
- if eachfish.x == turtle.x and eachfish.y == turtle.y and not eachfish.dead:
- fish[count].kill()
- print('turtle说:哈哈哈fish%d抓到你啦' % (count+1))
- count+=1
- print('Gameover')
复制代码
输出:
fish1说:我在(1,5)
fish2说:我在(0,5)
fish3说:我在(0,5)
turtle说:我在(0,5)
turtle说:哈哈哈fish2抓到你啦
turtle说:哈哈哈fish3抓到你啦
fish1说:我在(2,5)
turtle说:我在(0,5)
fish1说:我在(1,5)
turtle说:我在(1,5)
turtle说:哈哈哈fish1抓到你啦
Gameover
>>> |
|