马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
[b]乌龟和鱼的游戏故事[/b]
假设游戏场景为范围(x, y)为0<=x<=10,0<=y<=10
游戏生成1只乌龟和10条鱼
它们的移动方向均随机
乌龟的最大移动能力是2(Ta可以随机选择1还是2移动),鱼儿的最大移动能力是1
游戏界面:
import random
import time
import winsound
class Turtle:
def __init__(self):
self.x =random.randint(0,9)
self.y =random.randint(0,9)
self.worth = 100
def move(self):
direction =random.randint(1,4)#1 代表向左;2代表向右;3代表向上;4代表向下
step =random.randint(1,2) #移动距离
if direction == 1:
self.x -=step
if self.x<0:
self.x *=-1
elif direction ==2:
self.x +=step
if self.x>=10:
self.x -=10
elif direction ==3:
self.y +=step
if self.y>=10:
self.y -=10
else:
self.y -=step
if self.y<0:
self.y *=-1
self.worth -=1
def eatfish(self):
self.worth += 20
return self.worth
def getposition(self):
#print(self.x,self.y,"in Turtle")
return [self.x,self.y]
class Fish:
def __init__(self):
self.x =random.randint(0,9)
self.y =random.randint(0,9)
self.worth = 20
def move(self):
direction =random.randint(1,4)#1 代表向左;2代表向右;3代表向上;4代表向下
step = 1 #移动距离
if direction == 1:
self.x -=step
if self.x<0:
self.x *=-1
elif direction ==2:
self.x +=step
if self.x>=10:
self.x -=10
elif direction ==3:
self.y +=step
if self.y>=10:
self.y -=10
else:
self.y -=step
if self.y<0:
self.y *=-1
def getposition(self):
#print(self.x,self.y,"in Fish")
return [self.x,self.y]
def destroyed(self):
self.worth = 0
class Pool:
#turtle1 =Turtle()#生成乌龟1只
def __init__(self):
self.turtle1 = Turtle()
self.listFish = [] #生成鱼群---10只
for i in range(10):
self.listFish.append(Fish())
self.countTur =1
self.countFish =10
self.lines = [] #记录输出界面
def judge(self): #游戏结束返回False,否则返回True
for fish in self.listFish:
if self.turtle1.getposition() == fish.getposition() and fish.worth>0:
self.turtle1.eatfish()
fish.destroyed()
self.countFish -=1
winsound.Beep(600,50)#吃上鱼发出告警声 50毫秒时间
if (self.countFish == 0 or self.countTur == 0):
return False
else:
return True
def initPanel(self):
self.lines = []
self.lines.append(" 0 1 2 3 4 5 6 7 8 9 ")#行号0
self.lines.append(" |------------------------------")#行号1
self.lines.append("0| | | | | | | | | | | 0")#行号2
self.lines.append(" |-----------------------------|")#行号3
self.lines.append("1| | | | | | | | | | | 1")#行号4
self.lines.append(" |-----------------------------|")#行号5
self.lines.append("2| | | | | | | | | | | 2")#行号6
self.lines.append(" |-----------------------------|")#行号7
self.lines.append("3| | | | | | | | | | | 3")#行号8
self.lines.append(" |-----------------------------|")#行号9
self.lines.append("4| | | | | | | | | | | 4")#行号10
self.lines.append(" |-----------------------------|")#行号11
self.lines.append("5| | | | | | | | | | | 5")#行号12
self.lines.append(" |-----------------------------|")#行号13
self.lines.append("6| | | | | | | | | | | 6")#行号14
self.lines.append(" |-----------------------------|")#行号15
self.lines.append("7| | | | | | | | | | | 7")#行号16
self.lines.append(" |-----------------------------|")#行号17
self.lines.append("8| | | | | | | | | | | 8")#行号18
self.lines.append(" |-----------------------------|")#行号19
self.lines.append("9| | | | | | | | | | | 9")#行号20
self.lines.append("1|--------------------------------------------------------------------------------|")#行号21
self.lines.append("2|鱼 |鱼 |鱼 |鱼 |鱼 |鱼 |鱼 |鱼 |鱼 |鱼 |")#行号22
self.lines.append("3|1 |2 |3 |4 |5 |6 |7 |8 |9 |10 |")#行号23
self.lines.append("4|--------------------------------------------------------------------------------|")#行号24
self.lines.append("5| | | | | | | | | | | ")#行号25 鱼的生命值
self.lines.append("6|--------------------------------------------------------------------------------| ")#行号26
self.lines.append("7| | | | | | | | | | | ")#行号27 鱼的位置
self.lines.append("8|--------------------------------------------------------------------------------|")#行号28
self.lines.append("9|乌龟X生命值及位置: |")#行号29
self.lines.append("0|--------------------------------------------------------------------------------|")#行号30
self.lines.append("9|活乌龟数量: |活鱼数量: |")#行号31
def display(self):
self.lines = []
self.initPanel()
#显示鱼的位置,及生命值,活鱼用#表示,死鱼用.表示
for i in range(10):
self.lines[25]=self.lines[25][0:3+8*i]+str(self.listFish[i].worth)+self.lines[25][3+8*i+len(str(self.listFish[i].worth)):]#生命值
posXy=str(self.listFish[i].x)+":"+str(self.listFish[i].y)
self.lines[27]=self.lines[27][0:3+8*i]+posXy+self.lines[27][3+8*i+len(posXy):]#X:Y坐标
#显示鱼的生命值
if self.listFish[i].worth>0:
self.lines[self.listFish[i].x*2+2]=self.lines[self.listFish[i].x*2+2][0:self.listFish[i].y*3+2]+"="+self.lines[self.listFish[i].x*2+2][self.listFish[i].y*3+3:]
#显示活鱼的位置
else:
self.lines[self.listFish[i].x*2+2]=self.lines[self.listFish[i].x*2+2][0:self.listFish[i].y*3+2]+"."+self.lines[self.listFish[i].x*2+2][self.listFish[i].y*3+3:]
#显示死鱼位置
########################################################################
self.lines[self.turtle1.x*2+2]=self.lines[self.turtle1.x*2+2][0:self.turtle1.y*3+2]+"X"+self.lines[self.turtle1.x*2+2][self.turtle1.y*3+3:]
self.lines[29]=self.lines[29][0:14]+str(self.turtle1.worth)+" "+str(self.turtle1.x)+":"+str(self.turtle1.y)+self.lines[29][18+len(str(self.turtle1.worth)):]
#显示乌龟位置及生命值
#############################################################################
self.lines[31]=self.lines[31][0:12]+str(self.countTur)+self.lines[31][13:42]+str(self.countFish)+self.lines[31][42+len(str(self.countFish)):]
#####统一显示
for each in self.lines:
print(each)
def act(self):
if self.turtle1.worth>0:
self.turtle1.move()#乌龟移动
if self.turtle1.worth<=0:#如果乌龟生命值为零 则调整池子中乌龟的数量
self.countTur=0
for fish in self.listFish:#鱼群移动
if fish.worth>0:
fish.move()
result = self.judge() #判断一下乌龟有无吃到鱼群中的鱼,并由此乌龟调整生命值和存活鱼的数量
#池子中如果鱼,或龟的数量有一个为零,则返回False游戏结束,否则返回True
self.display()#显示移动和判定后所有动物(乌龟和鱼)的位置及新的生命值
return result
myPool =Pool()
while True:
result =myPool.act()
if result==False:
break
当移动到场景边缘,自动向反方向移动
乌龟初始化体力为100(上限)
乌龟每移动一次,体力消耗1
当乌龟和鱼坐标重叠,乌龟吃掉鱼,乌龟体力增加20
鱼暂不计算体力
当乌龟体力值为0(挂掉)或者鱼儿的数量为0游戏结束
|