I’ve written about plants fighting zombies and Tetris in the past and the reaction was good, but this one has more momentum

Let’s write a cool run this time



And that’s how it came out

Let’s update all the code below

I’m going to define it the same way

import pygame,sys
import random
12
Copy the code

Write the game configuration

Width = 1200 # window width height = 508 # window height size = width, Height score=None # score myFont=myFont1=None # font surObject=None # Obstacle picture surGameOver=None # game end picture bg=None # Background object role=None GameState =None # gameState=None # gameState (0,1) 12345678910111213Copy the code

Write characters

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. g=1 # gravity self.vy=0 #y speed self.vy_start=-20 # def getRect(self): return (0,self.y+12,self.w,self.h) 12345678910111213Copy the code

Write an obstacle

Class Object: 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 rect1 [1] + [3] < rect2 [1] + 20) or (rect2[1]+rect2[3]<rect1[1]+20): return False return True 1234567891011121314151617Copy the code

Writing background

Class Bg: # def __init__(self,surface): self.surface=surface self.dx=-10 self.w=surface.get_width() self.rect=surface.get_rect() 123456 def initGame(): Global bg, role, clock, gameState surObject, surGameOver, score, initialization myFont, myFont1, objectList # score score = 0 # initialization objectList = [] Font("./freesansbold.ttf",32) myFont1= Pygame.font.Font("./freesansbold.ttf",64) # create a clock object Clock = pygame.time.clock () # gameState=0 # game background SurBg =pygame.image.load("image/bg.bmp").convert_alpha() bg= bg (surBg) # SurGameOver =pygame.image.load("image/gameover.bmp").convert_alpha() # figure image SurRole =pygame.image.load("image/ role-.png ").convert_alpha() role= role (surRole,508-85) # surObject=pygame.image.load("image/object.png").convert_alpha() def addObject(): Global surObject, object, objectList, object rate = 4 # is generated obstacles if not random. Randint (0300) < 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 # If event.type== PyGame. QUIT: sys.exit() elif event.type== PyGame. KEYDOWN: # Space 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: # initGame() if gameState==0: 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 # Move the object addObject() for objectList: If object.x+object.w<=0: ObjectList. Remove (object) score+=10 Collision (Role. GetRect (), Object.getRect ()): If (Object.currentFrame ==6): Print (score) print(score) else: print(score) print(score) print(score) print(score) else: print(score) print(score) ) 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646 5666768697071727374757677787980818283848586878889909192939495Copy the code

Ok, this is the whole code of this cool run every day, have a question can leave a message, I see will return. If you are interested in Python, you are welcome to join us and get free learning materials and source code.