PyGame game programming

  • One of the reasons why Python is so popular is that it is used in a wide variety of fields, including game development. Instead, the preferred module for game development in Python is PyGame.

1. Getting to know Pygame first

  • PyGame is a cross-platform Python module designed for video games, including images, sounds, etc., built on the basis of SDL (Simple DirectMedia Layer), allowing real-time video game development without being affected by low-level languages ​​such as C or lower-level Assembly language bound. Based on such a vision, all required game functions and concepts (mainly graphics) are completely simplified into the game logic itself, and all resource structures can be provided by high-level languages ​​(such as Python).

1. Install Pygame

  • The official website of PyGame is www.pygame.org. You can view the relevant documents of PyGame at this URL. The installation command for PyGame is simple:
pip install pygame
  • Check if the module is installed successfully:
import pygame
pygame.ver
  • The result of the operation is as follows:
    insert image description here

2. Common modules of Pygame

  • The advantage of Pygame in game development is that you don't need to think too much about the underlying content, and you can focus on the game logic. For example, PyGame integrates many low-level related modules, such as accessing display devices, managing events, using fonts, etc.
  • Pygame commonly used modules are as follows:
module name Function
pygame.cdrom Access the optical drive.
pygame.cursors Load cursor.
pygame.display Access display devices.
pygame.draw Draw shapes, lines and points.
pygame.event Manage events.
pygame.font Use fonts.
pygame.image Load and store images.
pygame.joystick Use a gamepad or something similar.
pygame.key Read keyboard keys.
pygame.mixer sound.
pygame.mouse mouse.
pygame.movie Play video.
pygame.music play music.
pygame.overlay Access advanced video overlays.
pygame.rect Manage rectangular areas.
pygame.sndarray Manipulate sound data.
pygame.sprite Manipulate moving images.
pygame.surface Manage images and screens.
pygame.surfarray Manage bitmap image data.
pygame.time Manage time and frame information.
pygame.transform Zoom and pan the image.
  • Use Pygame's display module and event module to create a PyGame window, the code is as follows:
# -*- coding: utf-8 -*-
import sys         # 导入sys模块
import pygame       # 导入pygame模块


pygame.init()        # 初始化pygame
size = width, height = 320, 240     # 设置窗口尺寸
screen = pygame.display.set_mode(size)    # 显示窗口

# 执行死循环,确保窗口一直显示
while True:
    # 检查事件
    for event in pygame.event.get():    # 遍历所有事件
        if event.type == pygame.QUIT:     # 如果单击关闭窗口,则退出
            sys.exit()
pygame.quit()       # 退出pygame
  • The result of the operation is as follows:
    insert image description here

2. Make a jumping ball game (Pygame basic use)

  • Create a game window, and then create a small ball inside the window. Move the ball at a certain speed. When the ball hits the edge of the game window, the ball bounces back and continues to move.
    (1) Create a game window with width and height set to 640*480:
import sys    # 导入sys模块
import pygame    # 导入pygame模块

pygame.init()     # 初始化pygame
size = width,height=640,480     # 设置窗口
screen = pygame.display.set_mode(size)     # 显示窗口
  • In the above code, first import the pygame module, and then call the init() method to initialize the pygame module. Next, set the width and height of the window, and finally use the display module to display the form.
  • Common methods of the display module:
method name Function
pygame.display.init Initialize the display module.
pygame.display.quit End the display module.
pygame.display.get_init Returns True if the display module has been initialized.
pygame.display.set_mode Initializes an interface ready to be displayed.
pygame.display.get_surface Get the current Surface object.
pygame.display.flip Update the entire Surface object to be displayed on the screen.
pygame.display.update Update part of the content to be displayed on the screen. If there is no parameter, it has the same function as flip.
(2) When running the above code, a flashing black window will appear, because the program will automatically close after execution is completed. If you want to keep the window displayed, you need to use while True to keep the program running. In addition, you need to set the close button. The code is as follows:
# -*-coding:utf-8 -*-
import sys    # 导入sys模块
import pygame   # 导入pygame模块

pygame.init()     # 初始化pygame
size = width, height = 640, 480    # 设置窗口
screen = pygame.display.set_mode(size)   # 显示窗口

# 执行死循环,确保窗口一直显示
while True:
    # 检查事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:   # 如果单击关闭窗口,则退出
            sys.exit()
