Making Flappy bird games with pygame

Original link:
Making Flappy bird games with pygame

All the materials needed in this article are put in the official account [thumb note], and the students who need it will reply to FPB.

0. Final effect:

1. Build a minimum framework

1.1 required functions

  1. Initialize all imported pygame modules.
pygame.init()
  1. Generate a window. A surface object is returned. Set the window size through resolution, flags for extension options, and depth for setting colors, but it is not recommended.
pygame.display.set_mode((resolution =(0,0),flags = 0,depth = 0))
  1. Set the window title.
pygame.display.set_caption("Flappy bird_Lin")
  1. Detect events.
pygame.event.get()
  1. Update the display.
pygame.display.update()
  1. End the program.
pygame.quit()
sys.exit()

1.2 frame up

First, import the required libraries.

import sys
import pygame
from pygame.locals import *

Secondly, initialize and set the module.

pygame.init()
screen = pygame.display.set_mode((288,512))
pygame.display.set_caption("Flappy bird_Lin")

Finally, get the event and respond.

#In this case, the only event that needs to be detected is whether to exit
while True:
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
            pygame.quit()
			sys.exit()
    pygame.display.update()	#Refresh screen

1.3 complete framework

import sys
import pygame
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((288,512))
pygame.display.set_caption("Flappy bird_Lin")

#In this case, the only event that needs to be detected is whether to exit
while True:
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
            pygame.quit()
			sys.exit()
    pygame.display.update()	#Update screen

2. Add picture elements

2.2 realization effect

2.1 required functions

  1. Load a new file from the file. Return to a Surface
pygame.image.load('Picture path')
  1. Add a picture to the window. source: the Surface returned in the first step; dest: Specifies the drawing location.
screen.blit(source,dest)
  1. Update screen
pygame.display.update()	#Update screen

2.2 add to frame

Because I want to implement the flappy bird, I add the image element of the flappy bird start interface to the framework.

background_image = 'K:/bird/background-night.png'
green = 'K:/bird/base.png'
mid_b = 'K:/bird/bluebird-midflap.png'
welcome = 'K:/bird/message.png'

import pygame
from pygame.locals import *
import sys

pygame.init()
screen = pygame.display.set_mode((288,512))
pygame.display.set_caption("Flappy bird_Lin")

background = pygame.image.load(background_image)
green_base = pygame.image.load(green)
mid_bird = pygame.image.load(mid_b)
welcome1 = pygame.image.load(welcome)

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    screen.blit(background,(0,0))
    screen.blit(green_base,(0,400))
    screen.blit(mid_bird,(52,225))
    screen.blit(welcome1,(52,52))
    pygame.display.update()

3. Achieve bird flying effect

It's easy to make the picture move, just change the position coordinates of the picture regularly. But this way, the moving speed of the pictures will be different (the running speed of the program changes) in different situations. So we use another way to solve this problem.

In order to ensure the same distance of bird movement in the same time, we introduce the Clock object to calculate the time of each frame, speed * time = distance, so we only need to set a constant speed value to ensure the same speed of the picture in any case.

3.0 realization effect

3.1 required functions

  1. Initialize the Clock object.
pygame.time.Clock()
  1. Update the clock to calculate how many milliseconds have passed since the last call.
clock.tick()

3.2 realization of ideas

First, you need to initialize the Clock object and set a speed value before entering the loop. The run time is then recorded for each frame in the loop. Finally, multiply the running time by the speed value to get the distance that should be run this time.

#Before entering the cycle
clock = pygame.time.Clock()
speed = 100

#Enter cycle
	time_passed = clock.tick()
	time_passed_ms = time_passed/1000	#Milliseconds to seconds
	distance = speed*time_passed_ms
	x += distance	#Coordinate position of picture
    
    if x >lim:
    	x -= lim	#Clear 0, in order to reduce the running time, do not reset to zero directly

3.3 add to frame

background_image = 'K:/bird/background-night.png'
green = 'K:/bird/base.png'
mid_b = 'K:/bird/bluebird-midflap.png'
welcome = 'K:/bird/message.png'

import pygame
from pygame.locals import *
import sys

pygame.init()
screen = pygame.display.set_mode((288,512))
pygame.display.set_caption("Flappy bird_Lin")

background = pygame.image.load(background_image)#.convert()
green_base = pygame.image.load(green)
mid_bird = pygame.image.load(mid_b)
welcome1 = pygame.image.load(welcome)

base_x = 0
bird_y = 225
speed = 100
clock = pygame.time.Clock()

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    screen.blit(background,(0,0))
    screen.blit(green_base,(base_x,400))
    screen.blit(mid_bird,(52,bird_y))
    screen.blit(welcome1,(52,52))

    time_passed = clock.tick()
    time_passed_seconds = time_passed / 1000.0
 
    distance_moved = time_passed_seconds * speed
    base_x -= distance_moved
    if base_x<-40:
        base_x += 40
    
    pygame.display.update()

4. Achieve the effect of bird's wings

There are two steps to achieve the bird flying effect. First, you need to let the bird move up and down. Second, you need to switch pictures to achieve the effect of flapping wings.

4.1 bird up and down

The implementation of this step is very simple, and the method is consistent with the above. No more details, go straight to the code.

#Before entering the cycle
base_y = 225
bird_speed = 50
#Enter cycle
	time_passed_seconds = time_passed / 1000.0 
    bird_distance = time_passed_seconds * bird_speed    
    base_y = base_y + dir*bird_distance
    if base_y >240:
        dir = -1
    if base_y <210:
        dir = 1

The steps added to the framework are no longer repeated, as before.

4.2 birds flapping their wings

To achieve the effect of a bird flapping its wings, you only need to switch images every few frames. But it is also easy to appear fast and slow phenomenon with the machine performance and other reasons. In order to solve this problem, we can use the method similar to the green brick movement, time-based image switching.

#Switch speed and count value need to be added before entering the cycle
change_speed = 500
count = 0
#After entering the cycle
	#It is necessary to determine the increase of count (the speed of change) according to the running time
	count +=change_speed*time_passed_seconds
    if 0<=(count)<=50:
        screen.blit(mid_bird,(52,base_y))
        
    elif 50<(count)<=100:
        screen.blit(up_bird,(52,base_y))
        
    elif 100<(count)<=150:
        screen.blit(down_bird,(52,base_y))
    #When the count value is too large, reset to zero to reduce the running time
    if count>140:
        count -= 140 
15 original articles published, 3 praised, 401 visited
Private letter follow

Posted on Thu, 12 Mar 2020 04:50:45 -0400 by rastem