In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to make flappybird games based on Python". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to make flappybird games based on Python.
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.
be all eagerness to see it
Run the following command in the cmd window:
Python Game6.py
Brief introduction of principle
Because it is rewritten, so let's reintroduce the principle of implementation.
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
To replace the previous one:
Pygame.sprite.collide_rect
(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).
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)
After the game is over, enter the game interface. No corresponding game material is found, so just let the game interface stand still, and then the bird runs in freefall until it falls to the ground. The code is implemented as follows:
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)
Click the space bar or ↑ key to restart the game.
For more information on the complete source code of All done, please see the relevant files.
Se_pos) showScore (screen, score, number_images) bird.draw (screen) pygame.display.update () clock.tick (cfg.FPS)
Click the space bar or ↑ key to restart the game.
At this point, I believe you have a deeper understanding of "how to make flappybird games based on Python". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.