pygame.quit()     # 退出pygame
  • In the above code, polling event detection is added. pygame.event.get() can get the event queue, use for...in to traverse the event, and then judge the event type according to the type attribute. The event processing method here is similar to that of GUI. For example, event.type equal to pygame.GUIT means that the event of closing the pygame window is detected, pygame.KEYDOWN means the event of keyboard press , pygame.MOUSEBUTTONDOWN means the event of mouse press , etc.
    (3) Add a small ball in the window. We first prepare a ball.png image, then load the image, and finally display the image in the window:
# #######################1. 窗口一闪而过
# import sys    # 导入sys模块
# import pygame    # 导入pygame模块
#
# pygame.init()     # 初始化pygame
# size = width,height=640,480     # 设置窗口
# screen = pygame.display.set_mode(size)     # 显示窗口

# ##############################2. 窗口持续
# # -*-coding:utf-8 -*-
# import sys    # 导入sys模块
# import pygame   # 导入pygame模块
#
# pygame.init()     # 初始化pygame
# size = width, height = 640, 480    # 设置窗口
# screen = pygame.display.set_mode(size)   # 显示窗口
#
# # 执行死循环,确保窗口一直显示
# while True:
#     # 检查事件
#     for event in pygame.event.get():
#         if event.type == pygame.QUIT:   # 如果单击关闭窗口,则退出
#             sys.exit()
# pygame.quit()     # 退出pygame

# ######################3. 添加ball的窗口
# -*-coding:utf-8 -*-
import sys    # 导入sys模块
import pygame   # 导入pygame模块

pygame.init()     # 初始化pygame
size = width, height = 640, 480   # 设置窗口
screen  = pygame.display.set_mode(size)   # 显示窗口
color = (0, 0, 0)   # 设置颜色

ball = pygame.image.load("ball.png")    # 加载图片
ballrect = ball.get_rect()    # 获取矩形区域

# 执行死循环,确保窗口一直显示
while True:
    # 检查事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:    # 如果点击关闭窗口,则退出
            sys.exit()
        screen.fill(color)    # 填充颜色
        screen.blit(ball, ballrect)    # 将图片画到窗口上
        pygame.display.flip()    # 更新全部显示
pygame.quit()       # 退出pygame
  • The above code uses the load() method of the image module to load the image, and the return value ball is a Surface object. Surface is a pygame object used to represent a picture, and various operations such as painting, deformation, and copying can be performed on a Surface object. In fact, the screen is just a surface, and pygame.display.set_mode returns a screen Surface object. If you draw the Surface object of ball to the screen.Surface object, you need to use the blit() method, and finally use the flip method of the display module to update the entire Surface object to be displayed on the screen.
  • The result of the operation is as follows:
    insert image description here
  • Common methods of the Surface object are as follows:
method name Function
pygame.Surface.blit Draw an image onto another image.
pygame.Surface.convert Converts the pixel format of an image.
pygame.Surface.convert_alpha Convert the pixel format of the image, including the conversion of the alpha channel.
pygame.Surface.fill Fill Surface with color
pygame.Surface.get_rect Get the rectangular area of ​​Surface.

(4)下面该让小球动起来了。ball.get_rect()方法返回值ballrect()方法返回值ballrect是一个Rect对象,该对象有一个move()方法可用于移动矩形。move(x,y)函数有两个参数,第一个参数是X轴移动的距离,第二个参数是Y轴移动的距离。窗体左上角坐标为(0,0),例如move(100,50)。

  • 为实现小球不停地移动,将move()函数添加到while循环内:
# -*- coding:utf-8-*-
import sys
import pygame

pygame.init()      # 初始化pygame
size = width, height = 640, 480    # 设置窗口
screen = pygame.display.set_mode(size)    # 显示窗口
color = (0, 0, 0)    # 设置颜色

ball = pygame.image.load("ball.png")    # 加载图片
ballrect = ball.get_rect()       # 获取矩形区域

speed = [5, 5]     # 设置移动的X轴、Y轴距离
# 执行死循环,确保窗口一直显示
while True:
    # 检查事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:    # 如果单击关闭窗口,则退出
            sys.exit()
    ballrect = ballrect.move(speed)    # 移动小球
    screen.fill(color)    # 填充颜色
    screen.blit(ball, ballrect)     # 将图片画到窗口上
    pygame.display.flip()      # 更新全部显示
pygame.quit()     # 退出pygame

insert image description here

  • 运行上述代码,小球在屏幕中一闪而过,此时,小球并没有真正消失,而是移动到窗体外了,此时需要添加碰撞检测的功能。当小球与窗体的任一边缘发生碰撞,则改变小球的移动方向:
