假设游戏场景为范围(x,y)为0<=x<=10,0<=y<=10:
1.游戏生成1只乌龟和10条鱼,它们的移动方向均随机
2.乌龟的最大移动能力是2(可以随机选择1还是2移动),鱼儿的最大移动能力是1
3.当移动到场景边缘,自动向反方向移动
4.乌龟初始化体力为100(上限)
5.乌龟每移动一次,体力消耗1
6.当乌龟和鱼坐标重叠,乌龟吃掉鱼,乌龟体力增加20
7.鱼暂时不计算体力
8.当乌龟体力值为0或者鱼儿的数量为0游戏结束
一、定义大鱼(乌龟)类
class Turtle:
#初始化乌龟的属性:体力、位置
def __init__(self):
self.power=100
self.x=random.randint(0,10)
self.y=random.randint(0,10)
def move(self):
#乌龟随机移动一次,位置的变动
self.x+=random.choice([1,2,-1,-2])
self.y+=random.choice([1,2,-1,-2])
#判断是否越界
if self.x<0:
self.x=0-self.x
if self.x>10:
self.x=10-(self.x-10)
if self.y<0:
self.y=0-self.y
if self.y>10:
self.y=10-(self.y-10)
#移动一次消耗体力1
self.power-=1
#返回乌龟的位置
return (self.x,self.y)
二、定义小鱼类
class Fish:
#初始化
def __init__(self):
self.x=random.randint(0,10)
self.y=random.randint(0,10)
def move(self):
#小鱼每移动一次,返回的位置坐标
self.x+=random.choice([1,-1])
self.y+=random.choice([1,-1])
#判断是否越界
if self.x<0:
self.x=0-self.x
if self.x>10:
self.x=10-(self.x-10)
if self.y<0:
self.y=0-self.y
if self.y>10:
self.y=10-(self.y-10)
return (self.x,self.y)
三、主逻辑turtle=Turtle()
turtle=Turtle() #实例化对象,生成一只乌龟
fish=[] #创建一个列表,用于存放生成的十条小鱼
for i in range(10):
new_fish=Fish()
fish.append(new_fish)
while True:
turtle_moves=turtle.move()
#把列表拷贝给迭代器,然后在判断乌龟、小鱼位置是否重叠的时候用于删除原列表。因为不拷贝的情况下,迭代器删除元素
会出现意想不到的问题,因为迭代器是直接引用列表的数据进行引用
for each_fish in fish[:]:
fish_moves=each_fish.move()
if turtle_moves==fish_moves:
turtle.power+=20
fish.remove(each_fish)
print (str((turtle.x,turtle.y))+"------------------"+"The turtle eat the fish!!!")
if turtle.power>100:
turtle.power=100
if turtle.power==0:
print ("The turtlr has no power~!!!")
print ("And the game is over!!!")
break
if len(fish)==0:
print ("There are no fish!!")
print ("And the game is over!!!")
break
优化:如果乌龟、小鱼每次移动只能移动一个方向:(在类里边确认移动位置前追加判断是往哪个方向移动)#encoding:utf-8
import random
'''
假设游戏场景为范围(x,y)为 0<=x<=10,0<=y<=10
游戏生成1只乌龟和10条鱼
它们的移动方向均随机
乌龟的最大移动能力是2(可以随机选择1还是2移动),鱼儿的最大移动能力是1
当移动到场景边缘,自动向反方向移动
乌龟初始化体力为100(上限)
乌龟每移动一次,体力消耗1
当乌龟和鱼坐标重叠,乌龟吃掉鱼,乌龟体力增加20
鱼暂时不计算体力
当乌龟体力值为0或者鱼儿的数量为0游戏结束
'''
class Turtle:
def __init__(self):
self.power=100
self.x=random.randint(0,10)
self.y=random.randint(0,10)
def move(self):
#默认前进方向为左右,当随机移动位置为0,即前进方向为上下
r=random.choice([1,2,0,-1,-2])
if r==0:
self.y+=random.choice([1,2,-1,-2])
else:
self.x+=r
if self.x<0:
self.x=0-self.x
if self.x>10:
self.x=10-(self.x-10)
if self.y<0:
self.y=0-self.y
if self.y>10:
self.y=10-(self.y-10)
self.power-=1
return (self.x,self.y)
class Fish:
def __init__(self):
self.x=random.randint(0,10)
self.y=random.randint(0,10)
def move(self):
r=random.choice([1,0,-1])
if r==0:
self.y+=random.choice([1,-1])
else:
self.x+=r
if self.x<0:
self.x=0-self.x
if self.x>10:
self.x=10-(self.x-10)
if self.y<0:
self.y=0-self.y
if self.y>10:
self.y=10-(self.y-10)
return (self.x,self.y)
turtle=Turtle()
fish=[]
for i in range(10):
new_fish=Fish()
fish.append(new_fish)
while True:
turtle_moves=turtle.move()
for each_fish in fish[:]:
fish_moves=each_fish.move()
if turtle_moves==fish_moves:
turtle.power+=20
fish.remove(each_fish)
print (str((turtle.x,turtle.y))+"------------------"+"The turtle eat the fish!!!")
if turtle.power>100:
turtle.power=100
if turtle.power==0:
print ("The turtlr has no power~!!!")
print ("And the game is over!!!")
break
if len(fish)==0:
print ("There are no fish!!")
print ("And the game is over!!!")
break