乌龟吃鱼类 -python

""" 
游戏编程:按照要求定义一个乌龟类和鱼类并尝试编程:
    - 假设游戏的场景范围(x,y)为  0<= x<= 10   0<= y<= 10
    - 游戏生成一只乌龟和10条鱼 
    - 他们的移动方向均为随机
    - 乌龟的最大移动能力为2(乌龟可以选择移动是1还是2),鱼的最大移动能力为1
    - 当移动到场景边缘,自动向反方向移动
    - 乌龟初始化体力为100(上限)
    - 乌龟每移动一次,体力消耗1
    - 当乌龟和鱼重叠时,乌龟吃掉鱼,乌龟的体力增加20
    - 鱼不计算体力
    - 当乌龟体力值为0,或者与的数量为0时,游戏结束
 """
import random as r
class Turtle():
    def __init__(self):
        self.power = 100

        self.x = r.randint(0, 10)
        self.y = r.randint(0, 10) 
    def move(self):
        new_x = self.x + r.choice([1,2,-1,-2])
        new_y = self.y + r.choice([1,2,-1,-2])

        if new_x < 0:
            self.x = 0 - (new_x - 0)
        elif new_x >10:
            self.x = 10 - (new_x - 10)
        else:
            self.x = new_x

        if new_y < 0:
            self.y = 0 - (new_y - 0)
        elif new_y >10:
            self.y = 10 - (new_y - 10)
        else:
            self.y = new_y
        self.power -= 1
        return (self.x, self.y)
    def eat(self):
        self.power += 20
        if self.power > 100:
            self.power = 100
class Fish():
    def __init__(self):
        self.x = r.randint(0, 10)
        self.y = r.randint(0, 10) 
    def move(self):
        new_x = self.x + r.choice([1,-1])
        new_y = self.y + r.choice([1,-1])
    
        if new_x < 0:
            self.x = 0 - (new_x - 0)
        elif new_x >10:
            self.x = 10 - (new_x - 10)
        else:
            self.x = new_x

        if new_y < 0:
            self.y = 0 - (new_y - 0)
        elif new_y >10:
            self.y = 10 - (new_y - 10)
        else:
            self.y = new_y
        return (self.x,self.y)
turple = Turtle()
fish = []
print("=" *20)
print("游戏开始")
for i in range(10):
    new_fish = Fish()
    fish.append(new_fish)
fish_num = 10
while True:
    if not turple.power:
        print("乌龟没有体力了!游戏结束!")
        break
    if not len(fish):
        print("小鱼被吃完了!")
        break
    for i in fish[:]:
        
        if i.move() == turple.move():
            turple.eat()
            fish.remove(i)
            fish_num -= 1
            print("小乌龟遇见了小鱼,吧唧,吃掉一条,小鱼现在还有{}条".format(fish_num))


  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

影修

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值