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

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

 

posted @ 2018-07-29 11:16  Iving  阅读(303)  评论(0编辑  收藏  举报