pygame初步探索

https://www.bilibili.com/video/BV1XW411E7vb?p=1

一、python的IDE(集成开发环境)工具

IDE工具

二、pygame 模块的安装(以pycharm为例)以及简单介绍

如果你的电脑使用pycharm编辑器,可以通过以下方式安装:

pip install pygame

pip是python的包管理工具,就是第三方库安装、下载的管家。
Pygame是Python最经典的2D游戏开发第三方库,也支持3D游戏开发。
pygame适用于游戏逻辑验证、游戏入门及系统演示验证;pygame是一种游戏开发引擎,基本逻辑具有参考价值。

三、小试牛刀

1.pygame模块中自带演示小游戏
python -m pygame.examples.aliens
2.pygame最小开发框架

sys是Python的标准库
sys提供Python运行时环境变量的操控,sys.exit()用于退出结束游戏并退出

#导入pygame和sys库
import pygame
import sys

#初始化init()及设置
pygame.init()
screen = pygame.display.set_mode((600,400))
pygame.display.set_caption("Pygame游戏之旅")

#获取事件并逐类响应>>>重复执行
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
    pygame.display.update()
3.壁球小游戏(展示型)与图像的基本使用

关键要素

小球的链接
https://python123.io/PY15/PYG02-ball.gif
笛卡尔坐标系

分析

surface对象

Rect对象

矩形移动

壁球的反弹运动

背景颜色填充

让图像在另一个图像上

#导入pygame和sys
import pygame,sys

#初始化设置
pygame.init()
#定义窗口宽度和高度
size = width,height =600,400
#速度列表
speed = [1,1]
#配置背景颜色
BLACK = 0,0,0
#设置窗口的宽度和高度
screen = pygame.display.set_mode(size)
#设置游戏的名称
pygame.display.set_caption("Python壁球")
#导入图片
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()
    #设置角色的偏移量,即在横轴方向移动x像素,纵轴方向移动y像素,xy为整数
    ballrect = ballrect.move(speed[0],speed[1])
    #壁球的反弹运动
    if ballrect.left < 0 or ballrect.right > width:
        speed[0] = -speed[0]
    if ballrect.top<0 or ballrect.bottom > height:
        speed[1] = -speed[1]
    #显示窗口背景填充为color颜色,采用RGB色彩体系
    screen.fill(BLACK)
    #通过Rect对象引导对壁球的绘制
    screen.blit(ball, ballrect)
    #刷新屏幕
    pygame.display.update()
4.壁球小游戏(节奏型)与屏幕的帧率设置
关键要素

操作时间

控制帧速度
#导入pygame和sys
import pygame,sys

#初始化设置
pygame.init()
#定义窗口宽度和高度
size = width,height =600,400
#速度列表
speed = [1,1]
#配置背景颜色
BLACK = 0,0,0
#设置窗口的宽度和高度
screen = pygame.display.set_mode(size)
#设置游戏的名称
pygame.display.set_caption("Python壁球")
#导入图片
ball = pygame.image.load("ball.png")
#获得矩形图形
ballrect = ball.get_rect()
fps = 300#每秒帧数参数
#创建一个Clock对象,用于操作时间
fclock = pygame.time.Clock()



#获取事件并逐类响应
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
    #设置角色的偏移量,即在横轴方向移动x像素,纵轴方向移动y像素,xy为整数
    ballrect = ballrect.move(speed[0],speed[1])
    #壁球的反弹运动
    if ballrect.left < 0 or ballrect.right > width:
        speed[0] = -speed[0]
    if ballrect.top<0 or ballrect.bottom > height:
        speed[1] = -speed[1]
    #显示窗口背景填充为color颜色,采用RGB色彩体系
    screen.fill(BLACK)
    #通过Rect对象引导对壁球的绘制
    screen.blit(ball, ballrect)
    #刷新屏幕
    pygame.display.update()
    fclock.tick(fps)
5.壁球小游戏(操控型)与键盘的基本使用
需求要素


键盘敲击事件
#导入pygame和sys
import pygame,sys

#初始化设置
pygame.init()
#定义窗口宽度和高度
size = width,height =600,400
#速度列表
speed = [1,1]
#配置背景颜色
BLACK = 0,0,0
#设置窗口的宽度和高度
screen = pygame.display.set_mode(size)
#设置游戏的名称
pygame.display.set_caption("Python壁球")
#导入图片
ball = pygame.image.load("ball.png")
#获得矩形图形
ballrect = ball.get_rect()
fps = 300#每秒帧数参数
#创建一个Clock对象,用于操作时间
fclock = pygame.time.Clock()



#获取事件并逐类响应
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                speed[0] = speed[0] if speed[0] == 0 else(abs(speed[0]-1)*int(speed[0]/abs(speed[0])))
            elif event.key == pygame.K_RIGHT:
                speed[0] = speed[0] + 1 if speed[0]>0 else speed[0] - 1
            elif event.key == pygame.K_UP:
                speed[1] = speed[1] +1 if speed[1] > 0 else speed - 1
            elif event.key == pygame.K_DOWN:
                speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1])-1)*int(speed[1]/abs(speed[1]))
    #设置角色的偏移量,即在横轴方向移动x像素,纵轴方向移动y像素,xy为整数
    ballrect = ballrect.move(speed[0],speed[1])
    #壁球的反弹运动
    if ballrect.left < 0 or ballrect.right > width:
        speed[0] = -speed[0]
    if ballrect.top<0 or ballrect.bottom > height:
        speed[1] = -speed[1]
    #显示窗口背景填充为color颜色,采用RGB色彩体系
    screen.fill(BLACK)
    #通过Rect对象引导对壁球的绘制
    screen.blit(ball, ballrect)
    #刷新屏幕
    pygame.display.update()
    fclock.tick(fps)

