# Python lesson 5 - a python game about evolution

catalogue

1. General

2. Game settings

3. Programming

3.1 import

3.2 establishment of jawless fish

3.3 establishment of Paramecium

3.3 establishment of main cycle

3.4} program operation results

3.4.1 when the boundary position is set to [0,4]

3.4.2 when the boundary position is set to [0,8]

3.4.3 when the boundary position is set to [0,10]

4. Summary

# 1. General

After learning Python for a week, the middle-aged uncle decided to make a small game about evolution. The knowledge used in the game is relatively simple, and the technologies used include:

1. import random number module:

• The random number of [min,max] is obtained by randint;
• Use choice to get a random result of choice([c1,c2,c3]).

2. Object oriented programming:

• Use class
• Using the def function

# 2. Game settings

The game settings are as follows:

1. Suppose there is a primitive ocean, the size of which is 10 * 10

2. The game first sets up two characters, namely 10 Paramecium and 1 jawless fish

3. Both fish and Paramecium can move randomly in four directions

4. The maximum moving distance of no Hubei worm is 2 (1 or 2 for each movement), and the moving ability of Paramecium is 1

5. When two creatures move to the edge, they automatically move in the opposite direction

6. The initial value of physical strength of jawless insects is 100, and the physical strength of Paramecium is unlimited;

7. When there is no jaw worm moving, physical exertion will be 1 (no matter moving 1 or 2) for each movement

8. When the frontless worm eats Paramecium, the physical strength is increased by 20;

9. When the jawless strength is 0 or the number of Paramecium is 0, the game ends.

# 3. Programming

## 3.1 import

```# Import random class
import random as rd```

## 3.2 establishment of jawless fish

• First, establish the constructor, and initialize the position x, y and power in the constructor
• Establish the move function, move randomly in x or y direction, and move 1, 2, - 1 or - 2 each time; And judge whether it reaches the boundary; Every time you move, you will lose 1 point of physical strength
• The eat function is established. Once the Paramecium is eaten, the physical strength is increased by 20
```# Establish a jawbone free model and initialize it
X_max = 8
X_min = 0
Y_max = 8
Y_min = 0
class WEC:
def __init__(self):
# Location initialization
self.posx = rd.randint(X_min,X_max)
self.posy = rd.randint(Y_min,Y_max)
# Energy initialization
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('The frontless insect moves in the wrong direction\n')
# Identify whether the boundary has been reached
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 establishment of Paramecium

• First, establish the constructor, and initialize the position x and position y in the constructor
• Establish the move function, move randomly in x or y direction, and move 1 or - 1 each time; And judge whether it reaches the boundary
```# Establish a paramecium model and initialize it
class CaoLvChong:
def __init__(self):
# Location initialization
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('The frontless insect moves in the wrong direction\n')
# Identify whether the boundary has been reached
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 establishment of main cycle

• First, instantiate the jawless fish object;
• Instantiate 10 Paramecium objects and form a queue
• Establish a main loop. In the main loop, judge the jump conditions of the loop:
• Condition 1: Paramecium queue is empty, that is, all Paramecium are eaten;
• Condition 2: jawless fish exhausted
• After each cycle, judge once in the cycle whether the position of Paramecium is the same as that of jawless fish. If it is the same, it means that it is eaten by jawless fish, and the fish corresponding to this position is deleted from the queue
• Print results
```# Instantiate a jawless model
fish = WEC()
# Instantiate 10 Paramecium
clc  = []
for i in range(10):
clc.append(CaoLvChong())
count = 0
while True:
count = count + 10
# If Paramecium is eaten up
if not len(clc):
print('Paramecium has been eaten up!!!!')
break
# If the fish run out of strength
if not fish.power:
print('Exhausted!!!')
break
fish_pos = fish.move()
for each_clc in clc[:]:
if each_clc.move() == fish_pos :
fish.eat()
clc.remove(each_clc)
print('A bug was eaten')
print('The poor frontless fish left altogether',count,'step')```

# 4. Summary

This paper mainly makes a series connection of the knowledge learned in the early stage, reviews the content by establishing a simple evolutionary game, and provides relevant data for criticism and correction.

Posted by poelinca on Thu, 14 Apr 2022 17:07:52 +0930