# -*- coding:utf-8 -*-
import sys    # 导入sys模块
import pygame     # 导入pygame模块

pygame.init()      # 初始化pygame
size = width, height = 640, 480    # 设置窗口
screen = pygame.display.set_mode(size)     # 显示窗口
color = (0, 0, 0)     # 设置窗口背景颜色

ball = pygame.image.load("ball.png")    # 加载图片
ballrect = ball.get_rect()    # 获取矩形区域

speed = [1, 1]      # 设置移动的X轴、Y轴距离
# 执行死循环,确保窗口一直显示
while True:
    # 检查事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:     # 如果单击关闭窗口,则退出
            sys.exit()
    ballrect = ballrect.move(speed)     # 移动小球
    # 碰到左右边缘
    if ballrect.left < 0 or ballrect.right > width:
        speed[0] = -speed[0]
    # 碰到上下边缘
    if ballrect.top < 0 or ballrect.bottom > height:
        speed[1] = -speed[1]
    screen.fill(color)      # 填充颜色
    screen.blit(ball, ballrect)     # 将图片画到窗口上
    pygame.display.flip()     # 更新全部显示

pygame.quit()     # 退出pygame
  • 上述代码中,添加了碰撞检测功能。如果球碰到左右边缘,则更改X轴数据为负数;如果碰到上下边缘,则改Y轴数据为负数。
  • 运行结果如下:
    insert image description here
    (6)运行上述代码发现好像有多个小球在飞快移动,这是因为运行上述代码的时间非常短,导致肉眼错觉,因此需要添加一个“时钟”来控制程序运行时间。用pygame的time模块控制。使用pygame时钟前,必须先创建一个Clock对象的一个实例,然后在while循环中设置多长时间运行一次。代码如下:
# -*- coding:utf-8 -*-
import sys    # 导入sys模块
import pygame    # 导入pygame模块

pygame.init()     # 初始化pygame
size = width, height = 640, 480    # 设置窗口
screen = pygame.display.set_mode(size)    # 显示窗口
color = (0, 0, 0)    # 设置颜色

ball = pygame.image.load("ball.png")    # 加载图片
ballrect = ball.get_rect()   # 获取矩形区域

speed = [5, 5]     # 设置移动的X轴、Y轴距离
clock = pygame.time.Clock()     # 设置时钟
# 执行死循环,确保窗口一直显示
while True:
    clock.tick(60)    # 每秒执行60次
    # 检查事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:     # 如果单击关闭窗口,则退出
            sys.exit()
    ballrect = ballrect.move(speed)    # 移动小球
    # 碰到左右边缘
    if ballrect.left < 0 or ballrect.right > width:
        speed[0] = -speed[0]
    # 碰到上下边缘
    if ballrect.top < 0 or ballrect.bottom > height:
        speed[1] = -speed[1]
    screen.fill(color)     # 填充颜色
    screen.blit(ball, ballrect)     # 将图片画到窗口上
    screen.blit(ball, ballrect)     # 将图片画到窗口上
    pygame.display.flip()    # 更新全部显示
pygame.quit()      # 退出pygame
  • 至此,就完成了跳跃的小球游戏。
  • 运行效果如下:
    insert image description here

3. 开发Flappy Bird游戏

1. 游戏简介

  • Flappy Bird是一款鸟类飞行游戏,由越南河内独立游戏开发者阮哈东(DongNguyen)开发。在Flappy Bird这款游戏中,玩家只需要用一根手指来操控,单击触摸手机屏幕,小鸟就会往上飞,不断地单击就会不断地往高处飞。放松手指,则会快速下降。所以玩家要控制小鸟一直向前飞行,然后注意躲避途中高低不平的管子。如果小鸟碰到了障碍物,游戏就会结束。每当小鸟飞过一组管道,玩家就会获得一分。

2. 游戏分析

  • 在Flappy Bird中,主要有两个对象:小鸟和管道。可以创建Bird类和Pineline类来分别表示这两个对象。小鸟可以通过上下移动来躲避管道,所以在Bird类中创建一个birdUpdate()方法,实现小鸟的上下移动。为了体现小鸟向前飞行的特征,可以让管道一直向左侧移动,这样在窗口中就好像小鸟在向前飞行。所以,在Pineline类中也创建了一个updatePipline()方法,实现管道的向左移动。此外,还创建了3个函数:createMap()函数用于绘制地图;checkDead()函数用于判断小鸟的生命状态;getResult()函数用于获取最终分数。最后在主逻辑中实例化类并调用相关方法,实现相应功能。

