Write an everyday cool run in Python


Import PyGame, sysimPort Random; width = 1200 # window 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=NoneObjectList =[] # objectList=[] # objectList=[] # objectList=[] # objectList= None # clock=None gameState=None #__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 #0Self. g=1 # gravity self.vy=0 #y speed self.vy_start=-20 # start speed def getRect(self):return# def __init__(self,surface,x=0,y=0): (0,self.y+12,self.w,self.h)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, the self. The y, self. W, the self, h)Def collision(self,rect1,rect2): if (recT2 [0]>=rect1[2]-20) or (rect1[0]+40>= recT2 [2])or (rect1[1]+rect1[3]returnFalsereturn 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, role, clock, gameState surObject, surGameOver, score, myFont, myFont1, initialization objectList# score score = 0 # initializationFont(“./freesansbold.ttf”,32) myFont1=pygame.font.Font(“./freesansbold.ttf”,64) # myFont=pygame.fontClock = pygame.time.clock ()# initialize gameState gameState=0# game backgroundSurBg =pygame.image.load(“image/bg.bmp”).convert_alpha()bg= bg (surBg)#SurGameOver =pygame.image.load(“image/gameover.bmp”).convert_alpha()# figure imageSurRole =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) returny=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 handler for event in pygame.event.get():if event.type == pygame.quit:Sys.exit ()elif event.type== PyGame. KEYDOWN:# Spacebar jump if gameState==0:if event.key== PyGame. K_SPACE:if role.role.state=1 role.vy=role.vy_startelif role.state==1: Role-state =2 role-.vy = role-.vy_startelIf gameState==1:if event.key== PyGame.k_space :# restart initGame()ifGameState ==0:# background move bg.dx+=10if bg.dx==1200: bg.dx=0 # character move if role. State==0: role.currentFrame+=1if 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+=10Collision (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)Ok, this is the whole code of this cool run every day, have a question can leave a message, I see will reply.

Leave a Reply

Your email address will not be published.