编程语言
首页 > 编程语言> > Python第五课----一个关于进化的Python游戏

Python第五课----一个关于进化的Python游戏

作者:互联网

目录

1. 概述

2.游戏设定

3.程序编写

3.1 import导入

3.2 无颚鱼类的建立

3.3 草履虫类的建立

3.3 主循环的建立

3.4 程序运行结果

3.4.1 当把边界位置设定为[0,4]时

3.4.2 当把边界位置设定为[0,8]时

3.4.3 当把边界位置设定为[0,10]时

4. 小结


1. 概述

中年大叔在学习了一周Python后,决定做一个关于进化的小游戏,游戏用到的知识比较简单,使用的技术包括:

1. import 导入随机数模块:

2. 面向对象编程:

2.游戏设定

游戏的设定如下:

1. 假设有一片原始海洋,原始海洋的大小为10*10

2. 游戏首先设定2个角色,分别是10只草履虫和1只无颚鱼

3. 鱼和草履虫都可以向4个方向随机移动

4. 无鄂虫的最大移动距离为2(每次移动1或者2),草履虫的移动能力为1

5. 当两种生物移动到边缘时,自动向反方向移动

6. 无颚虫的体力初始值为100,草履虫体力无限;

7. 当无颚虫移动时,每移动一次,体力消耗1(无论移动1还是2)

8. 当无额虫吃了草履虫时,体力加20;

9. 当无颚虫体力为0或者草履虫数量为0时,游戏结束。

3.程序编写

3.1 import导入

# 导入random类
import random as rd

3.2 无颚鱼类的建立

# 建立一个无颚虫的模型,并对其进行初始化
X_max = 8
X_min = 0
Y_max = 8
Y_min = 0
class WEC:
    def __init__(self):
        # 位置初始化
        self.posx = rd.randint(X_min,X_max)
        self.posy = rd.randint(Y_min,Y_max)
        # 能量初始化
        self.power = 100
    def move(self):
        new_x = self.posx
        new_y = self.posy
        axis_choice = rd.choice([0,1])
        if axis_choice == 0:
            new_x = self.posx + rd.choice([1,2,-1,-2])
        elif axis_choice == 1:
            new_y = self.posy + rd.choice([1,2,-1,-2])
        else:    
            print('无额虫的移动方向错误\n')
        # 辨识是否已经到达边界
        if new_x >= X_max:
            new_x = X_max 
        elif new_x <=0:
            new_x = 0 
        if new_y >= Y_max:
            new_y = 10 
        elif new_y <=Y_min:
            new_y = 0 
        self.power -= 1
        return(new_x,new_y)
    def eat(self):
        self.power += 20
        if self.power >=100:
            self.power = 100
        return self.power

3.3 草履虫类的建立

# 建立一个草履虫的模型,并对其进行初始化
class CaoLvChong:
    def __init__(self):
    # 位置初始化
        self.posx = rd.randint(X_min,X_max)
        self.posy = rd.randint(Y_min,Y_max)
    def move(self):
        new_x = self.posx
        new_y = self.posy
        axis_choice = rd.choice([0,1])
        if axis_choice == 0:
            new_x = self.posx + rd.choice([1,-1])
        elif axis_choice == 1:
            new_y = self.posy + rd.choice([1,-1])
        else:    
            print('无额虫的移动方向错误\n')
        # 辨识是否已经到达边界
        if new_x >= X_max:
            new_x = X_max 
        elif new_x <=X_min:
            new_x = X_min
        if new_y >= Y_max:
            new_y = Y_max 
        elif new_x <=Y_min:
            new_y = Y_min 
        return(new_x,new_y)

3.3 主循环的建立

# 实例化一个无颚虫模型
fish = WEC()
# 实例化10个草履虫
clc  = []
for i in range(10):
    clc.append(CaoLvChong())
count = 0 
while True:
    count = count + 10
    # 如果草履虫都被吃光了
    if not len(clc):
        print('草履虫被吃光了 !!!!')
        break
    # 如果鱼的体力光了
    if not fish.power:
        print('体力耗尽!!!')
        break
    fish_pos = fish.move()
    for each_clc in clc[:]:
        if each_clc.move() == fish_pos :
            fish.eat()
            clc.remove(each_clc)
            print('一个虫子被吃掉了')
print('可怜的无额鱼一共走了',count,'步')

3.4 程序运行结果

3.4.1 当把边界位置设定为[0,4]时

3.4.2 当把边界位置设定为[0,8]时

3.4.3 当把边界位置设定为[0,10]时

4. 小结

本文主要将前期学习的知识进行了一个串联,通过建立一个简单的进化游戏对内容进行了复习,相关数据供大家批判指正。

 

标签:rd,Python,max,self,第五课,choice,----,草履虫,new
来源: https://blog.csdn.net/baiyou1987/article/details/114221423