3. 搭建主框架

  • 通过前面分析,我们可以搭建起Flappy Bird游戏的主框架。Flappy Bird游戏有两个对象:小鸟和管道。先来创建这两个类,类中的具体方法可以先使用pass语句替代。然后创建一个绘制地图的函数createMap()。最后,在主逻辑中绘制背景图片。关键代码如下:
import pygame
import sys
import random


class Bird(object):
    """定义一个鸟类"""
    def __init__(self):
        """定义初始化方法"""
        pass

    def birdUpdate(self):
        pass


class Pipeline(object):
    """定义一个管道类"""
    def __init__(self):
        """定义初始化方法"""
        pass

    def updatePipeline(self):
        """水平移动"""
        pass


def createMap(screen, background):
    """定义创建地图的方法"""
    screen.fill((255, 255, 255))     # 填充颜色
    screen.blit(background, (0, 0))    # 填入到背景
    pygame.display.update()    # 更新显示


if __name__ == "__main__":
    """主程序"""
    pygame.init()     # 初始化pygame
    size = width, height = 400, 720    # 设置窗口
    screen = pygame.display.set_mode(size)    # 显示窗口
    clock = pygame.time.Clock()    # 设置时钟
    Pipeline = Pipeline()     # 实例化管道类
    Bird = Bird()    # 实例化鸟类
    while True:
        clock.tick(60)     # 每秒执行60次
        # 轮询事件
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
        background = pygame.image.load("assets/background.png")     # 加载背景图片
        createMap(screen, background)    # 绘制地图
    pygame.quit()    # 退出

  • 运行结果如下:
    insert image description here

4. 创建小鸟类

  • 下面创建小鸟类。该类需要初始化很多参数,所以定义一个__init__()方法,用来初始化各种参数,包括鸟飞行的几种状态、飞行的速度、跳跃的高度等。然后定义birdUpdate()方法,该方法用于实现小鸟的跳跃和坠落。接下来,在主逻辑的轮询事件中添加键盘按下事件或鼠标单击事件,如按下鼠标,使小鸟上升等。最后,在createMap()方法中显示小鸟的图像。
    Please add a picture description
    Please add a picture description
    Please add a picture description
import pygame
import sys
import random
# 素材参考地址:https://www.aigei.com/s?q=flappy+bird&type=2d


class Bird(object):
    """定义一个鸟类"""
    def __init__(self):
        """定义初始化方法"""
        self.birdRect = pygame.Rect(65, 50, 50, 50)    # 鸟的矩形
        # 定义鸟的3种状态列表
        self.birdStatus = [pygame.image.load("assets/1.png"),
                           pygame.image.load("assets/2.png"),
                           pygame.image.load("assets/dead.png")]
        self.status = 0   # 默认飞行状态
        self.birdX = 120   # 鸟所在X轴坐标
        self.birdY = 350    # 鸟所在Y轴坐标,即上下飞行高度
        self.jump = False      # 默认情况小鸟自动降落
        self.jumpSpeed = 10    # 跳跃高度
        self.gravity = 5    # 重力
        self.dead = False     # 默认小鸟生命状态为活着

    def birdUpdate(self):
        if self.jump:
            # 小鸟跳跃
            self.jumpSpeed -= 1    # 速度递减,上升越来越慢
            self.birdY -= self.jumpSpeed    # 鸟Y轴坐标减小,小鸟上升
        else:
            # 小鸟坠落
            self.gravity += 0.2     # 重力递增,下降越来越快
            self.birdY += self.gravity      # 鸟Y轴坐标增加,小鸟下降
        self.birdRect[1] = self.birdY    # 更改Y轴位置


class Pipeline(object):
    """定义一个管道类"""
    def __init__(self):
        """定义初始化方法"""
        pass

    def updatePipeline(self):
        """水平移动"""
        pass


def createMap(screen, background):
    """定义创建地图的方法"""
    screen.fill((255, 255, 255))     # 填充颜色
    screen.blit(background, (0, 0))    # 填入到背景
    # 显示小鸟
    if Bird.dead:     # 撞管道状态
        Bird.status = 2
    elif Bird.jump:    # 起飞状态
        Bird.status = 1
    screen.blit(Bird.birdStatus[Bird.status], (Bird.birdX, Bird.birdY))    # 设置小鸟的坐标
    Bird.birdUpdate()     # 鸟移动
    pygame.display.update()    # 更新显示


