I feel that the reaction between zombies and Tetris written last time is pretty good , This article is more dynamic
Write a cool run every day this time

That's the effect picture written out
Let's update all the code
Or do you define it first
import pygame,sys import random
Write the game configuration
width = 1200 # Window width height = 508 # Window height size = width, height score=None # fraction
myFont=myFont1=None # typeface surObject=None # Obstacle picture surGameOver=None # Game end picture bg=None
# Background object role=None # Character object object=None # Obstacle Object objectList=[] # Obstacle Object array clock=None # Clock
gameState=None # Game status (0,1) express ( In game , game over )
Write characters
class Role: # character def __init__(self,surface=None,y=None): self.surface=surface
self.y=y self.w=(surface.get_width())/12 self.h=surface.get_height()/2 self.
currentFrame=-1 self.state=0 #0 Represents running status ,1 Represents the jump state ,2 Represents a continuous jump self.g=1 # Gravitational acceleration self.vy=0
#y Shaft speed self.vy_start=-20 # Take off start speed def getRect(self): return (0,self.y+12,self.w,
Writing obstacles
class Object: # obstacle def __init__(self,surface,x=0,y=0): self.surface=surface
self.x=x self.y=y self.w=surface.get_width() self.h=surface.get_height() self.
currentFrame=random.randint(0,6) self.w = 100 self.h = 100 def getRect(self):
return (self.x,self.y,self.w,self.h) def collision(self,rect1,rect2): # collision detection if (
rect2[0]>=rect1[2]-20) or (rect1[0]+40>=rect2[2])or (rect1[1]+rect1[3]<rect2[1]+
20) or (rect2[1]+rect2[3]<rect1[1]+20): return False return True
Write background
class Bg: # background def __init__(self,surface): self.surface=surface self.dx=-10 self
.w=surface.get_width() self.rect=surface.get_rect() def initGame(): global bg,
# Fraction initialization score=0 # initialization objectList=[] # Load font myFont=pygame.font.Font(
"./freesansbold.ttf",32) myFont1=pygame.font.Font("./freesansbold.ttf",64) #
Create clock object ( You can control the game cycle frequency ) clock = pygame.time.Clock() # Initialize game state gameState=0 # Game background surBg
=pygame.image.load("image/bg.bmp").convert_alpha() bg=Bg(surBg) # End screen
surGameOver=pygame.image.load("image/gameover.bmp").convert_alpha() # Images of people
surRole=pygame.image.load("image/role.png").convert_alpha() role=Role(surRole,
508-85) # Obstacle picture surObject=pygame.image.load("image/object.png").convert_alpha()
def addObject(): global surObject,object,objectList,object rate=4 # Whether obstacles are generated if
not random.randint(0,300)<rate: return y=random.choice([height-100,height-200,
height-300,height-400]) object=Object(surObject,width+40,y) objectList.append(
object) def updateLogic(): global gameState,score # Keyboard event handling for event in pygame.
event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type==pygame.
KEYDOWN: # Space bar jump if gameState==0: if event.key==pygame.K_SPACE: if role.state==0:
role.state=1 role.vy=role.vy_start elif role.state==1: role.state=2 role.vy=
role.vy_start elif gameState==1: if event.key==pygame.K_SPACE: # Restart the game initGame(
) if gameState==0: # Background movement bg.dx+=10 if bg.dx==1200: bg.dx=0 # Character movement if role.
state==0: role.currentFrame+=1 if role.currentFrame==12: role.currentFrame=0
else: role.y+=role.vy role.vy+=role.g if role.y>=508-85: role.y=508-85 role.
state=0 # Movement of obstacles addObject() for object in objectList: object.x-=10 # Obstacle movement #
Obstacle out of screen , Remove obstacles if object.x+object.w<=0: objectList.remove(object) score+=10
# Avoid obstacles , plus 10 branch print(" Removed a target ") # collision detection if object.collision(role.getRect(),object.
getRect()): if(object.currentFrame==6): objectList.remove(object) score+=100
# Eat gold coins and 100 branch print(score) print(" Ate a gold coin ") else: gameState=1 # Game failure print(" There was a collision !")
ok la , This is all the code for this cool run every day , You can leave a message if you have any questions , I'll come back when I see it .