The original link

The original link

All the pictures and music materials of this article have been put into the public account [thumb Notes]. If you need friends, you can reply “FPB”. Continue to update, welcome to follow ~

Make notes based on PyGame’s Flappy_bird

0. Final Effect:

1. Build a minimal framework

1.1 Required functions

  1. Initialize all imported PyGame modules.
pygame.init()
Copy the code
  1. Build a window. It returns a Surface object. Resolution is used to set the window size, flags for extension options, and depth for color, but this is not recommended.
Pygame.display.set_mode ((resolution = (0,0), flags = 0, depth = 0))Copy the code
  1. Set the window title.
pygame.display.set_caption("Flappy bird_Lin")
Copy the code
  1. Detect events.
pygame.event.get()
Copy the code
  1. Update the display.
pygame.display.update()
Copy the code
  1. End the program.
pygame.quit()
sys.exit()
Copy the code

1.2 Framework Building

First, import the required libraries.

import sys
import pygame
from pygame.locals import *
Copy the code

Next, the module is initialized and set up.

pygame.init()
screen = pygame.display.set_mode((288.512))
pygame.display.set_caption("Flappy bird_Lin")
Copy the code

Finally, get the event and respond.

In this case, the only event that needs to be checked 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 the screen
Copy the code

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 checked 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
Copy the code

2. Add image elements

2.2 Implementation Effect

2.1 Required functions

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

2.2 Adding to the framework

Since I want to implement Flappy_bird, I add the image element of the Flappy_bird start interface to the 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)
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()
Copy the code

3. Achieve bird flight effect

Moving an image is actually very easy, just need to change the position coordinates of the image regularly. However, this method will cause the image to move at different speeds in different situations (the application speed changes). Therefore, we take another approach to the problem.

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

3.0 Implementation Effect

3.1 Required functions

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

3.2 Train of Thought

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

# before entering the loop
clock = pygame.time.Clock()
speed = 100

# enter the loop
	time_passed = clock.tick()
	time_passed_ms = time_passed/1000	# milliseconds converted to seconds
	distance = speed*time_passed_ms
	x += distance	# Coordinate position of the image
    
    if x >lim:
    	x -= lim	In order to reduce the running time, do not return to zero directly
Copy the code

3.3 Adding 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)#.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()
Copy the code

4. Achieve the effect of bird wings

To make the bird fly, there are two steps. The first step is to move the bird up and down, and the second step is to switch the image to make the bird flap.

4.1 The bird moves up and down

The implementation of this step is simple, and the method is the same as above. Without further ado, let’s get right to the code.

# before entering the loop
base_y = 225
bird_speed = 50
# enter the loop
	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
Copy the code

The steps to add to the framework are also omitted, as before.

4.2 The bird flaps its wings

The bird flapping effect is achieved by switching the image every few frames. In order to solve this problem, we can adopt a method similar to the green brick movement, which is time-based image switching.

Add the switch speed and count before entering the loop
change_speed = 500
count = 0
# after entering the loop
	# Depending on the running time, decide how much to increase the count.
	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, return to zero to reduce the running time
    if count>140:
        count -= 140 
Copy the code