Python小游戏大鱼吃小鱼

假设游戏场景为范围(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
 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值