if __name__ == "__main__":
    """主程序"""
    pygame.init()     # 初始化pygame
    size = width, height = 400, 720    # 设置窗口
    screen = pygame.display.set_mode(size)    # 显示窗口
    clock = pygame.time.Clock()    # 设置时钟
    Pipeline = Pipeline()     # 实例化管道类
    Bird = Bird()    # 实例化鸟类
    while True:
        clock.tick(60)     # 每秒执行60次
        # 轮询事件
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if (event.type == pygame.KEYDOWN or event.type == pygame.MOUSEBUTTONDOWN) and not Bird.dead:
                Bird.jump = True     # 跳跃
                Bird.gravity = 5    # 重力
                Bird.jumpSpeed = 10     # 跳跃速度
        background = pygame.image.load("assets/background.png")     # 加载背景图片
        createMap(screen, background)    # 绘制地图
    pygame.quit()    # 退出


  • 上述代码在Bird类中设置了birdStatus属性,该属性是一个鸟类图片的列表,列表中显示鸟类的3种飞行状态,根据小鸟的不同状态加载相应的图片。在birdUpdate()方法中,为了达到较好的动画效果,使用jumpSpeed和gravity两个属性逐渐变化。运行上述代码,在窗体内创建一只鸟,默认情况下小鸟会一直下降。当单击一下鼠标或按一下键盘,小鸟会跳跃一下,高度上升。运行效果如下图:

insert image description here

5. 创建管道类

  • 创建完鸟类之后,我们来创建管道类。同样,在_init_()方法中初始化各种参数,包括设置管道的坐标,加载上下管道图片等。然后在updatePipline()方法中,定义管道向左移动的速度,并且当管道移出屏幕时重新绘制下一组管道。最后,在createMap()函数中显示管道。关键代码如下:
import pygame
import sys
import random
# 素材参考地址:https://www.aigei.com/s?q=flappy+bird&type=2d


class Bird(object):
    """定义一个鸟类"""
    def __init__(self):
        """定义初始化方法"""
        self.birdRect = pygame.Rect(65, 50, 50, 50)    # 鸟的矩形
        # 定义鸟的3种状态列表
        self.birdStatus = [pygame.image.load("assets/1.png"),
                           pygame.image.load("assets/2.png"),
                           pygame.image.load("assets/dead.png")]
        self.status = 0   # 默认飞行状态
        self.birdX = 120   # 鸟所在X轴坐标
        self.birdY = 350    # 鸟所在Y轴坐标,即上下飞行高度
        self.jump = False      # 默认情况小鸟自动降落
        self.jumpSpeed = 10    # 跳跃高度
        self.gravity = 5    # 重力
        self.dead = False     # 默认小鸟生命状态为活着

    def birdUpdate(self):
        if self.jump:
            # 小鸟跳跃
            self.jumpSpeed -= 1    # 速度递减,上升越来越慢
            self.birdY -= self.jumpSpeed    # 鸟Y轴坐标减小,小鸟上升
        else:
            # 小鸟坠落
            self.gravity += 0.2     # 重力递增,下降越来越快
            self.birdY += self.gravity      # 鸟Y轴坐标增加,小鸟下降
        self.birdRect[1] = self.birdY    # 更改Y轴位置


class Pipeline(object):
    """定义一个管道类"""
    def __init__(self):
        """定义初始化方法"""
        self.wallx = 400      # 管道所在X轴坐标
        self.pineUp = pygame.image.load("assets/top.png")     # 加载上管道图片
        self.pineDown = pygame.image.load("assets/bottom.png")     # 加载下管道图片

    def updatePipeline(self):
        """管道水平移动方法"""
        self.wallx -= 5      # 管道X轴坐标递减,即管道向左移动
        # 当管道运行到一定位置,即小鸟飞越管道,分数加1,并且管道重置
        if self.wallx < -80:
            self.wallx = 400


def createMap(screen, background):
    """定义创建地图的方法"""
    screen.fill((255, 255, 255))     # 填充颜色
    screen.blit(background, (0, 0))    # 填入到背景

    # 显示管道
    screen.blit(Pipeline.pineUp, (Pipeline.wallx, -300))     # 上管道坐标位置(X,Y)
    screen.blit(Pipeline.pineDown, (Pipeline.wallx, 500))    # 下管道坐标位置(X,Y)
    Pipeline.updatePipeline()      # 管道移动

    # 显示小鸟
    if Bird.dead:     # 撞管道状态
        Bird.status = 2
    elif Bird.jump:    # 起飞状态
        Bird.status = 1
    screen.blit(Bird.birdStatus[Bird.status], (Bird.birdX, Bird.birdY))    # 设置小鸟的坐标
    Bird.birdUpdate()     # 鸟移动

    pygame.display.update()    # 更新显示


