In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to achieve flappy bird Mini Game by python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Development tools:
Python version: 3.6.4
Related modules:
Pygame module
And some modules that come with python.
Environment building
Install Python and add it to the environment variable, and pip installs the relevant modules you need.
Run the video:
Text:
First, let's write a start screen to make it look more like a game. The effect goes something like this:
The principle is also simple, and there are three key points:
(1) the dark green and light green alternating floor below keeps moving to the left to create the illusion that the bird is flying forward.
(2) switch the picture of the bird every few frames to achieve the effect of flapping the bird's wings:
(3) regularly change the position of the bird in the vertical direction to achieve the effect of moving up and down.
Specifically, the code implementation is as follows:
Def startGame (screen, sounds, bird_images, other_images, backgroud_image, cfg): base_pos = [0, cfg.SCREENHEIGHT*0.79] base_diff_bg = other_images ['base'] .get_width ()-backgroud_image.get_width () msg_pos = [(cfg.SCREENWIDTH-other_images [' message']. Get_width ()) / 2 Cfg.SCREENHEIGHT*0.12] bird_idx = 0 bird_idx_change_count = 0 bird_idx_cycle = itertools.cycle ([0,1,2,1]) bird_pos = [cfg.SCREENWIDTH*0.2 (cfg.SCREENHEIGHT-list (bird_images.values ()) [0] .get _ height ()) / 2] bird_y_shift_count = 0 bird_y_shift_max = 9 shift = 1 clock = pygame.time.Clock () while True: for event in pygame.event.get (): if event.type = = pygame.QUIT or (event.type = = pygame.KEYDOWN and event.key = = pygame.K_ESCAPE): pygame.quit () Sys.exit () elif event.type = = pygame.KEYDOWN: if event.key = = pygame.K_SPACE or event.key = = pygame.K_UP: return {'bird_pos': bird_pos 'base_pos': base_pos 'bird_idx': bird_idx} sounds [' wing'] .play () bird_idx_change_count + = 1 if bird_idx_change_count% 5 = 0: bird_idx = next (bird_idx_cycle) bird_idx_change_count = 0 base_pos [0] =-((- base_pos [0] + 4)% base_diff_bg) bird_y_shift_count + = 1 if bird_y _ shift_count = = bird_y_shift_max: bird_y_shift_max = 16 shift =-1 * shift bird_y_shift_count = 0 bird_pos [- 1] = bird_pos [- 1] + shift screen.blit (backgroud_image (0,0) screen.blit (list (bird_images.values ()) [bird_idx], bird_pos) screen.blit (other_images ['message'], msg_pos) screen.blit (other_images [' base'], base_pos) pygame.display.update () clock.tick (cfg.FPS)
Click the spacebar or ↑ key to enter the main program. For the main program, after the necessary initialization work, on the basis of the content implemented in the game start interface, the main contents that need to be implemented are as follows:
(1) the pipe and the floor alternating with dark green and light green continue to move to the left to achieve the effect of the bird flying forward.
(2) Press the key to detect, when the player clicks the space bar or the ↑ key, the bird slows down the acceleration upward until the upward speed decelerates to 0, otherwise the bird moves in free fall (for convenience, it can be considered that the bird moves in a uniform straight line in a very short period of time)
(3) collision detection, when the bird collides with the pipe / game boundary, the game fails and enters the game end interface. Note that for collision detection to be more accurate, we use:
Pygame.sprite.collide_mask
Pipe:
(4) after entering the game, two pairs of pipes are randomly generated and constantly moved to the left. When the leftmost pipe is about to disappear because it reaches the left boundary of the game interface, regenerate a pair of pipes (be careful not to generate repeatedly)
(5) when the bird crosses a gap between the upper and lower pipes, the game score is increased by one (be careful not to repeat the score).
Score table
Simply paste the source code of the main program here:
# enter the main game score = 0bird_pos, base_pos, bird_idx = list (game_start_info.values ()) base_diff_bg = other_images ['base']. Get_width ()-backgroud_image.get_width () clock = pygame.time.Clock () #-Pipeline pipe_sprites = pygame.sprite.Group () for i in range (2): pipe_pos = Pipe.randomPipe (cfg) Pipe_images.get ('top')) pipe_sprites.add (Pipe (image=pipe_images.get (' top'), position= (cfg.SCREENWIDTH+200+i*cfg.SCREENWIDTH/2, pipe_pos.get ('top') [- 1])) pipe_sprites.add (Pipe (image=pipe_images.get (' bottom'), position= (cfg.SCREENWIDTH+200+i*cfg.SCREENWIDTH/2, pipe_pos.get ('bottom') [- 1])) #-bird class bird = Bird (images=bird_images, idx=bird_idx) Position=bird_pos) #-- whether to add pipeis_add_pipe = True#-- whether the game is in progress is_game_running = Truewhile is_game_running: for event in pygame.event.get (): if event.type = = pygame.QUIT or (event.type = = pygame.KEYDOWN and event.key = = pygame.K_ESCAPE): pygame.quit () sys.exit () elif event.type = = pygame.KEYDOWN: if event.key = = pygame.K_SPACE or event.key = = pygame.K_UP: bird.setFlapped () sounds ['wing'] .play () #-- collision detection for pipe in pipe_sprites: if pygame.sprite.collide_mask (bird Pipe): sounds ['hit'] .play () is_game_running = False #-- Update Bird boundary_values = [0, base_pos [- 1]] is_dead = bird.update (boundary_values, float (clock.tick (cfg.FPS)) / 1000.) If is_dead: sounds ['hit'] .play () is_game_running = False #-- move base to achieve the effect of birds flying forward base_pos [0] =-((- base_pos [0] + 4)% base_diff_bg) #-- move pipe to achieve birds flying forward flag = False for pipe in pipe_sprites: pipe.rect.left-= 4 if pipe.rect.centerx
< bird.rect.centerx and not pipe.used_for_score: pipe.used_for_score = True score += 0.5 if '.5' in str(score): sounds['point'].play() if pipe.rect.left < 5 and pipe.rect.left >0 and is_add_pipe: pipe_pos = Pipe.randomPipe (cfg, pipe_images.get ('top')) pipe_sprites.add (Pipe (image=pipe_images.get (' top'), position=pipe_pos.get ('top')) pipe_sprites.add (Pipe (image=pipe_images.get (' bottom')) Position=pipe_pos.get ('bottom')) is_add_pipe = False elif pipe.rect.right < 0: pipe_sprites.remove (pipe) flag = True if flag: is_add_pipe = True #-bind the necessary elements on the screen screen.blit (backgroud_image, (0,0)) pipe_sprites.draw (screen) screen.blit (other_images [' base'], base_pos) showScore (screen, score Number_images) bird.draw (screen) pygame.display.update () clock.tick (cfg.FPS)
Game over
If one of our protagonists is accidentally killed on the pole as we expected (flip up, right at the beginning of the game), it means gameOver ()
Def endGame (screen, sounds, showScore, score, number_images, bird, pipe_sprites, backgroud_image, other_images, base_pos) Cfg): sounds ['die'] .play () clock = pygame.time.Clock () while True: for event in pygame.event.get (): if event.type = = pygame.QUIT or (event.type = = pygame.KEYDOWN and event.key = = pygame.K_ESCAPE): pygame.quit () sys.exit () elif event.type = = pygame.KEYDOWN: if event.key = = pygame.K_SPACE or event. Key = = pygame.K_UP: return boundary_values = [0 Base_pos [- 1]] bird.update (boundary_values, float (clock.tick (cfg.FPS)) / 1000.) Screen.blit (backgroud_image, (0,0)) pipe_sprites.draw (screen) screen.blit (other_images ['base'], base_pos) showScore (screen, score, number_images) bird.draw (screen) pygame.display.update () clock.tick (cfg.FPS) "how python implements flappy bird Mini Game" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.