Python code to implement Flippy Bird

140 lines of code to implement Flippy Bird

Saying what the Chinese name of this game is, I ca n’t remember it. I do n’t have much to say. The 140-line realization of the second chapter of the mini-game series is still a simple mini-game. Compared with the Sudoku game, it is displayed on the game interface. It is more difficult on the top, but it is simpler in terms of logic. It is nothing more than speed, acceleration, time, position, and collision detection that need to be dealt with. The entire dynamic display is required on the interface;

The whole code will still be given at the end, but it can still be  run directly from my  Github warehouse Fork, and the image resources are there, have fun.

Operation and gameplay:

  1. python main.py runs the game;
  2. Mouse click is to pause, then click to continue;
  3. Spacebar to jump;

Subsequent expansion:

  1. The appearance of pipes can be more random, including location and length, and it is currently a very simple way;
  2. The speed of the game can be faster and faster, which is currently fixed;
  3. The free fall speed and jumping speed of the bird need to be optimized, and the current operation feels not so smooth;
  4. Add buttons such as scoring system, start, and return;

Bird diagram, need to fetch

Game screenshot

processing

When suspended

At death

Key code analysis

Pipeline moving over time

It can be seen that for this game, the pipe is actually moved instead of the bird, so here is mainly to deal with the change of the position of the pipe drawing, and the entire process of a loop. If there are N pipes displayed on the screen, then it can be imagined as N + 2 pipes will appear on our interface without stopping in circles;

tunnel_list = [x-speed if x-speed>-200 else 2100 for x in tunnel_list ] def draw_tunnel(): for x in tunnel_list: pygame.draw.rect(screen,COLORS['darkgreen'],(x,0,100,350),0) pygame.draw.rect(screen,COLORS['darkgreen'],(x+100,550,100,350),0)

Free fall birdie and jump after clicking the space

Without operation, the bird's up and down movement is to do a free fall, that is, the process of falling faster and faster, and when we click  空格 to jump, the actual speed of the bird is actually changed, so the bird will Jump up and down slowly until the speed is 0, continue to decline, in line with basic physical rules;

G = 9.8*30 # g
JUMP_V = -300
bird_x,bird_y = 700,450 bird_v = 0 if not jump: bird_v += G*frame else: bird_v = JUMP_V jump = False bird_y += frame*bird_v def draw_bird(): screen.blit(birdImg,[bird_x,bird_y])

Impact checking

Detect whether the bird touches the pipe or falls to the ground, so it is a footless bird, in fact, it is to detect whether the two rectangles overlap.

def rect_cover(rect1,rect2,up=True):
    # bird
    left_up1 = (rect1[0],rect1[1]) left_down1 = (rect1[0],left_up1[1]+rect1[3]) right_up1 = (left_up1[0]+rect1[2],rect1[1]) right_down1 = (left_up1[0]+rect1[2],left_up1[1]+rect1[3]) # tunnel left_up2 = (rect2[0],rect2[1]) left_down2 = (rect2[0],left_up2[1]+rect2[3]) right_up2 = (left_up2[0]+rect2[2],rect2[1]) right_down2 = (left_up2[0]+rect2[2],left_up2[1]+rect2[3]) # check if (left_up2[0] <= right_up1 [ 0] <= right_up2 [ 0]): # x, it must be the right line contact, so judge the bird ’s right if if and and (left_up2 [ 1] <= right_up1 [ 1] <= left_down2 [ 1]): return True elif ( not up) and (left_up2 [ 1] <= right_down1 [ 1] <= left_down2 [ 1]): return True return False

Pygame drawing rect does not support transparent layers implemented under transparency

Seeing that, in fact, with the help of Surface, set it to the desired transparency and blit it on our screen. Direct draw rect does not support RGBA. A sets the alpha. I do n’t know why this ugly design;

def draw_dead():
    s = pygame.Surface(SIZE, pygame.SRCALPHA)
    s.fill((255,255,255,240)) screen.blit(s, (0,0)) txt = font120.render('YOU DEAD',True,COLORS['black']) x,y = 450,400 screen.blit(txt,(x,y))

All codes

import sys

import pygame
from pygame.color import THECOLORS as COLORS

