SoFunction
Updated on 2024-11-14

Example code for using pygame library pgu

preamble

Now more and more people use pygame to make small games, but pygame it is no pop-up window mechanism
Generally, we use either the tkinter library or the pgu library to solve this problem.
which pgu library is not yet very suitable for newcomers to a manual introduction, only to download the file in some of the functions of the example and description, so this article mainly introduces pgu triggered by the button and set the event of the two ways
Also solved the problem of pop-ups under pygame's window

I. What is pgu?

The full name of pgu is Phil's pyGame Utilities, a set of modules and scripts for pygame, which also has gui integrated with some smaller modules

download address

download address

pgu homepage

II. Steps for use

1. Installation of libraries

Open cmd under window10 and type pip install pygame-pgu.
You can also download the package directly using the address above

2. Make a button pop-up window

The code is as follows (example):
repository

import pygame
import random
import pgu 
from pgu import gui,timer

First use pygame to generate a 500*500 window, there are many popups in pgu's example, but fewer popups on the pygame-generated window

()
screencaption = .set_caption('chess')
screen = .set_mode((500, 500))

Construct a custom dialog class.

class TestDialog():
    def __init__(this):
        title = ("Some Dialog Box")                   #Popup Title
        label = ("Close this window to resume.")      # pop-up content
        .__init__(this, title, label)

Initialization, pgu is best to generate a container to store the buttons that need to be put, etc., and finally remember to init initialize the

app = ()                       # Initialize the gui
c = (align=-1,valign=-1) # Generate the container for the gui
abc = TestDialog()                    #Generate popups abc
btn = ("a")                 # Generate buttons with text a
(, , None)#Bind the button to the popup of a popup window
(btn,0,0)                        # Place the button at container (0,0) position
(c)    

First use pygame to generate a 500*500 window, then import the pgu popup elements with

()
screencaption = .set_caption('chess')
screen = .set_mode((500, 500))

The main function is pygame's event loop fetch mechanism, but you need to be careful to use the function to pass pygame's events to pgu, otherwise you can't execute pgu's commands

while True:
    for e in ():
        if  is : 
            ()
        else:
            (e)    #Pass pygame events to pgu, important!
    ((0,0,0))    # Generate a screen
    ()             #Draw the contents of the pgu container
    ()

3. Creating event-triggered pop-up windows

In addition to triggering events through buttons on the interface, we often also need to use an event that occurs to trigger a pop-up window, where the variable a is set, every time a button is pressed a increases by 1 until a is greater than 5 when the pop-up window is triggered by the pop-up event.
repository

a = 0

Bind the self-increment function of a to the button

def add(self):
    global a
    a = a + 1
(, add, None)#Bind the button to the popup of the popup window

Add a state function that pops up a popup window when it is judged to be True.

 if a > 5:
        ()
        a = 0

4. Complete code for both models

Button triggered popups

import pygame
import random
import pgu 
from pgu import gui,timer


class TestDialog():
    def __init__(this):
        title = ("Some Dialog Box")
        label = ("Close this window to resume.")
        .__init__(this, title, label)


()
screencaption = .set_caption('chess')
screen = .set_mode((500, 500))
app = ()                       # Initialize the gui
c = (align=-1,valign=-1) # Generate the container for the gui
abc = TestDialog()                    #Generate popups abc
btn = ("a")                 # Generate buttons with text a
(, , None)#Bind the button to the popup of a popup window
(btn,0,0)                       # Place the button at container (0,0) position
(c)                           

while True:
    for e in ():
        if  is : 
            ()
        else:
            (e)    #Pass pygame events to pgu, important!
    ((0,0,0))    # Generate a screen
    ()             #Draw the contents of the pgu container
    ()

Event Triggered Popup Code

import pygame
import random
import pgu 
from pgu import gui,timer


class TestDialog():
    def __init__(this):
        title = ("Some Dialog Box")
        label = ("Close this window to resume.")
        .__init__(this, title, label)


()
screencaption = .set_caption('chess')
screen = .set_mode((500, 500))
global a 
a = 0
app = ()                       # Initialize the gui
c = (align=-1,valign=-1) # Generate the container for the gui
abc = TestDialog()                    #Generate popups abc
btn = ("a")                 # Generate buttons with text a
def add(self):
    global a
    a = a + 1
(, add, None)#Bind the button to the popup of a popup window
(btn,0,0)                       # Place the button at container (0,0) position
(c)                           

while True:
    for e in ():
        if  is : 
            ()
        else:
            (e)    #Pass pygame events to pgu, important!
    ((0,0,0))    # Generate a screen
    ()             #Draw the contents of the pgu container
    if a > 5:
        ()
        a = 0
    ()

I found this out in person when I tried it:
1, if there is no function of the pop-up window will not be able to close the
2. If not, the pgu event cannot be executed.

summarize

pgu's library online content is less, and more than tkinter use in pygame will be more stable, but pgu's hard to be unable to display the Chinese interface, so there is a need in this regard, it is best to use the tkinter

to this article on the use of pygame library pgu introduction to this article, more related to the use of pygame library pgu content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!