人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動(dòng)態(tài)

pygame庫(kù)pgu使用示例代碼

發(fā)布日期:2022-02-02 13:42 | 文章來源:CSDN

現(xiàn)在用pygame制作小游戲的人越來越多,但是pygame它是沒有彈窗機(jī)制的
一般解決這個(gè)問題我們會(huì)使用tkinter庫(kù)或者pgu庫(kù)兩種方式
其中pgu庫(kù)還沒有很適合新手的一個(gè)手冊(cè)介紹,只有下載文件中的一些函數(shù)的例子與說明,因此本文主要介紹pgu由按鈕與設(shè)定事件觸發(fā)的兩種方式
此外還解決了在pygame的窗口下彈窗的問題

一、pgu是什么?

pgu全稱是Phil's pyGame Utilities,是pygame的一組模塊與腳本,其中還有g(shù)ui集成了一些小模塊

下載地址

下載地址

pgu主頁(yè)

二、使用步驟

1.安裝庫(kù)

window10下打開cmd輸入pip install pygame-pgu即可
也可以用上面的地址直接下載包

2.制作按鈕彈窗

代碼如下(示例):
引入庫(kù)

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

先用pygame生成一個(gè)500*500的窗口,pgu的例子里有許多彈窗,但是在pygame生成的窗口上彈窗的比較少

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

構(gòu)造一個(gè)自定義的彈窗(dialog)類

class TestDialog(gui.Dialog):
 def __init__(this):
  title = gui.Label("Some Dialog Box") #彈窗標(biāo)題
  label = gui.Label("Close this window to resume.")#彈窗內(nèi)容
  gui.Dialog.__init__(this, title, label)

初始化,pgu最好生成一個(gè)容器來存放需要放的按鈕等,最后記得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生成一個(gè)500*500的窗口,再用導(dǎo)入pgu的彈窗元素

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

主函數(shù)就是pygame的事件循環(huán)獲取機(jī)制,但是需要注意的是用app.event函數(shù)將pygame的事件傳達(dá)到pgu中,否則無法執(zhí)行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)) #生成一個(gè)屏幕
 app.paint() #將pgu容器的內(nèi)容畫出來
 pygame.display.update()

3.制作事件觸發(fā)彈窗

除了通過界面上的按鈕觸發(fā)事件,我們常常也需要采用發(fā)生了某個(gè)事件觸發(fā)彈窗,這里設(shè)置了變量a,每按動(dòng)一次按鈕a增加1,直到a大于5時(shí)觸發(fā)彈窗彈出事件
引入庫(kù)

a = 0

將a的自增函數(shù)綁定到按鈕上

def add(self):
 global a
 a = a + 1
btn.connect(gui.CLICK, add, None)#將按鈕與彈窗的彈出綁定

加入判斷狀態(tài)函數(shù),判斷為True時(shí)彈出彈窗即可

 if a > 5:
  abc.open()
  a = 0

4.兩種模式完整代碼

按鈕觸發(fā)彈窗

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)) #生成一個(gè)屏幕
 app.paint() #將pgu容器的內(nèi)容畫出
 pygame.display.update()

事件觸發(fā)彈窗代碼

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)) #生成一個(gè)屏幕
 app.paint() #將pgu容器的內(nèi)容畫出
 if a > 5:
  abc.open()
  a = 0
 pygame.display.update()

本人在嘗試時(shí)發(fā)現(xiàn):
1、如果沒有screen.fill的函數(shù)彈出的窗口會(huì)無法關(guān)閉
2、如果沒有app.event則pgu事件無法執(zhí)行

總結(jié)

pgu的庫(kù)網(wǎng)上內(nèi)容較少,且較之tkinter運(yùn)用在pygame上會(huì)更加穩(wěn)定,但是pgu的硬傷是無法顯示中文的界面,因此有這方面的需求最好還是使用tkinter

到此這篇關(guān)于pygame庫(kù)pgu使用介紹的文章就介紹到這了,更多相關(guān)pygame庫(kù)pgu使用內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!

香港服務(wù)器租用

版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實(shí)時(shí)開通

自選配置、實(shí)時(shí)開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對(duì)1客戶咨詢顧問

在線
客服

在線客服:7*24小時(shí)在線

客服
熱線

400-630-3752
7*24小時(shí)客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部