Python实现Flappy Bird:基于Pygame的游戏开发入门
本文介绍如何使用Python和Pygame库从零开始制作Flappy Bird游戏。教程涵盖环境准备、游戏元素分析(背景、小鸟、管道)、资源提取以及核心代码实现,重点讲解Bird类的创建与移动逻辑,帮助具备基础编程知识的读者掌握2D游戏开发的基本流程。
Flappy Bird曾风靡一时,本文将以此为例,作为学习使用Python开发游戏的第一课。
本课程假设读者已具备较好的编程基础,并对Python语言有初步了解。
一、准备工作
1、使用Python开发游戏需要哪些工具?
1)Python解释器,本文使用的是Python 2.7版本,其他版本差异不大。
2)Pygame库,这是核心依赖,整个游戏逻辑均基于此库实现。
2、游戏分析
Flappy Bird机制相对简单,主要可划分为四个部分:
1)背景。包含两层背景,分别是天空背景(bg)和地面背景(land)。bg指带有蓝天白云的图像,land指底部带有斜纹的地面图像。
2)小鸟(bird)。这是游戏的主角,无需多言。
3)管道(pipe)。即游戏中阻挡小鸟飞行的水管障碍物。
4)其他元素。包括开始菜单界面和分数显示板。
重点分析对象为小鸟和管道。
小鸟持续向右飞行,若玩家未进行点击操作,小鸟会受重力影响下落。
管道不断生成,每成功通过一个管道,玩家得分加一。
当小鸟撞击管道或坠落到地面时,游戏结束。
3、资源准备
获取一个Flappy Bird的APK文件,解压提取内部资源,可获得以下内容:
1)名为atlas.png的图片文件,其中包含游戏所需的所有图像资源。
2)五个ogg格式的音频文件,包含所有音效素材。
3)名为atlas.txt的文本文件,记录了各图像在atlas.png中的具体坐标位置。
二、开始编码
前文已分析两个核心元素:小鸟和管道。本章节将重点讲解小鸟的实现。
首先,需要创建一个名为Bird的类对象。
Bird类包含以下属性:
1)图像。定义小鸟的外观表现。
2)尺寸。定义小鸟的长宽大小。
3)碰撞状态。用于判断是否发生碰撞,若发生碰撞则触发游戏结束(Game Over)。
4)速度。定义每帧移动的距离。
小鸟具有两种动作状态:静止时受重力下落,点击屏幕时向上飞跃。
基于上述逻辑,编写如下代码:
class Bird(pygame.sprite.Sprite):
def __init__(self,bird_img,pos):
pygame.sprite.Sprite.__init__(self)
self.image = bird_img
self.rect = self.image.get_rect()
self.rect.midbottom = pos
self.speed = 1
self.is_hit = False
def move(self):
self.rect.left += self.speed
self.rect.top += self.speed
def click(self):
self.rect.top -= 1.5*self.speed
回顾前文,Flappy Bird的所有图像资源均整合在atlas.png文件中。
Pygame提供了便捷的函数,用于从大图中截取所需资源。
首先加载图片资源:
#load img
game_img = pygame.image.load('res/img/atlas.png')
bg_rect = pygame.Rect(0,0,288,512)
bg_img = game_img.subsurface(bg_rect).convert()
然后分别获取需要的图片。
#config bird
bird_rect = pygame.Rect(0,970,48,48)
bird_pos = [100,230]
bird_img = game_img.subsurface(bird_rect).convert_alpha()
bird = Bird(bird_img,bird_pos)
通过上述步骤,成功获取了小鸟(bird)和背景(background)的图像数据。
最后,由于在电脑端运行,需将点击屏幕的操作映射为按下空格键。
key_pressed = pygame.key.get_pressed()
if not bird.is_hit:
if key_pressed[K_SPACE]:
bird.click()
至此,主要功能实现完毕。尽管程序可能存在细微Bug,但这将在后续章节讨论。
完整代码如下:
# -*- coding: utf-8 -*-
"""
@author: Kevio
"""
import pygame
from pygame.locals import *
from sys import exit
import random
# configure
screen_w = 288
screen_h = 512
# class
class Bird(pygame.sprite.Sprite):
def __init__(self,bird_img,pos):
pygame.sprite.Sprite.__init__(self)
self.image = bird_img
self.rect = self.image.get_rect()
self.rect.midbottom = pos
self.speed = 1
self.is_hit = False
def move(self):
self.rect.left += self.speed
self.rect.top += self.speed
def click(self):
self.rect.top -= 1.5*self.speed
# init the game
pygame.init()
screen = pygame.display.set_mode((screen_w,screen_h))
pygame.display.set_caption('flappy bird @Kevio')
#load img
game_img = pygame.image.load('res/img/atlas.png')
bg_rect = pygame.Rect(0,0,288,512)
bg_img = game_img.subsurface(bg_rect).convert()
#config bird
bird_rect = pygame.Rect(0,970,48,48)
bird_pos = [100,230]
bird_img = game_img.subsurface(bird_rect).convert_alpha()
bird = Bird(bird_img,bird_pos)
#config the game
score = 0
clock = pygame.time.Clock()
running = True
while running:
clock.tick(60)
screen.fill(0)
screen.blit(bg_img,(0,0))
if not bird.is_hit:
screen.blit(bird.image,bird.rect)
bird.move()
else:
running = False
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
key_pressed = pygame.key.get_pressed()
if not bird.is_hit:
if key_pressed[K_SPACE]:
bird.click()
以上就是本文的全部内容,希望对各位的学习有所帮助,也欢迎大家继续支持本站。
免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。









