乌龟吃鱼简单游戏-类的应用
Posted python-t
tags:
篇首语:本文由小常识网(cha138.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
以上是关于乌龟吃鱼简单游戏-类的应用的主要内容,如果未能解决你的问题,请参考以下文章