def draw_background(): # white background screen.fill(COLORS['lightblue']) pygame.draw.rect(screen,COLORS['black'],(-100,902,3000,200),5) def draw_tunnel(): for x in tunnel_list: pygame.draw.rect(screen,COLORS['darkgreen'],(x,0,100,350),0) pygame.draw.rect(screen,COLORS['darkgreen'],(x+100,550,100,350),0) def draw_bird(): screen.blit(birdImg,[bird_x,bird_y]) def draw_context(): txt = font50.render('Count time: '+str(int(count_time))+' S',True,COLORS['black']) x,y = 10,920 screen.blit(txt,(x,y)) def draw_pause(): s = pygame.Surface(SIZE, pygame.SRCALPHA) s.fill((255,255,255,220)) screen.blit(s, (0,0)) txt = font120.render('PAUSE',True,COLORS['darkgray']) x,y = 550,400 screen.blit(txt,(x,y)) def draw_dead(): s = pygame.Surface(SIZE, pygame.SRCALPHA) s.fill((255,255,255,240)) screen.blit(s, (0,0)) txt = font120.render('YOU DEAD',True,COLORS['black']) x,y = 450,400 screen.blit(txt,(x,y)) def rect_cover(rect1,rect2,up=True): # bird left_up1 = (rect1[0],rect1[1]) left_down1 = (rect1[0],left_up1[1]+rect1[3]) right_up1 = (left_up1[0]+rect1[2],rect1[1]) right_down1 = (left_up1[0]+rect1[2],left_up1[1]+rect1[3]) # tunnel left_up2 = (rect2[0],rect2[1]) left_down2 = (rect2[0],left_up2[1]+rect2[3]) right_up2 = (left_up2[0]+rect2[2],rect2[1]) right_down2 = (left_up2[0]+rect2[2],left_up2[1]+rect2[3]) # check if (left_up2[0]<=right_up1[0]<=right_up2[0]): # x,肯定是右侧线接触,因此判断bird的right即可 if up and (left_up2[1]<=right_up1[1]<=left_down2[1]): return True elif (not up) and (left_up2[1]<=right_down1[1]<=left_down2[1]): return True return False def check_dead(): bird_rect = (bird_x,bird_y,70,70) if bird_rect[1]+bird_rect[3]>900: return True for x in tunnel_list: up_rect = (x,0,100,350) down_rect = (x+100,550,100,350) if rect_cover(bird_rect,up_rect) or rect_cover(bird_rect,down_rect,up=False): return True return False if __name__ == "__main__": # init pygame pygame.init() # contant SIZE = [1500,1000] font50 = pygame.font.SysFont('Times', 50) font120 = pygame.font.SysFont('Times', 120) G = 9.8*30 # g JUMP_V = -300 # brid birdPath = 'bird.png' birdImg = pygame.image.load(birdPath) # tunnel tunnel_list = [100,600,1100,1600,2100] # create screen 500*500 screen = pygame.display.set_mode(SIZE) # variable parameter bird_x,bird_y = 700,450 bird_v = 0 count_time = 0 # level speed = 5 frame = 0.02 # main loop running = True pause = False jump = False dead = False while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False break elif event.type == pygame.MOUSEBUTTONDOWN: pause = not pause elif event.type == pygame.KEYUP: if chr(event.key) == ' ': jump = True # update data if not pause and not dead: count_time += frame tunnel_list = [x-speed if x-speed>-200 else 2100 for x in tunnel_list ] if not jump: bird_v += G*frame else: bird_v = JUMP_V jump = False bird_y += frame*bird_v # background draw_background() # tunnel draw_tunnel() # choose item draw_bird() # point draw_context() # pause if not dead and pause: draw_pause() # dead if dead: draw_dead() # flip pygame.display.flip() # pause 20ms pygame.time.delay(int(frame*1000)) # check win or not if check_dead(): #print('You dead, dumb ass!!!') #break dead = True pygame.quit()

At last

Both Sudoku and FlippyBird are  here  . You are welcome to run the Fork directly and follow up with other small games from time to time. At present, it is mainly based on simple action games, chess and cards. What do you want to do, or do you want to play You can tell me in the comment area, I will complete it as soon as possible;

last of the last

You can go to my Github to see if there are other things you need. At present, it is mainly self-made machine learning projects, various Python scripting tools, interesting small projects, Follow's big brother, Fork projects, etc .:

Guess you like

Origin www.cnblogs.com/MonsterJ/p/12694158.html