四、pygame屏幕绘制机制

pygame使用pygame.display来控制pygame的游戏屏幕,在pygame中只有一个游戏屏幕,如果我们想要切换到多个屏幕,我们只能在此屏幕消失后,生成新的屏幕。
使用笛卡尔坐标系,左上方是坐标原点,横方向是x轴,纵方向是y轴。屏幕的尺寸以像素为单位。


屏幕绘制的重要函数
pygame.display.set_mode()可以设置游戏屏幕的尺寸,以及相关的模式。
屏幕模式函数

注意:每种显示方式要配合相应的处理机制





Pygame窗口标题和图标设置
标题和图标函数



pygame中surface对象是唯一的与图像对应的对象



pygame窗口感知和刷新设置
屏幕控制函数


flip()函数会重新绘制窗口,update()函数仅仅会绘制变化的地方。

五、pygame的事件处理机制








注意:unicode码与平台有关,不推荐使用




鼠标事件及类型的使用





move()函数的参数xy是相对位置


事件处理重要函数



在pygame的设计中,事件队列只能存储108个事件,会将所有新到的事件丢弃掉。

#每帧刷新一次,并产生一次事件
#导入pygame和sys库
import pygame
import sys

#初始化init()及设置
pygame.init()
screen = pygame.display.set_mode((600,400))
pygame.display.set_caption("Pygame事件处理")
fps = 1
fclock = pygame.time.Clock()
num = 1


#获取事件并逐类响应>>>重复执行
while True:
    uevent = pygame.event.Event(pygame.KEYDOWN,{"unicode":123,"key":pygame.K_SPACE,"mod":pygame.KMOD_ALT})
    pygame.event.post(uevent)
    num = num+1
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.unicode =="":
                print("[KEYDOWN{}]".format(num),"#",event.key,event.mod)
            else:
                print("[KEYDOWN{}]".format(num),event.unicode,event.key,event.mod)
    pygame.display.update()
    fclock.tick(fps)

六.pygame的色彩与图形绘制机制

色彩表达

一些常用颜色



#导入pygame和sys
import pygame,sys

#初始化设置
pygame.init()
#图标导入并设置图标
icon=pygame.image.load("flower.png")#load()函数生成的是surflower图形对象,保存到变量里
pygame.display.set_icon(icon)
#定义窗口宽度和高度
size = width,height =600,400
#速度列表
speed = [1,1]
#配置背景颜色
BLACK = 0,0,0
#设置窗口的宽度和高度
screen = pygame.display.set_mode(size)
#设置游戏的名称
pygame.display.set_caption("Python壁球")
#导入图片
ball = pygame.image.load("ball.png")
#获得矩形图形
ballrect = ball.get_rect()
fps = 300#每秒帧数参数
#创建一个Clock对象,用于操作时间
fclock = pygame.time.Clock()
still = False
bgcolor = pygame.Color("black")

#定义通道函数
def RGBChannel(a):
    return 0 if a<0 else (255 if a>255 else int(a))

#获取事件并逐类响应
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                speed[0] = speed[0] if speed[0] == 0 else(abs(speed[0]-1)*int(speed[0]/abs(speed[0])))
            elif event.key == pygame.K_RIGHT:
                speed[0] = speed[0] + 1 if speed[0]>0 else speed[0] - 1
            elif event.key == pygame.K_UP:
                speed[1] = speed[1] +1 if speed[1] > 0 else speed[1] - 1
            elif event.key == pygame.K_DOWN:
                speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1])-1)*int(speed[1]/abs(speed[1]))
        elif event.type == pygame.VIDEORESIZE:
            size = width,height=event.size[0],event.size[1]
            screen = pygame.display.set_mode(size,pygame.RESIZABLE)
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                still = True
        elif event.type == pygame.MOUSEBUTTONUP:
            still = False
            if event.button == 1:
                ballrect = ballrect.move(event.pos[0]-ballrect.left,event.pos[1]-ballrect.top)
        elif event.type == pygame.MOUSEMOTION:
            if event.buttons[0] == 1:
                ballrect = ballrect.move(event.pos[0]-ballrect.left,event.pos[1]-ballrect.top)

    #将屏幕控制函数
    if pygame.display.get_active() and not still:
        #设置角色的偏移量,即在横轴方向移动x像素,纵轴方向移动y像素,xy为整数
        ballrect = ballrect.move(speed[0],speed[1])#move()函数处理的是相对位置
    #壁球的反弹运动
    if ballrect.left < 0 or ballrect.right > width:
        speed[0] = -speed[0]
        if ballrect.right>width and ballrect.right + speed[0]>ballrect.right:
            speed[0] = -speed[0]
    if ballrect.top<0 or ballrect.bottom > height:
        speed[1] = -speed[1]
        if ballrect.bottom>height and ballrect.bottom + speed[1] > ballrect.bottom:
            speed[1] = -speed[1]
    #建立颜色通道
    bgcolor.r = RGBChannel(ballrect.left*255/width)
    bgcolor.g = RGBChannel(ballrect.top*255/height)
    bgcolor.b = RGBChannel(min(speed[0],speed[1])*255/max(speed[0],speed[1]))

    #显示窗口背景填充为color颜色,采用RGB色彩体系
    # screen.fill(BLACK)
    screen.fill(bgcolor)
    #通过Rect对象引导对壁球的绘制
    screen.blit(ball, ballrect)
    #刷新屏幕
    pygame.display.update()
    fclock.tick(fps)













pygame的文字绘制机制



freetype()是绘制文字的增强方法,我们必须使用import将它引用







七、pygame绘制机制原理精髓





你可能感兴趣的:(pygame初步探索)