python乌龟吃鱼小游戏(类和对象及Easygui应用)

(此游戏为小甲鱼设计,这里进行一下小的改编)
要求:编写一个乌龟吃鱼小游戏:

游戏场景为范围(x,y)为 0<=x<=10,0<=y<=10

游戏生成1只乌龟和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(1,10)
        self.y=r.randint(1,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
        elif new_x>10:
            self.x=10-(new_x-10)
        else:
            self.x=new_x
        if new_y<0:
            self.y=0-new_y
        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(1,10)
        self.y=r.randint(1,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
        elif new_x>10:
            self.x=10-(new_x-10)
        else:
            self.x=new_x
        if new_y<0:
            self.y=0-new_y
        elif new_y>10:
            self.y=10-(new_y-10)
        else:
            self.y=new_y
        
        return(self.x,self.y)

turtle=Turtle()
fish=[]
for i in range(10):
    new_fish=Fish()
    fish.append(new_fish)

while True:
    if not len(fish):
        print('乌龟把鱼都吃完了,游戏结束')
        break
    if not turtle.power:
        print('乌龟体力耗尽了,游戏结束')
        break
    print('乌龟移动前坐标:(%d,%d)'%(turtle.x ,turtle.y))
    turtle.move()
    print('乌龟移动后坐标:(%d,%d)'%(turtle.x ,turtle.y))
    for num in fish:
        print('小鱼移动前坐标:(%d,%d)'%(num.x ,num.y))
        num.move()
        print('小鱼移动后坐标:(%d,%d)'%(num.x ,num.y))
        if num.x==turtle.x and num.y==turtle.y:
              turtle.eat()
              fish.remove(num)
              print('乌龟吃了一条鱼')
              print('乌龟的体力值为:%d'%turtle.power)

在这里插入图片描述

昨天学习了Easygui的一些方法,这里通过改进程序来应用:(鱼的数量改为了一条)

import random as r
import sys
import easygui as g

class Turtle:
    def __init__(self):
        self.power = 100
        self.x=r.randint(1,10)
        self.y=r.randint(1,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
        elif new_x>10:
            self.x=10-(new_x-10)
        else:
            self.x=new_x
        if new_y<0:
            self.y=0-new_y
        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(1,10)
        self.y=r.randint(1,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
        elif new_x>10:
            self.x=10-(new_x-10)
        else:
            self.x=new_x
        if new_y<0:
            self.y=0-new_y
        elif new_y>10:
            self.y=10-(new_y-10)
        else:
            self.y=new_y
        return(self.x,self.y)
title='乌龟吃鱼小游戏'
text=''
if g.ccbox('下面要开始游戏吗?',title,choices=('开始','退出')):

    
           
    turtle=Turtle()
    fish=[]
    for i in range(1):
        new_fish=Fish()
        fish.append(new_fish)

    while True:
        if not len(fish):
            text+='乌龟把鱼都吃完了,游戏结束\n'
            break
        if not turtle.power:
            text+='乌龟体力耗尽了,游戏结束\n'
            break
        text+='乌龟移动前坐标:(%d,%d)\n'%(turtle.x ,turtle.y)
        turtle.move()
        text+='乌龟移动后坐标:(%d,%d)\n'%(turtle.x ,turtle.y)
        for num in fish:
            text+='小鱼移动前坐标:(%d,%d)\n'%(num.x ,num.y)
            num.move()
            text+='小鱼移动后坐标:(%d,%d)\n'%(num.x ,num.y)
            if num.x==turtle.x and num.y==turtle.y:
                  turtle.eat()
                  fish.remove(num)
                  text+='乌龟吃了一条鱼\n'
                  text+='乌龟的体力值为:%d\n'%turtle.power
    g.textbox('游戏开始',title,text)

在这里插入图片描述
小乌龟太可怜了,总是吃不到鱼,这里我们把鱼也设置体力为10,移动一次体力减1,选择让小乌龟再来吃鱼:
改了的部分:

class Fish:
    def __init__(self):
        self.power = 10
        self.x=r.randint(1,10)
        self.y=r.randint(1,10)
    def move(self):
        if self.power:
            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
            elif new_x>10:
                self.x=10-(new_x-10)
            else:
                self.x=new_x
            if new_y<0:
                self.y=0-new_y
            elif new_y>10:
                self.y=10-(new_y-10)
            else:
                self.y=new_y
            self.power-=1
在这里插入代码片

在这里插入图片描述
学习更多基本python知识可点击主页关注,一起学习!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值