Foreword
"The harder you work, the luckier you are." π¦π¦οΌ Remember to make a little progress every day“
Python is the best language in the world
Hello everyone ~ ~ (I don't know if my first word is wrong, lazy. jpg doesn't have Baidu, if it's wrong, just pass by ~ ha ha)
Life is like a game of passing through customs. Every day, you will encounter many happy, unhappy, good and bad things.
Today, I'll write you a simple little dinosaur breakthrough game. I hope you will bravely rush forward no matter what you encounter
Keep going~
”There is no secret to success. If there are any, there are two: the first is to stick to it and never give up; the second is to be
When you want to give up, look back at the first secret: stick to it and never give up“
(quietly tell you this sentence is Baidu's π€«π€«π€«π€«π€«π€«)
So, let's keep going, full of energy and start rushing forward ππππππ
Text
1) Prepare the environment
The environment required for this article is as follows π
Python and pycham installation package: I use 3.7 Python and 2019 community version pycham. You can see this
Basically, they can be used and run with compatible code. Then there is a Pygame game module mainly used.
Environment installation won't work. You can ask me to get the installation package, the installation video and the corresponding permanent activation code of pycham professional version
Yes, but there are version restrictions and some cannot be activated.
The installation of third-party libraries is usually:
pip installΒ pygame
Or if you feel that the installation speed is too slow, you can add the image source file. Here, I usually use the image source of Douban:
pip install -i https://pypi.douban.com/simple/ pygame
2) Get the material ready
3) attached main program
import cfg import sys import random import pygame from modules import * '''main''' def main(highest_score): # Game initialization pygame.init() screen = pygame.display.set_mode(cfg.SCREENSIZE) pygame.display.set_caption('Little dinosaur breakthrough game') # Import all sound files sounds = {} for key, value in cfg.AUDIO_PATHS.items(): sounds[key] = pygame.mixer.Sound(value) # Game start interface GameStartInterface(screen, sounds, cfg) # Define some necessary elements and variables in the game score = 0 score_board = Scoreboard(cfg.IMAGE_PATHS['numbers'], position=(534, 15), bg_color=cfg.BACKGROUND_COLOR) highest_score = highest_score highest_score_board = Scoreboard(cfg.IMAGE_PATHS['numbers'], position=(435, 15), bg_color=cfg.BACKGROUND_COLOR, is_highest=True) dino = Dinosaur(cfg.IMAGE_PATHS['dino']) ground = Ground(cfg.IMAGE_PATHS['ground'], position=(0, cfg.SCREENSIZE[1])) cloud_sprites_group = pygame.sprite.Group() cactus_sprites_group = pygame.sprite.Group() ptera_sprites_group = pygame.sprite.Group() add_obstacle_timer = 0 score_timer = 0 # Game main loop clock = pygame.time.Clock() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE or event.key == pygame.K_UP: dino.jump(sounds) elif event.key == pygame.K_DOWN: dino.duck() elif event.type == pygame.KEYUP and event.key == pygame.K_DOWN: dino.unduck() screen.fill(cfg.BACKGROUND_COLOR) # --Add cloud randomly if len(cloud_sprites_group) < 5 and random.randrange(0, 300) == 10: cloud_sprites_group.add(Cloud(cfg.IMAGE_PATHS['cloud'], position=(cfg.SCREENSIZE[0], random.randrange(30, 75)))) # --Add cactus / flying dragon randomly add_obstacle_timer += 1 if add_obstacle_timer > random.randrange(50, 150): add_obstacle_timer = 0 random_value = random.randrange(0, 10) if random_value >= 5 and random_value <= 7: cactus_sprites_group.add(Cactus(cfg.IMAGE_PATHS['cacti'])) else: position_ys = [cfg.SCREENSIZE[1]*0.82, cfg.SCREENSIZE[1]*0.75, cfg.SCREENSIZE[1]*0.60, cfg.SCREENSIZE[1]*0.20] ptera_sprites_group.add(Ptera(cfg.IMAGE_PATHS['ptera'], position=(600, random.choice(position_ys)))) # --Update game elements dino.update() ground.update() cloud_sprites_group.update() cactus_sprites_group.update() ptera_sprites_group.update() score_timer += 1 if score_timer > (cfg.FPS//12): score_timer = 0 score += 1 score = min(score, 99999) if score > highest_score: highest_score = score if score % 100 == 0: sounds['point'].play() if score % 1000 == 0: ground.speed -= 1 for item in cloud_sprites_group: item.speed -= 1 for item in cactus_sprites_group: item.speed -= 1 for item in ptera_sprites_group: item.speed -= 1 # --Collision detection for item in cactus_sprites_group: if pygame.sprite.collide_mask(dino, item): dino.die(sounds) for item in ptera_sprites_group: if pygame.sprite.collide_mask(dino, item): dino.die(sounds) # --Draw game elements to the screen dino.draw(screen) ground.draw(screen) cloud_sprites_group.draw(screen) cactus_sprites_group.draw(screen) ptera_sprites_group.draw(screen) score_board.set(score) highest_score_board.set(highest_score) score_board.draw(screen) highest_score_board.draw(screen) # --Update screen pygame.display.update() clock.tick(cfg.FPS) # --Is the game over if dino.is_dead: break # Game end interface return GameEndInterface(screen, cfg), highest_score '''run''' if __name__ == '__main__': highest_score = 0 while True: flag, highest_score = main(highest_score) if not flag: break
4) Effect display
Game interface——
Obstacle interface——
Obstacles are π΅, Bird. Press the blank key to jump and avoid obstacles. The game is over!
Game over——
Summary
Hundreds of millions of people are playing the Google little dinosaur game. The highest score in the world is 99999? Do you want to try? What's your highest score
What about? (don't ask me. jpg I'm not over 1000) The complete source code can be obtained here