if __name__ == "__main__":
    """主程序"""
    pygame.init()     # 初始化pygame
    size = width, height = 400, 720    # 设置窗口
    screen = pygame.display.set_mode(size)    # 显示窗口
    clock = pygame.time.Clock()    # 设置时钟
    Pipeline = Pipeline()     # 实例化管道类
    Bird = Bird()    # 实例化鸟类
    while True:
        clock.tick(60)     # 每秒执行60次
        # 轮询事件
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if (event.type == pygame.KEYDOWN or event.type == pygame.MOUSEBUTTONDOWN) and not Bird.dead:
                Bird.jump = True     # 跳跃
                Bird.gravity = 5    # 重力
                Bird.jumpSpeed = 10     # 跳跃速度
        background = pygame.image.load("assets/background.png")     # 加载背景图片
        createMap(screen, background)    # 绘制地图
    pygame.quit()    # 退出

  • 上述代码中,在createMap()函数中,设置先显示管道,再显示小鸟。这样做的目的是,当小鸟与管道图像重合的时候,小鸟的图像显示在上层,而管道的图像显示在底层。运行结果如下:
    Please add a picture description

6. 计算得分

  • 当小鸟飞过管道时,玩家得分加1.这里对于飞过管道的逻辑做了简化处理:当管道移动到窗体左侧一定距离后,默认小鸟飞过管道,使分数加1,并显示在屏幕上。在updatePipeline()方法中实现该功能。代码如下:
import pygame
import sys
import random
# 素材参考地址:https://www.aigei.com/s?q=flappy+bird&type=2d




class Bird(object):
    """定义一个鸟类"""
    def __init__(self):
        """定义初始化方法"""
        self.birdRect = pygame.Rect(65, 50, 50, 50)    # 鸟的矩形
        # 定义鸟的3种状态列表
        self.birdStatus = [pygame.image.load("assets/1.png"),
                           pygame.image.load("assets/2.png"),
                           pygame.image.load("assets/dead.png")]
        self.status = 0   # 默认飞行状态
        self.birdX = 120   # 鸟所在X轴坐标
        self.birdY = 350    # 鸟所在Y轴坐标,即上下飞行高度
        self.jump = False      # 默认情况小鸟自动降落
        self.jumpSpeed = 10    # 跳跃高度
        self.gravity = 5    # 重力
        self.dead = False     # 默认小鸟生命状态为活着

    def birdUpdate(self):
        if self.jump:
            # 小鸟跳跃
            self.jumpSpeed -= 1    # 速度递减,上升越来越慢
            self.birdY -= self.jumpSpeed    # 鸟Y轴坐标减小,小鸟上升
        else:
            # 小鸟坠落
            self.gravity += 0.2     # 重力递增,下降越来越快
            self.birdY += self.gravity      # 鸟Y轴坐标增加,小鸟下降
        self.birdRect[1] = self.birdY    # 更改Y轴位置


class Pipeline(object):
    """定义一个管道类"""
    def __init__(self):
        """定义初始化方法"""
        self.wallx = 400      # 管道所在X轴坐标
        self.pineUp = pygame.image.load("assets/top.png")     # 加载上管道图片
        self.pineDown = pygame.image.load("assets/bottom.png")     # 加载下管道图片

    def updatePipeline(self):
        """管道水平移动方法"""
        self.wallx -= 5      # 管道X轴坐标递减,即管道向左移动
        # 当管道运行到一定位置,即小鸟飞越管道,分数加1,并且管道重置
        if self.wallx < -80:
            global score
            score += 1
            self.wallx = 400


def createMap(screen, background, font):
    """定义创建地图的方法"""
    screen.fill((255, 255, 255))     # 填充颜色
    screen.blit(background, (0, 0))    # 填入到背景

    # 显示管道
    screen.blit(Pipeline.pineUp, (Pipeline.wallx, -100))     # 上管道坐标位置(X,Y)
    screen.blit(Pipeline.pineDown, (Pipeline.wallx, 500))    # 下管道坐标位置(X,Y)
    Pipeline.updatePipeline()      # 管道移动

    # 显示小鸟
    if Bird.dead:     # 撞管道状态
        Bird.status = 2
    elif Bird.jump:    # 起飞状态
        Bird.status = 1
    screen.blit(Bird.birdStatus[Bird.status], (Bird.birdX, Bird.birdY))    # 设置小鸟的坐标
    Bird.birdUpdate()     # 鸟移动

    # 显示分数
    screen.blit(font.render("score: " + str(score), -1, (255, 255, 255)), (230, 20))    # 设置颜色及坐标位置

    pygame.display.update()    # 更新显示


