Define a fish and turtle and write a game

* Suppose the game scene is a range (x, y) by 0<=x<=10,0<=y<=10
* Game generation 1 A turtle and 10 Fish
* They move in random directions
* What is the turtle's maximum mobility 2(Ta You can choose at random 1 still 2 move ), What is a fish's maximum mobility 1
* When moving to the edge of the scene , Automatically move in the opposite direction
* Tortoise initialization strength is 100( upper limit )
* Every time the tortoise moves , Physical exertion 1
* When the coordinates of the turtle and the fish overlap , Turtles eat fish , Tortoise strength increases 20
* Fish do not calculate physical strength for the time being import random as r old_x=[0,10] old_y=[0,10] class Turtle: def
__init__(self): # Each definition should be used in the front self distinguish self.power=100 # Random initial position ,( Attention call randint Before r)
#???? Question why old_x[1],new_x[1] ##### answer : I smoked melon seeds , Look at the index value of the list 1 representative 10
self.x=r.randint(old_x[0],old_x[1]) self.y=r.randint(old_y[0],old_y[1]) def
move(self): # The tortoise is moving 1 or 2, Random movement , Therefore, using list definition is simpler than generating random numbers #choice() From a list , A tuple or string returns a random item
new_x=self.x+r.choice([-1,-2,1,2]) new_y=self.y+r.choice([1,2,-1,-2])
# Lateral movement does not exceed the boundary
# About why old_x[0] and old_x[10] Thinking about making borders instead of numeric borders : This game is defined in the storage space where the list is located , Simple values are not in the same storage space as the values in the list
if new_x>old_x[1]: self.x=old_x[1]-(new_x-old_x[1]) elif new_x<old_x[0]:
self.x=old_x[0]-(new_x-old_x[0]) else: self.x=new_x #
new_x=old_x Something went wrong , The original last position is self.x, Call function self.x # The ordinate does not exceed the boundary, which is the same as the abscissa if
new_y>old_y[1]: self.y=old_y[1]-(new_y-old_y[1]) elif new_y<old_y[0]:
self.y=old_y[0]-(new_y-old_y[0]) 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
# The movement of fish is roughly similar to that of tortoise class Fish: #class Lowercase def __init__(self):
self.x=r.randint(old_x[0],old_x[1]) self.y=r.randint(old_y[0],old_y[1]) def
move(self): new_x=self.x+r.choice([-1,1]) new_y=self.y+r.choice([1,-1,]) if
new_x>old_x[1]: self.x=old_x[1]-(new_x-old_x[1]) elif new_x<old_x[0]:
self.x=old_x[0]-(new_x-old_x[0]) else: self.x=new_x if new_y>old_y[1]:
self.y=old_y[1]-(new_y-old_y[1]) elif new_y<old_y[0]:
self.y=old_y[0]-(new_y-old_y[0]) else: self.y=new_y return (self.x,self.y)
turtle=Turtle() # Define an empty list fish=[] for i in range(10): # Defined 10 Fish new_fish=Fish()
fish.append(new_fish) # Finally, add the coordinates of ten fish to the empty list while True: # Again ,True Be sure to capitalize if
not len(fish): print(" The fish is finished , game over !") break if not turtle.power:
print(" The tortoise is exhausted , Hang up ") break # Eventually, some fish will be eaten pos=turtle.move() #
Deleting list elements in iterators is very dangerous , Unexpected problems often arise , Because iterators refer directly to the data of the list #
Here we copy the list to the iterator , Then delete the original list and there will be no problem for each_fish in fish[:]: #fish[:] Contains all the lists
#each_fish 10 Any one of them if each_fish.move()==pos: # Fish and tortoise are in the same position turtle.eat()
fish.remove(each_fish) # Delete this fish print(" A fish was eaten ")

Technology