码迷,mamicode.com
首页 > 其他好文 > 详细

乌龟吃鱼简单游戏-类的应用

时间:2018-07-29 13:07:36      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:super   while   style   bre   游戏   span   color   tle   .com   

技术分享图片

class Base(object):
    def __init__(self,x,y,a):
        self.x=x
        self.y=y
        self.flag=1
        self.a=a
    def move(self,a):
        if self.flag==1:
            self.x+=a
            self.y+=a-1
        else:
            self.x-=a
            self.y -= a-1
        if self.x>=10 or self.y>=10:
            self.flag=-1
        elif self.x<=0 or self.x<=0:
            self.flag=1
        # print("鱼坐标:(%d,%d)" % (self.x, self.y))
class Turtle(Base):
    def __init__(self,x,y,num,enemy,a):
        self.num=num
        self.enemy=enemy
        super(Turtle,self).__init__(x,y,a)
        print("乌龟数量:%d"%self.num)
        print("乌龟坐标:(%d,%d)"%(self.x,self.y))
    def move(self,a):
        if self.flag==1:
            self.x+=a
            self.y += a-1
        else:
            self.x-=a
            self.y -= a-1
        if self.x>=10 or self.y>=10:
            self.flag=-1
        elif self.x<=0 or self.y<=0:
            self.flag=1
        self.enemy-=1
        print("乌龟坐标:(%d,%d),能量:%d,"%(self.x,self.y,self.enemy))
def get(turtle,fish):
    if turtle.enemy<=90:
        if turtle.x==fish.x and turtle.y==fish.y:
            turtle.enemy+=10
            fish.num-=1
            print("鱼数量:%d"%(fish.num))
            print("乌龟能量:%d," % (turtle.enemy))
            if fish.num==0:
                exit()
class Fish(Base):
    def __init__(self,x,y,a,num):
        self.num = num
        self.a=a
        super(Fish,self).__init__(x,y,a)
        print("鱼数量:%d"%(self.num))
        print("鱼坐标:(%d,%d)" % (self.x,self.y))
turtle=Turtle(4,4,1,100,2)
fish=Fish(2,2,3,10)
amount=1
while True:
    turtle.move(2)
    fish.move(3)
    print("鱼坐标:(%d,%d)" % (fish.x, fish.y))
    get(turtle, fish)
    amount+=1
    if amount==1000:
        break

 

乌龟吃鱼简单游戏-类的应用

标签:super   while   style   bre   游戏   span   color   tle   .com   

原文地址:https://www.cnblogs.com/Python-T/p/9384713.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!