pygmae游戏开发——像素鸟(flappy_bird)

一、框架建立:

1.导入模块:

import sys,pygame,time,os,random
from pygame.locals import *

在下方package处搜索添加模块

pygmae游戏开发——像素鸟(flappy_bird)_第1张图片

2.环境变量加载:

PAC_DIR = os.getcwd()        #获得当前文件所在项目文件目录
sys.path.append(PAC_DIR)
 

3.配置变量(方便修改参数)
#menu:
COLOR=(255,255,255)#背景颜色白
SIZE = WIDTH,HEIGHT=336,600#视窗宽高
#bird:
BIRD_SIZE = BIRD_WIDTH,BIRD_HEIGHT=34,24#bird宽高
GRAVITY=5#模拟重力
GRAVITY_ACCESS=0.4#模拟重力加速度
BIRD_ACCESS=1.5#模拟上升阻力
FLY_SPEED=15#上升速度
VOLPLANING=1#滑翔时的降落速度
#pipe:
PIPE_SIZE=PIPE_WIDTH,PIPE_HEIGHT=54,640#管道宽高
PIPE_SPEED=3管道移动速度模拟飞行

4.模块加载
pygame.init()#pygame初始化

screen= pygame.display.set_mode(SIZE)#创建视窗
clock=pygame.time.Clock()#设置时钟实例

5.自定义函数
def menu_load():#调用blit功能显示所创建图片等初始化条件

"""

pygame.blit(类中加载对象,图片显示的位置(右上角为位置点位,元组))

"""
    clock.tick(30)  # 帧率
    screen.fill(COLOR)#刷新背景色
    screen.blit(background, (0, 0))#刷新背景位置
    screen.blit(bird.bird_status[bird.state], (bird.birdx, bird.birdy))#刷新bird图片加载对象与位置
    screen.blit(pipe.pipe,(pipe.pipex,pipe.rand_Y))#刷新pipeU图片加载对象与位置
    screen.blit(pipe.pipe,(pipe.pipex,pipe.rand_Y+pipe.blank+PIPE_HEIGHT))

#刷新pipeD图片加载对象与位置


    menu.goal_print()#分数打印
    if bird.death:
        screen.blit(menu.goal,(0,HEIGHT/2))
    else :
        screen.blit(menu.goal, (0,0 ))
def collision_check():#碰撞检测
    U_rect = pygame.Rect(pipe.pipex, pipe.rand_Y, PIPE_WIDTH, PIPE_HEIGHT)
    D_rect = pygame.Rect(pipe.pipex, pipe.rand_Y + pipe.blank + PIPE_HEIGHT, PIPE_WIDTH, PIPE_HEIGHT)
    bird_rect=pygame.Rect(bird.birdx,bird.birdy,BIRD_WIDTH,BIRD_HEIGHT)
    if bird_rect.colliderect(U_rect) or bird_rect.colliderect(D_rect):
        bird.death=True
        pipe.speed=0

6.类定义:

bird

pipe

menu

7.程序主循环:

if __name__ == "__main__":
    while True :

        menu_load()
        #退出命令接收:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                sys.exit()

       

        bird.bird_fly_check()#bird状态检测反馈与位置更新
        #pipe位置变化:
        pipe.pipe_update()#pipe位置刷新
        collision_check()#管道类与飞行鸟类碰撞检测

        pygame.display.update()#屏幕更新

二、源码:

*:pygmae项目素材文件目录:pygame/venv/images

#导入模块
import sys,pygame,time,os,random
from pygame.locals import *

#加载环境变量
PAC_DIR = os.getcwd()
sys.path.append(PAC_DIR)


#配置变量(方便修改参数)
#menu:
COLOR=(255,255,255)
SIZE = WIDTH,HEIGHT=336,600
#bird:
BIRD_SIZE = BIRD_WIDTH,BIRD_HEIGHT=34,24
GRAVITY=5
GRAVITY_ACCESS=0.4
BIRD_ACCESS=1.5
FLY_SPEED=15
VOLPLANING=1
#pipe:
RAND_Y_MAX=600
RAND_Y_MIN=140
PIPE_BLANK_HMAX=200
PIPE_BLANK_HMIN=100
PIPE_BLANK_LMAX=300
PIPE_BLANK_LMIN=100
PIPE_SIZE=PIPE_WIDTH,PIPE_HEIGHT=54,640
PIPE_SPEED=3
#模块加载
pygame.init()

screen= pygame.display.set_mode(SIZE)
clock=pygame.time.Clock()

#定义函数
#图片导入与配置:
def image_set(path,size):
    return pygame.transform.scale(pygame.image.load(path),size)
