python flappy bird游戏

flappy bird游戏

  • 前言
  • 开始
      • 献上代码
      • 代码剖析
      • 项目github
  • 其他
            • 作者

前言

开始

献上代码

from tkinter.messagebox import showinfo
from pygame.locals import *
import tkinter as tk
from random import *
import pygame
base = tk.Tk()
base.geometry('0x0')
base.resizable(False,False)
def gen_pipe(h):
    surf = pygame.Surface((64,h))
    surf.fill((61,145,64))
    rint = randint(100,h - 100)
    pygame.draw.rect(surf,(51,161,201),Rect((0,rint),(64,rint + 64)))
    return surf
def check(surf,r1):
    r1 = ((int(r1[0][0]),int(r1[0][1])),(int(r1[1][0]),int(r1[1][1])))
    for i in range(r1[0][0],r1[1][0]):
        for j in range(r1[0][1],r1[1][1]):
            if(surf.get_at((i,j)) != (51,161,201)):
                return True
    return False
def main():
    WIDTH,HEIGHT = 640,480
    scr = pygame.display.set_mode((WIDTH,HEIGHT))
    bird = pygame.image.load('bird.png')
    bird_y = WIDTH / 2
    pipe = gen_pipe(HEIGHT)
    pipe_x = int(WIDTH)
    score = 0
    while(1):
        if(pipe_x <= -pipe.get_width()):
            pipe = gen_pipe(HEIGHT)
            pipe_x = int(WIDTH)
            score += 1
        pipe_x -= 1
        bird_y += (1 / 3)
        pygame.display.update()
        scr.fill((51,161,201))
        scr.blit(pipe,(pipe_x,0))
        if (bird_y < 0 or bird_y > HEIGHT or check(scr, ((100, bird_y), (133, bird_y + 32)))):
            return score
        scr.blit(bird, (100, bird_y))
        for ev in pygame.event.get():
            if(ev.type == QUIT):
                exit()
            elif(ev.type == KEYDOWN and ev.key == K_SPACE):
                bird_y -= 70
if(__name__ == '__main__'):
    while(1):
        s = main()
        showinfo('GAMEOVER!','GAMEOVER!score:%s'%s)

只用了51行代码,优秀不优秀

代码剖析

from tkinter.messagebox import showinfo
from pygame.locals import *
import tkinter as tk
from random import *
import pygame
base = tk.Tk()
base.geometry('0x0')
base.resizable(False,False)

导入库并初始化

def gen_pipe(h):
    surf = pygame.Surface((64,h))
    surf.fill((61,145,64))
    rint = randint(100,h - 100)
    pygame.draw.rect(surf,(51,161,201),Rect((0,rint),(64,rint + 64)))
    return surf

生成flappy bird中的管道,思路如下:

  • 生成一个与屏幕同高,与管道宽的pygame.Surface对象(后称“对象”)
  • 将其填充上管道的颜色
  • 在其中画一个与对象同宽的长方形,并填充成天的颜色

这样,就生成出了一组管道(一上一下)

def check(surf,r1):
    r1 = ((int(r1[0][0]),int(r1[0][1])),(int(r1[1][0]),int(r1[1][1])))
    for i in range(r1[0][0],r1[1][0]):
        for j in range(r1[0][1],r1[1][1]):
            if(surf.get_at((i,j)) != (51,161,201)):
                return True
    return False

检查小鸟是否撞上柱子(即小鸟所在的位置有没有管道的颜色)

def main():
    WIDTH,HEIGHT = 640,480
    scr = pygame.display.set_mode((WIDTH,HEIGHT))
    bird = pygame.image.load('bird.png')
    bird_y = WIDTH / 2
    pipe = gen_pipe(HEIGHT)
    pipe_x = int(WIDTH)
    score = 0
    while(1):
        if(pipe_x <= -pipe.get_width()):
            pipe = gen_pipe(HEIGHT)
            pipe_x = int(WIDTH)
            score += 1
        pipe_x -= 1
        bird_y += (1 / 3)
        pygame.display.update()
        scr.fill((51,161,201))
        scr.blit(pipe,(pipe_x,0))
        if (bird_y < 0 or bird_y > HEIGHT or check(scr, ((100, bird_y), (133, bird_y + 32)))):
            return score
        scr.blit(bird, (100, bird_y))
        for ev in pygame.event.get():
            if(ev.type == QUIT):
                exit()
            elif(ev.type == KEYDOWN and ev.key == K_SPACE):
                bird_y -= 70

这里分开剖析

def main():
    WIDTH,HEIGHT = 640,480
    scr = pygame.display.set_mode((WIDTH,HEIGHT))
    bird = pygame.image.load('bird.png')
    bird_y = WIDTH / 2
    pipe = gen_pipe(HEIGHT)
    pipe_x = int(WIDTH)
    score = 0

初始化变量:

  • 窗口长宽
  • 窗口对象
  • 鸟的贴图
  • 鸟的y轴坐标
  • 管道
  • 管道的x轴坐标
    while(1):
        if(pipe_x <= -pipe.get_width()):
            pipe = gen_pipe(HEIGHT)
            pipe_x = int(WIDTH)
            score += 1
        pipe_x -= 1
        bird_y += (1 / 3)
        pygame.display.update()
        scr.fill((51,161,201))
        scr.blit(pipe,(pipe_x,0))
     	if (bird_y < 0 or bird_y > HEIGHT or check(scr, ((100, bird_y), (133, bird_y + 32)))):
            return score
        scr.blit(bird, (100, bird_y))

进入游戏循环处理:

  • 管道如果已经挪出界,创建新的管道
  • 鸟向下飞 1 3 \frac{1}{3} 31个像素。
  • 管道向前移动一个像素
  • 刷新页面
  • 将页面填充成天的颜色
  • 将管道贴图贴在屏幕上
  • 如果鸟飞出界或撞上管道,游戏结束
  • 将鸟的贴图贴在屏幕上
        for ev in pygame.event.get():
            if(ev.type == QUIT):
                exit()
            elif(ev.type == KEYDOWN and ev.key == K_SPACE):
                bird_y -= 70

处理如下时间事件 ↓ \downarrow

  • 按下(X)退出游戏
  • 按下空格键鸟向上飞70个像素


if(__name__ == '__main__'):
    while(1):
        s = main()
        showinfo('GAMEOVER!','GAMEOVER!score:%s'%s)

运行并提示gameover

项目github

github传送门

其他

项目素材均来自爱给网


作者

hit-road

拜拜,下课!
回到顶部

你可能感兴趣的:(pygame,python,游戏,游戏开发,2d游戏)