小甲鱼课后习题:乌龟吃小鱼

题目:

image-20220302212910963

文字概述:

  • 假设游戏场景为范围(x, y)为0<=x<=10,0<=y<=10
  • 游戏生成1只乌龟和10条鱼
  • 它们的移动方向均随机
  • 乌龟的最大移动能力是2(Ta可以随机选择1还是2移动),鱼儿的最大移动能力是1
  • 当移动到场景边缘,自动向反方向移动
  • 乌龟初始化体力为100(上限)
  • 乌龟每移动一次,体力消耗1
  • 当乌龟和鱼坐标重叠,乌龟吃掉鱼,乌龟体力增加20
  • 鱼暂不计算体力
  • 当乌龟体力值为0(挂掉)或者鱼儿的数量为0游戏结束

解答:

  1. 小甲鱼解法:
import random as r

legal_x = [0, 10]
legal_y = [0, 10]

class Turtle:
    def __init__(self):
        # 初始体力
        self.power = 100
        # 初始位置随机
        self.x = r.randint(legal_x[0], legal_x[1])
        self.y = r.randint(legal_y[0], legal_y[1])

    def move(self):
        # 随机计算方向并移动到新的位置(x, y)
        new_x = self.x + r.choice([1, 2, -1, -2])
        new_y = self.y + r.choice([1, 2, -1, -2])
        # 检查移动后是否超出场景x轴边界
        if new_x < legal_x[0]:
            self.x = legal_x[0] - (new_x - legal_x[0])
        elif new_x > legal_x[1]:
            self.x = legal_x[1] - (new_x - legal_x[1])
        else:
            self.x = new_x
        # 检查移动后是否超出场景y轴边界
        if new_y < legal_y[0]:
            self.y = legal_y[0] - (new_y - legal_y[0])
        elif new_y > legal_y[1]:
            self.y = legal_y[1] - (new_y - legal_y[1])
        else:
            self.y = new_y        
        # 体力消耗
        self.power -= 1
        # 返回移动后的新位置
        return (self.x, self.y)

    def eat(self):
        self.power += 20
        if self.power > 100:
            self.power = 100

class Fish:
    def __init__(self):
        self.x = r.randint(legal_x[0], legal_x[1])
        self.y = r.randint(legal_y[0], legal_y[1])
        
    def move(self):
        # 随机计算方向并移动到新的位置(x, y)
        new_x = self.x + r.choice([1, -1])
        new_y = self.y + r.choice([1, -1])
        # 检查移动后是否超出场景x轴边界
        if new_x < legal_x[0]:
            self.x = legal_x[0] - (new_x - legal_x[0])
        elif new_x > legal_x[1]:
            self.x = legal_x[1] - (new_x - legal_x[1])
        else:
            self.x = new_x
        # 检查移动后是否超出场景y轴边界
        if new_y < legal_y[0]:
            self.y = legal_y[0] - (new_y - legal_y[0])
        elif new_y > legal_y[1]:
            self.y = legal_y[1] - (new_y - legal_y[1])
        else:
            self.y = new_y
        # 返回移动后的新位置
        return (self.x, self.y)

turtle = Turtle()
fish = []
for i in range(10):
    new_fish = Fish() #生成10个鱼
    fish.append(new_fish)

while True:
    if not len(fish):
        print("鱼儿都吃完了,游戏结束!")
        break
    if not turtle.power:
        print("乌龟体力耗尽,挂掉了!")
        break

    pos = turtle.move()
    # 在迭代器中删除列表元素是非常危险的,经常会出现意想不到的问题,因为迭代器是直接引用列表的数据进行引用
    # 这里我们把列表拷贝给迭代器,然后对原列表进行删除操作就不会有问题了^_^
    for each_fish in fish[:]:
        if each_fish.move() == pos:
            # 鱼儿被吃掉了
            turtle.eat()
            fish.remove(each_fish)
            print("有一条鱼儿被吃掉了...")

  1. 我的解法:
import random as r
#firstly,creating an area 
area_x = [1,10]
area_y = [1,10]

#先看小乌龟
class Tortoise:
    def __init__(self,hp=100):
        self.hp = hp
        #randoming creat a initial site
        self.t_x = r.randint(1,10)
        self.t_y = r.randint(1,10)
    def t_move(self):
        # how t moving route
        new_t_x = self.t_x + r.choice([2,1,-1,-2]) 
        new_t_y = self.t_y + r.choice([2,1,-1,-2]) 
        #you should to check whether t is in area
        # x site
        if new_t_x <= area_x[0]:
            self.t_x = area_x[0] - (new_t_x - area_x[0])
        elif new_t_x >= area_x[1]:
            self.t_x = area_x[1] - (new_t_x - area_x[1])
        else:
            self.t_x = new_t_x
        # y site
        if new_t_y <= area_y[0]:
            self.t_y = area_y[0] - (new_t_y - area_y[0])
        elif new_t_y >= area_y[1]:
            self.t_y = area_y[1] - (new_t_y - area_y[1])
        else:
            self.t_y = new_t_y
        # hp cost
        self.hp -= 1
        #return t site
        return(self.t_x,self.t_y)
    
    def eat(self):
        self.hp += 20
        if self.hp > 100:
            self.hp = 100

class Fish:
    def __init__(self,):
        #randoming creat a initial site
        self.t_x = r.randint(1,10)
        self.t_y = r.randint(1,10)
    def move(self):
        # how t moving route
        new_t_x = self.t_x + r.choice([1,-1]) 
        new_t_y = self.t_y + r.choice([1,-1]) 
        #you should to check whether t is in area
        # x site
        if new_t_x <= area_x[0]:
            self.t_x = area_x[0] - (new_t_x - area_x[0])
        elif new_t_x >= area_x[1]:
            self.t_x = area_x[1] - (new_t_x - area_x[1])
        else:
            self.t_x = new_t_x
        # y site
        if new_t_y <= area_y[0]:
            self.t_y = area_y[0] - (new_t_y - area_y[0])
        elif new_t_y >= area_y[1]:
            self.t_y = area_y[1] - (new_t_y - area_y[1])
        else:
            self.t_y = new_t_y

        return(self.t_x,self.t_y)

#对乌龟进行类的实例化
t = Tortoise()
fish = []
#对鱼进行类的实例化
for i in range(10):
    _10fish = Fish() # 生成10条鱼
    fish.append(_10fish)

#吃鱼
while True:
    t_site = t.t_move()
    for each_fish in fish[:]:
        if each_fish.move() == t_site:
            t.eat()
            fish.remove(each_fish)
            print('一条小鱼被吃了')
    
    if len(fish) == 0:
        print('鱼被吃完了!')
        break
    elif t.hp == 0:
        print('小乌龟累死了!')
        break
'''
这个程序算是入门题,我们要做一个抽象,
首先把乌龟的特征进行抽象:
1.初始化位置,hp(体力)
2.一个技巧就是运动的行为和运动的检测要分开写,不能混为一谈
3.将对象能实例化的都实例化,可以在调用的时候是为一种方法,平常心,认为是简单的程序
'''
posted @ 2022-03-02 21:33  karlsorpan  阅读(153)  评论(0)    收藏  举报