#图片打印
def menu_load():
    clock.tick(30)  # 帧率
    screen.fill(COLOR)
    screen.blit(background, (0, 0))
    screen.blit(bird.bird_status[bird.state], (bird.birdx, bird.birdy))
    screen.blit(pipe.pipe,(pipe.pipex,pipe.rand_Y))
    screen.blit(pipe.pipe,(pipe.pipex,pipe.rand_Y+pipe.blank+PIPE_HEIGHT))
    menu.goal_print()
    if bird.death:
        screen.blit(menu.goal,(0,HEIGHT/2))
    else :
        screen.blit(menu.goal, (0,0 ))
def collision_check():
    U_rect = pygame.Rect(pipe.pipex, pipe.rand_Y, PIPE_WIDTH, PIPE_HEIGHT)
    D_rect = pygame.Rect(pipe.pipex, pipe.rand_Y + pipe.blank + PIPE_HEIGHT, PIPE_WIDTH, PIPE_HEIGHT)
    bird_rect=pygame.Rect(bird.birdx,bird.birdy,BIRD_WIDTH,BIRD_HEIGHT)
    if bird_rect.colliderect(U_rect) or bird_rect.colliderect(D_rect):
        bird.death=True
        pipe.speed=0
#类定义:

#bird:
class bird:
    def __init__(self):
        self.bird_status=[
                     image_set(rf"{PAC_DIR}\vemv\images\bird_A.png",BIRD_SIZE),
                     image_set(rf"{PAC_DIR}\vemv\images\bird_U.png",BIRD_SIZE),
                     image_set(rf"{PAC_DIR}\vemv\images\bird_D.png",BIRD_SIZE),
                     image_set(rf"{PAC_DIR}\vemv\images\bird_death.png",BIRD_SIZE)
                        ]
        self.birdx=WIDTH/2
        self.birdy=HEIGHT/2
        self.gravity=GRAVITY
        self.fly_speed=FLY_SPEED
        self.gravity_access=GRAVITY_ACCESS
        self.bird_access=BIRD_ACCESS
        self.state=2
        self.death=False
        self.fly=False


    def bird_update(self):
        if not self.fly and not self.death:#降落状态
            self.state=2
            self.birdy+=self.gravity
            self.gravity+=self.gravity_access
            self.fly_speed=FLY_SPEED
        elif self.fly and not self.death :#飞行
            if self.fly_speed >0:
                self.state=1
                self.birdy-=self.fly_speed
                bird.fly_speed-=self.bird_access
            else :
                self.state=0
                self.birdy+=VOLPLANING
            self.gravity = GRAVITY
            self.fly=False
        else :
            self.state = 3
            if self.birdyHEIGHT-BIRD_HEIGHT :
            self.death=True
        if self.birdy<=0:
            self.birdy=0
        self.bird_update()


#pipe:
goal=[0]
class pipe:
    def __init__(self):
        self.pipe=image_set(rf"{PAC_DIR}\vemv\images\pipe.png",PIPE_SIZE)
        self.pipex=WIDTH-PIPE_WIDTH
        self.rand_Y=-random.randint(140,RNND_Y_MAX)
        if self.rand_Y<240:
            self.blank=random.randint(100,200)
        else:
            self.blank=random.randint(100,300)
        self.speed=PIPE_SPEED

    def pipe_update(self):
        if self.pipex>=0:
            self.pipex-=self.speed
        else :
            self.pipex=WIDTH-PIPE_WIDTH
            self.rand_Y = -random.randint(RAND_Y_MIN, RAND_Y_MAX)
            if self.rand_Y < 240:
                self.blank = random.randint(PIPE_BLANK_HMIN, PIPE_BLANK_HMAX)
            else:
                self.blank = random.randint(PIPE_BLANK_LMIN, PIPE_BLANK_LMAX)
        if self.pipex +PIPE_WIDTH == bird.birdx and not bird.death:
            goal[0]+=1

#menu:
class menu:
    def __init__(self):
        pygame.font.init()
        self.font = pygame.font.SysFont('arial', 20)
    def goal_print(self):
        if not bird.death:
            self.goal=self.font.render("YOU GOAL IS "+str(goal[0]),1,COLOR)
        else :
            self.goal = self.font.render("YOU DEAD, THE FINAL GOAL IS " + str(goal[0]), 1, (0,255,255))
# 初始属性:
background=image_set(rf"{PAC_DIR}\vemv\images\background.png",SIZE)
bird=bird()
pipe=pipe()
menu=menu()
#主循环
if __name__ == "__main__":
    while True :
        menu_load()
        #命令接收:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                sys.exit()

        # bird状态:

        bird.bird_fly_check()
        pygame.display.update()
        #pipe位置变化:
        pipe.pipe_update()
        collision_check()

三、素材文件:

 

 

 

pygmae游戏开发——像素鸟(flappy_bird)_第2张图片

 素材文件网站:

 游戏角色人物,行走,头像 2d素材 免费下载 - 爱给网 (aigei.com)

四、实机运行:

pygmae游戏开发——像素鸟(flappy_bird)_第3张图片

你可能感兴趣的:(python)