if __name__ == "__main__":
    """主程序"""
    pygame.init()     # 初始化pygame
    pygame.font.init()    # 初始化字体
    font = pygame.font.SysFont(None, 50)    # 设置默认字体和大小
    size = width, height = 400, 680    # 设置窗口
    screen = pygame.display.set_mode(size)    # 显示窗口
    clock = pygame.time.Clock()    # 设置时钟
    Pipeline = Pipeline()     # 实例化管道类
    Bird = Bird()    # 实例化鸟类
    score = 0    # 初始化分数
    while True:
        clock.tick(60)     # 每秒执行60次
        # 轮询事件
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if (event.type == pygame.KEYDOWN or event.type == pygame.MOUSEBUTTONDOWN) and not Bird.dead:
                Bird.jump = True     # 跳跃
                Bird.gravity = 5    # 重力
                Bird.jumpSpeed = 10     # 跳跃速度
        background = pygame.image.load("assets/background.png")     # 加载背景图片
        createMap(screen, background, font)    # 绘制地图
    pygame.quit()    # 退出

  • 运行效果如下:
    Please add a picture description

7. 碰撞检测

  • 当小鸟与管道相碰撞时,小鸟颜色变为灰色,游戏结束,并且显示总分数。在checkDead()函数中通过pygame.Rect()可以分别获取小鸟的矩形区域对象和管道的矩形区域对象,该对象有一个colliderect()方法可以判断俩各个矩形区域是否相碰撞。如果相碰撞,设置Bird.dead属性为True。此外,当小鸟飞出窗体时,也设置Bird.dead属性为True。最后,用两行文字显示总成绩。关键代码如下:
import pygame
import sys
import random
# 素材参考地址:https://www.aigei.com/s?q=flappy+bird&type=2d


class Bird(object):
    """定义一个鸟类"""
    def __init__(self):
        """定义初始化方法"""
        self.birdRect = pygame.Rect(65, 50, 50, 50)    # 鸟的矩形
        # 定义鸟的3种状态列表
        self.birdStatus = [pygame.image.load("assets/1.png"),
                           pygame.image.load("assets/2.png"),
                           pygame.image.load("assets/dead.png")]
        self.status = 0   # 默认飞行状态
        self.birdX = 120   # 鸟所在X轴坐标
        self.birdY = 350    # 鸟所在Y轴坐标,即上下飞行高度
        self.jump = False      # 默认情况小鸟自动降落
        self.jumpSpeed = 10    # 跳跃高度
        self.gravity = 5    # 重力
        self.dead = False     # 默认小鸟生命状态为活着

    def birdUpdate(self):
        if self.jump:
            # 小鸟跳跃
            self.jumpSpeed -= 1    # 速度递减,上升越来越慢
            self.birdY -= self.jumpSpeed    # 鸟Y轴坐标减小,小鸟上升
        else:
            # 小鸟坠落
            self.gravity += 0.2     # 重力递增,下降越来越快
            self.birdY += self.gravity      # 鸟Y轴坐标增加,小鸟下降
        self.birdRect[1] = self.birdY    # 更改Y轴位置


class Pipeline(object):
    """定义一个管道类"""
    def __init__(self):
        """定义初始化方法"""
        self.wallx = 400      # 管道所在X轴坐标
        self.pineUp = pygame.image.load("assets/top.png")     # 加载上管道图片
        self.pineDown = pygame.image.load("assets/bottom.png")     # 加载下管道图片

    def updatePipeline(self):
        """管道水平移动方法"""
        self.wallx -= 5      # 管道X轴坐标递减,即管道向左移动
        # 当管道运行到一定位置,即小鸟飞越管道,分数加1,并且管道重置
        if self.wallx < -80:
            global score
            score += 1
            self.wallx = 400


