I feel like I wrote plants vs. zombies and Tetris pretty good, and then I had this article and I wrote a cool run every day

So that’s what I’m going to do and I’m going to update all of this code and I’m going to define it the way I always do

import pygame,sys
import random
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 Clock =None # gameState=None # gameState (0,1)Copy 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)Copy 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 TrueCopy 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() 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) )Copy the code

conclusion

The above is the plant vs zombie source process, we can write to play a play I am in vain, a like learning like programming young people want to learn Python can pay attention to my private letter oh ~