目录
- 前言
 - 一、pgu是什么?
 - 二、使用步骤
 -  1.安装库
 - 2.制作按钮弹窗
 - 3.制作事件触发弹窗
 - 4.两种模式完整代码
 
- 总结
 
前言
现在用pygame制作小游戏的人越来越多,但是pygame它是没有弹窗机制的
一般解决这个问题我们会使用tkinter库或者pgu库两种方式
其中pgu库还没有很适合新手的一个手册介绍,只有下载文件中的一些函数的例子与说明,因此本文主要介绍pgu由按钮与设定事件触发的两种方式
此外还解决了在pygame的窗口下弹窗的问题
一、pgu是什么?
pgu全称是Phil's pyGame Utilities,是pygame的一组模块与脚本,其中还有gui集成了一些小模块
下载地址
下载地址
pgu主页
二、使用步骤
 1.安装库
window10下打开cmd输入pip install pygame-pgu即可
也可以用上面的地址直接下载包
2.制作按钮弹窗
代码如下(示例):
引入库
import pygame
import random
import pgu 
from pgu import gui,timer
 
先用pygame生成一个500*500的窗口,pgu的例子里有许多弹窗,但是在pygame生成的窗口上弹窗的比较少
pygame.init()
screencaption = pygame.display.set_caption('chess')
screen = pygame.display.set_mode((500, 500))
 
构造一个自定义的弹窗(dialog)类
class TestDialog(gui.Dialog):
    def __init__(this):
        title = gui.Label("Some Dialog Box")                   #弹窗标题
        label = gui.Label("Close this window to resume.")      #弹窗内容
        gui.Dialog.__init__(this, title, label)
 
初始化,pgu最好生成一个容器来存放需要放的按钮等,最后记得init初始化
app = gui.App()                       #初始化gui
c = gui.Container(align=-1,valign=-1) #生成gui的容器
abc = TestDialog()                    #生成弹窗abc
btn = gui.Button("a")                 #生成文字为a的按钮
btn.connect(gui.CLICK, abc.open, None)#将按钮与弹窗的弹出绑定
c.add(btn,0,0)                        #将按钮安放在容器(0,0)位置
app.init(c)    
 
先用pygame生成一个500*500的窗口,再用导入pgu的弹窗元素
pygame.init()
screencaption = pygame.display.set_caption('chess')
screen = pygame.display.set_mode((500, 500))
 
主函数就是pygame的事件循环获取机制,但是需要注意的是用app.event函数将pygame的事件传达到pgu中,否则无法执行pgu的命令
while True:
    for e in pygame.event.get():
        if e.type is pygame.QUIT: 
            pygame.quit()
        else:
            app.event(e)    #将pygame的事件传递给pgu,很重要
    screen.fill((0,0,0))    #生成一个屏幕
    app.paint()             #将pgu容器的内容画出来
    pygame.display.update()
 
3.制作事件触发弹窗
除了通过界面上的按钮触发事件,我们常常也需要采用发生了某个事件触发弹窗,这里设置了变量a,每按动一次按钮a增加1,直到a大于5时触发弹窗弹出事件
引入库
a = 0
将a的自增函数绑定到按钮上
def add(self):
    global a
    a = a + 1
btn.connect(gui.CLICK, add, None)#将按钮与弹窗的弹出绑定
 
加入判断状态函数,判断为True时弹出弹窗即可
 if a > 5:
        abc.open()
        a = 0
 
4.两种模式完整代码
按钮触发弹窗
import pygame
import random
import pgu 
from pgu import gui,timer
class TestDialog(gui.Dialog):
    def __init__(this):
        title = gui.Label("Some Dialog Box")
        label = gui.Label("Close this window to resume.")
        gui.Dialog.__init__(this, title, label)
pygame.init()
screencaption = pygame.display.set_caption('chess')
screen = pygame.display.set_mode((500, 500))
app = gui.App()                       #初始化gui
c = gui.Container(align=-1,valign=-1) #生成gui的容器
abc = TestDialog()                    #生成弹窗abc
btn = gui.Button("a")                 #生成文字为a的按钮
btn.connect(gui.CLICK, abc.open, None)#将按钮与弹窗的弹出绑定
c.add(btn,0,0)                       #将按钮安放在容器(0,0)位置
app.init(c)                           
while True:
    for e in pygame.event.get():
        if e.type is pygame.QUIT: 
            pygame.quit()
        else:
            app.event(e)    #将pygame的事件传递给pgu,很重要
    screen.fill((0,0,0))    #生成一个屏幕
    app.paint()             #将pgu容器的内容画出
    pygame.display.update()
 
事件触发弹窗代码
import pygame
import random
import pgu 
from pgu import gui,timer
class TestDialog(gui.Dialog):
    def __init__(this):
        title = gui.Label("Some Dialog Box")
        label = gui.Label("Close this window to resume.")
        gui.Dialog.__init__(this, title, label)
pygame.init()
screencaption = pygame.display.set_caption('chess')
screen = pygame.display.set_mode((500, 500))
global a 
a = 0
app = gui.App()                       #初始化gui
c = gui.Container(align=-1,valign=-1) #生成gui的容器
abc = TestDialog()                    #生成弹窗abc
btn = gui.Button("a")                 #生成文字为a的按钮
def add(self):
    global a
    a = a + 1
btn.connect(gui.CLICK, add, None)#将按钮与弹窗的弹出绑定
c.add(btn,0,0)                       #将按钮安放在容器(0,0)位置
app.init(c)                           
while True:
    for e in pygame.event.get():
        if e.type is pygame.QUIT: 
            pygame.quit()
        else:
            app.event(e)    #将pygame的事件传递给pgu,很重要
    screen.fill((0,0,0))    #生成一个屏幕
    app.paint()             #将pgu容器的内容画出
    if a > 5:
        abc.open()
        a = 0
    pygame.display.update()
 
本人在尝试时发现:
1、如果没有screen.fill的函数弹出的窗口会无法关闭
2、如果没有app.event则pgu事件无法执行
总结
pgu的库网上内容较少,且较之tkinter运用在pygame上会更加稳定,但是pgu的硬伤是无法显示中文的界面,因此有这方面的需求最好还是使用tkinter
到此这篇关于pygame库pgu使用介绍的文章就介绍到这了,更多相关pygame库pgu使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
                            
                            
                                您可能感兴趣的文章:- 使用Python第三方库pygame写个贪吃蛇小游戏
 - python运用pygame库实现双人弹球小游戏
 - pygame库实现移动底座弹球小游戏
 - pygame库实现俄罗斯方块小游戏
 - 使用PyInstaller将Pygame库编写的小游戏程序打包为exe文件及出现问题解决方法
 - 详解使用PyInstaller将Pygame库编写的小游戏程序打包为exe文件