def createMap(screen, background, font):
    """定义创建地图的方法"""
    screen.fill((255, 255, 255))     # 填充颜色
    screen.blit(background, (0, 0))    # 填入到背景

    # 显示管道
    screen.blit(Pipeline.pineUp, (Pipeline.wallx, -100))     # 上管道坐标位置(X,Y)
    screen.blit(Pipeline.pineDown, (Pipeline.wallx, 500))    # 下管道坐标位置(X,Y)
    Pipeline.updatePipeline()      # 管道移动

    # 显示小鸟
    if Bird.dead:     # 撞管道状态
        Bird.status = 2
    elif Bird.jump:    # 起飞状态
        Bird.status = 1
    screen.blit(Bird.birdStatus[Bird.status], (Bird.birdX, Bird.birdY))    # 设置小鸟的坐标
    Bird.birdUpdate()     # 鸟移动

    # 显示分数
    screen.blit(font.render("score: " + str(score), -1, (255, 255, 255)), (230, 20))    # 设置颜色及坐标位置

    pygame.display.update()    # 更新显示


def checkDead():
    # 上方管子的矩形位置
    upRect = pygame.Rect(Pipeline.wallx, -100,Pipeline.pineUp.get_width() - 10, Pipeline.pineUp.get_height())
    # 下方管子的矩形位置
    downRect = pygame.Rect(Pipeline.wallx, 500, Pipeline.pineDown.get_width() - 10, Pipeline.pineDown.get_height())
    # 检测小鸟与上下方管子是否碰撞
    if upRect.colliderect(Bird.birdRect) or downRect.colliderect(Bird.birdRect):
        Bird.dead = True
        return True
    else:
        return False


def getResult():
    final_text1 = "Game over"
    final_text2 = "Your final score is: " + str(score)
    ft1_font = pygame.font.SysFont("Arial", 70)      # 设置第一行文字字体
    ft1_surf = ft1_font.render(final_text1, 1, (242, 3, 36))     # 设置第一行文字的颜色
    ft2_font = pygame.font.SysFont("Arial", 50)     # 设置第二行文字字体
    ft2_surf = ft2_font.render(final_text2, 1, (253, 177, 6))    # 设置第二行文字颜色
    # 设置第一行文字显示位置
    screen.blit(ft1_surf, [screen.get_width() / 2 - ft1_surf.get_width() / 2, 100])
    # 设置第二行文字显示位置
    screen.blit(ft2_surf, [screen.get_width() / 2 - ft2_surf.get_width() / 2, 200])

    pygame.display.flip()    # 更新整个待显示的Surface对象到屏幕上


if __name__ == "__main__":
    """主程序"""
    pygame.init()     # 初始化pygame
    pygame.font.init()    # 初始化字体
    font = pygame.font.SysFont(None, 50)    # 设置默认字体和大小
    size = width, height = 400, 680    # 设置窗口
    screen = pygame.display.set_mode(size)    # 显示窗口
    clock = pygame.time.Clock()    # 设置时钟
    Pipeline = Pipeline()     # 实例化管道类
    Bird = Bird()    # 实例化鸟类
    score = 0    # 初始化分数
    while True:
        clock.tick(60)     # 每秒执行60次
        # 轮询事件
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if (event.type == pygame.KEYDOWN or event.type == pygame.MOUSEBUTTONDOWN) and not Bird.dead:
                Bird.jump = True     # 跳跃
                Bird.gravity = 5    # 重力
                Bird.jumpSpeed = 10     # 跳跃速度
        background = pygame.image.load("assets/background.png")     # 加载背景图片
        if checkDead():   # 检测小鸟生命状态
            getResult()     # 如果小鸟死亡,游戏结束,显示游戏总分数
        else:
            createMap(screen, background, font)    # 绘制地图
    pygame.quit()    # 退出

  • In the checkDead() method of the above code, upRect.colliderect(Bird.birdRect) is used to detect whether the bird’s rectangular area collides with the upper pipe’s rectangular area, and the parameter of the colliderect() function is another rectangular area object. The death of the bird after the collision is as follows:

insert image description here

  • This example has realized the basic functions of Flappy Bird, but there are still many areas to be improved, such as setting the difficulty of the game, including setting the height of the pipe, the flying speed of the bird, etc.

4. Summary

  • It mainly explains how to use Pygame to develop games. First understand the basic usage of Pygame through a jumping ball game, and then use Python to gradually develop a well-known game Flappy Bird. Through the study of this chapter, you can master the basic knowledge of Pygame, and use the Python object-oriented way of thinking to develop a small Python game, and further experience the fun of Python programming.

Guess you like

Origin blog.csdn.net/ungoing/article/details/130812087