Introduction:

“Sprint – have the time of your life!”

— Gu Mu zi acridine

Big news! Mumuzi is going to introduce a new game to you today!

Let’s take a look at what’s making people so excited

“Cool Run every day” this game has a history of several years, at the beginning is really very popular, I still remember my first contact with this game is in the summer after the college entrance examination, because that

A holiday is longer, went to a clothing store to work, idle down when the store is playing every day cool run, in order to have a topic to talk into the pit, and later found that there is not

Less students are also playing, and the score is much higher than their own. Calculate now calculate probably past six or seven years, oneself gave up already every day cool ran.

Maybe because daily Cool run is the first mobile game I contact, I am quite impressed — now I finally download it and find that many rules have been changed

It doesn’t work at all. It’s not? Get ready to make a simple Python version of Run every Day mini game, by the way, back to your childhood!

Introduction:

“Cool Run every day” is Tencent mobile game platform for the mass wechat and mobile QQ users tailored to create a boutique mobile game.

On the basis of following the traditional play, we have specially added a series of exciting designs such as shining mounts and stirring pets to bring the most handy parkour experience for players.

Anytime and anywhere with wechat, QQ friends to play together, seize the ranking, love to send non-stop, high score dazzle out! Intimate interactions bring you closer to your friends, sharing and showing off makes you feel better

Friends attention, become cool running talent! Tell your friends out loud: Parkour will never be the same again!

The body of the

This article is made by Pygame game module “Run every day” small game ~

One, under preparation

All right! We officially began to prepare the small game required material, environment!

Photo materials:

Environment installation:

Python3.6, Pycharm2019(most versions are running), and some built-in modules for Pygame.

Module installation: PIP install -i https://pypi.douban.com/simple/ + module nameCopy the code

Rules of the game:

Press the space bar jump, press a short jump, press two long jump, there will be a lot of obstacles in the middle, as well as the corresponding gold coins, etc., after the failure of the game press the space bar to start again.

Second, the game steps

1). Game initialization

  • Create a game window of a specific size using PyGame.

  • Display the initial interface of the game, prompting the user to start the game

  • Put a background image in the game interface and show objects such as people, obstacles and coins.

2). Game control

  • Characters automatically run forward, press the space bar to control the character jump.

  • Create an obstacle using the addObstacle function.

  • Use the updateScreen function to constantly update the display of objects in the interface.

3) collision detection

  • Use the ListenKeyBoard function to monitor the user’s keyboard input, and detect whether people and obstacle coins collide.

  • Collision between character and obstacle: when the intersection of position information of character and obstacle is detected, it is judged as collision. The health of character is reduced by one, and the obstacle is destroyed.

  • Collision between character and gold: When a character bumps into gold, the gold is destroyed, scoring +100.

  • When the character successfully avoids an obstacle, the score is +10.

  • Use the judgeState function to determine if the game is over.

  • The final score is displayed at the end of the game and the user is prompted to press “Enter” to restart the game.

Three, formal code knocking

Import Pygame,sys import random # game configuration 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=None ObjectList =[] # objectList=[] # objectList=[] # objectList=[] # objectList=[] # objectList= 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. g=1 # gravity self.vy=0 #y speed self.vy_start=-20 # def getRect(self): Return (0,self. Y +12,self. W,self. H) 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 class Bg: 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) ) def updateView(screen): Screen. Blit (bg.surface,[-bg.dx,0]) screen. Blit (bg.surface,[1200-bg.dx,0] textSur=myFont.render("score:%d"%score, True, (128, 128, 128)) screen.blit(textSur, Screen. Blit (Role-.surface, [0, role-.y], [int(Role-.CurrentFrame) * role-.w, 0, role-.w, Role. H]) # map for object in objectList: screen.blit(object.surface, [object.x, object.y], [int(object.currentFrame) * object.w, 0, object.w, object.h]) def judgeState(screen): global gameState if gameState==0: updateView(screen) return elif gameState==1: Blit (surGameOver,[0,0]) textSur = myfont1. render("GameOver Score:%d"% Score, True, (255, 0, 0)) screen.blit(textSur, (width/2-350, height/2+150)) def main(): Pygame.init () Screen = Pygame.display. Set_mode (size) PyGame.display. InitGame () screen.blit(bg.surface,[0,0]) while True: JudgeState (screen) updateLogic() Pygame.display.flip () main()Copy the code

Iv. Effect display

Screenshot display —

The game begins:

A jump, an obstacle:

Game over:

conclusion

After watching this game, is it full of memories: when parents are not at home, secretly open the computer to play the game

When you first bought a smartphone, download one or two of the most popular games…

Now, every game, is the mark of time, you have what game memory kill? Before also wrote a lot of childhood classic games can go to see ha!

Complete project source: private letter xiaobian 06 or click on this line of blue font can receive the source code for free!

Previous games recommend —

Item 1.1 Mine Clearance

Pygame: It is said that this is the most difficult minesweeper game in history, no one, you feel……

Project 1.2 Contra

Pygame combat: After many years, “Contra” pixel style is more than classic and feelings @ all members

Item 1.3 Space Mecha game

Pygame: Thousands of lines of code to implement the Mecha Adventure Game.

Item 1.4 Fruit Ninja

【Pygame Combat 】 Popular worldwide fruit game upgrade version “fruit Ninja” online, you dare to PK?

🍓 Article summary —

The article summary | Python 1.0-2021 | has continuously updated, direct look at this article is enough

More content + source code in the article summary oh!! Welcome to read ~)