The pygame library pgu uses sample code
- 2021-11-24 02:24:48
- OfStack
Preface
Now more and more people use pygame to make small games, but pygame has no pop-up mechanism
To solve this problem, we will use tkinter library or pgu library in two ways
pgu library is not very suitable for novice introduction of a manual, only download the file of a number of function examples and explanations, so this article mainly introduces pgu by the button and set events triggered by two ways
In addition, it also solves the problem of pop-up window under the window of pygame
1. What is pgu?
The full name of pgu is Phil 's pyGame Utilities, which is a group of modules and scripts of pygame, and gui integrates one small module
Download address
Download address
pgu Home Page
2. Use steps
1. Install the library
Open cmd under window10 and enter pip install pygame-pgu
You can also download the package directly with the above address
2. Make a button pop-up window
The code is as follows (example):
Introducing library
import pygame
import random
import pgu
from pgu import gui,timer
First, use pygame to generate a 500*500 window. There are many pop-ups in the example of pgu, but there are few pop-ups in the window generated by pygame
pygame.init()
screencaption = pygame.display.set_caption('chess')
screen = pygame.display.set_mode((500, 500))
Construct a custom pop-up window (dialog) class
class TestDialog(gui.Dialog):
def __init__(this):
title = gui.Label("Some Dialog Box") # Pop-up title
label = gui.Label("Close this window to resume.") # Pop-up contents
gui.Dialog.__init__(this, title, label)
Initialization, pgu had better generate a container to store the buttons needed to be placed, and finally remember init initialization
app = gui.App() # Initialization gui
c = gui.Container(align=-1,valign=-1) # Generate gui Container of
abc = TestDialog() # Generate a pop-up window abc
btn = gui.Button("a") # Generate text as a Button of
btn.connect(gui.CLICK, abc.open, None)# Bind the button to the pop-up of the pop-up window
c.add(btn,0,0) # Place the button in the container (0,0) Position
app.init(c)
First use pygame to generate a 500*500 window, and then use the pop-up elements imported from pgu
pygame.init()
screencaption = pygame.display.set_caption('chess')
screen = pygame.display.set_mode((500, 500))
The main function is the pygame event loop retrieval mechanism, but it should be noted that the pygame event is transmitted to pgu using app. event function, otherwise the command of pgu cannot be executed
while True:
for e in pygame.event.get():
if e.type is pygame.QUIT:
pygame.quit()
else:
app.event(e) # Will pygame The event is passed to the pgu , very important
screen.fill((0,0,0)) # Generate 1 Screens
app.paint() # Will pgu Draw the contents of the container
pygame.display.update()
3. Make an event trigger pop-up window
In addition to triggering events through buttons on the interface, we often need to trigger the pop-up window by using an event. Here, the variable a is set, and every time the button a is pressed, it will increase by 1 until a is greater than 5 to trigger the pop-up event of the pop-up window
Introducing library
a = 0
Bind the self-increasing function of a to the button
def add(self):
global a
a = a + 1
btn.connect(gui.CLICK, add, None)# Bind the button to the pop-up of the pop-up window
Add the judgment state function, and pop-up window can be popped up when it is judged as True
if a > 5:
abc.open()
a = 0
4. Complete code for both modes
Button triggers pop-up window
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() # Initialization gui
c = gui.Container(align=-1,valign=-1) # Generate gui Container of
abc = TestDialog() # Generate a pop-up window abc
btn = gui.Button("a") # Generate text as a Button of
btn.connect(gui.CLICK, abc.open, None)# Bind the button to the pop-up of the pop-up window
c.add(btn,0,0) # Place the button in the container (0,0) Position
app.init(c)
while True:
for e in pygame.event.get():
if e.type is pygame.QUIT:
pygame.quit()
else:
app.event(e) # Will pygame The event is passed to the pgu , very important
screen.fill((0,0,0)) # Generate 1 Screens
app.paint() # Will pgu The contents of the container are drawn
pygame.display.update()
Event trigger pop-up code
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() # Initialization gui
c = gui.Container(align=-1,valign=-1) # Generate gui Container of
abc = TestDialog() # Generate a pop-up window abc
btn = gui.Button("a") # Generate text as a Button of
def add(self):
global a
a = a + 1
btn.connect(gui.CLICK, add, None)# Bind the button to the pop-up of the pop-up window
c.add(btn,0,0) # Place the button in the container (0,0) Position
app.init(c)
while True:
for e in pygame.event.get():
if e.type is pygame.QUIT:
pygame.quit()
else:
app.event(e) # Will pygame The event is passed to the pgu , very important
screen.fill((0,0,0)) # Generate 1 Screens
app.paint() # Will pgu The contents of the container are drawn
if a > 5:
abc.open()
a = 0
pygame.display.update()
When I tried, I found:
1. If there is no function of screen. fill, the pop-up window will not be closed
2. The pgu event cannot be executed without app. event
Summarize
The content of pgu is less than that of tkinter, and it will be more stable than that of pygame, but the hard injury of pgu is that it can't display Chinese interface, so it is best to use tkinter for this requirement