Use pygame to develop a game: ``jumping rabbit'' (5)
This section will increase the scoring mechanism, that is, how many platforms have been jumped, how many points are there, and if the player falls outside the game frame, the player dies and a new game starts.
Increase points mechanism
The principle of the points mechanism is actually very simple. Draw the corresponding text in the game box through pygame. When the player jumps to the new platform, the points will change accordingly. The logic is too simple, just look at the code.
# main.py/Game # def new(self): # self.score = 0 def update(self): # ... # 1/4 0 1/4 if self.player.rect.top <= HEIGHT/4: # self.player.pos.y += abs(self.player.vel.y) # for plat in self.platforms: # plat.rect.y += abs(self.player.vel.y) if plat.rect.top >= HEIGHT: plat.kill() # - self.score += 10
In the new() method, the integral object is initialized, and then updated in the update() method. The scoring method here is not to score points when players jump to a new platform, but to score points after the old platform is destroyed. This can avoid the situation where the player does not jump up, but keeps jumping in place to get points.
Next is the logic of drawing the integral text, which is implemented in the draw() method.
# main.py/Game def draw(self): # self.screen.fill(BLACK) self.all_sprites.draw(self.screen) # - self.draw_text(str(self.score), 22, WHITE, WIDTH/2, 15) # pg.display.flip()
In the draw() method, the self.draw_text() method is called to display the text. The method code is as follows.
# main.py/Game def __init__(self): # ... # self.font_name = pg.font.match_font(FONT_NAME) # def draw_text(self, text, size, color, x, y): font = pg.font.Font(self.font_name, size) # text_surface = font.render(text, True, color) # text_rect = text_surface.get_rect() # text_rect.midtop = (x, y) # self.screen.blit(text_surface, text_rect) #
Detailed comments are given in the code, so I won t go into details here.
Add the above code, run the game, there will be corresponding points effect
If the player's box is outside the game box, the player is dead. Implement this logic in the update() method of the Game class
#main.py/Game def update(self): # - if self.player.rect.bottom > HEIGHT: for sprite in self.all_sprites: sprite.rect.y -= max(self.player.vel.y, 10) # 0 - if sprite.rect.bottom < 0: sprite.kill()
At the beginning, it is judged whether the bottom of the player object square is greater than the height of the game frame. If it is greater, it means that the player is already at the bottom of the game frame and part of it is already outside the game frame. At this time, the player dies and the game ends.
Draw the game start interface
Do you remember the overall structure given in the second section, which includes the show_start_screen() method and the show_go_screen() method, because the overall operating logic is as follows:
g = Game() g.show_start_screen() # while g.running: g.new() g.show_go_screen() # pg.quit()
The drawing logic of the game start interface can be written into the show_start_screen() method, the code is as follows:
# main.py/Game # def show_start_screen(self): self.screen.fill(BGCOLOR) # self.draw_text(TITLE, 48, WHITE, WIDTH/2, HEIGHT/4) # self.draw_text("Left and right button move, space bar jump", 22, WHITE, WIDTH/2, HEIGHT/2) self.draw_text("Press any key to start the game", 22, WHITE, WIDTH/2, HEIGHT * 3/4) # pg.display.flip() self.wait_for_key() #
In the show_start_screen() method, the entire background color is filled in a meeting, and then the draw_text() method is called to draw the text. If you use Chinese, it cannot be displayed. To display Chinese in pygame, you need to specify the corresponding system font, such as system "Song Ti", "HeiTi", etc., finally call the wait_for_key() method to wait. The method logic is as follows:
# main.py/Game def wait_for_key(self): waiting = True while waiting: self.clock.tick(FPS) for event in pg.event.get(): if event.type == pg.QUIT: # waiting = False self.running = False if event.type == pg.KEYUP: # waiting = False
The logic of the wait_for_key() method is an infinite loop. Only the user clicks the close button or hits any key on the keyboard to exit the loop.
After exiting the loop, you can enter the real game loop and start the game.
In this section, we implemented the scoring logic, death logic, and simple welcome interface logic when the game starts.
Because of space considerations, the complete code is not given in the article, but in order to facilitate your understanding, I uploaded the corresponding code to github
If the article is helpful or interesting to you, click "I'm watching" to support the author.