Python + Pygame + PygButton检测 [英] Python + Pygame + PygButton detecting
问题描述
我使用Python 3.3与Pygame和PygButton(低代表不允许我超过两个链接)
我现在的文件是[ int.py http://pastebin.com/sDRhieCG ]和[ scene.py http://pastebin.com/Y2Mgsgmr ]。
I'm using Python 3.3 with Pygame and PygButton (low rep don't allow me more than two links) My files at the moment are [int.py http://pastebin.com/sDRhieCG ] and [scene.py http://pastebin.com/Y2Mgsgmr ].
使int.py中的mainloop尽可能小。代码在mainloop上有一个注释掉的start_screen按钮的例子。它工作,但是每一个新的屏幕mainloop会膨胀。
The idea is making a mainloop in int.py the smaller as possible. The code has a commented-out example of the start_screen buttons at the mainloop. It works, but with every new screen the mainloop would be bloated.
所以我创建了一个Scene类应用背景,文本和按钮。它工作,但我可以使按钮工作。例如。
So I created a Scene class to apply background, texts and buttons. It works, but I can make the buttons work. E.g. the bquit button doesn't quit the screen (as it did previously when inserted into the mainloop).
我试图在场景中创建一个scene_loop() )来运行特定场景提供的一切。
I'm trying to create a scene_loop() inside the Scene() to run everything the specific scene has to offer. With a button click it would change scene and such, start a new scene_loop.
在Scene类被实例化之后,我似乎无法添加特定的方法,所以我创建了一个新的scene_loop。一个Scene_Start类来处理特定的方法,如scene_loop及其按钮(因为背景很容易通过Scene类放置)。
I can't seem to add specific methods after the Scene class is instanced, so I created a Scene_Start class to deal with specific methods like the scene_loop and its buttons (since the background is easily placed through the Scene class).
帮助?
tl ; dr:
1. PygButton不在mainloop外部工作
2.如何创建一个scene_loop来代替该场景的mainloop,unbloatingmainloop启动应用程序和更改场景)。
tl;dr: 1. PygButton isn't working outside mainloop 2. How can I create a scene_loop that replaces the mainloop for that scene, "unbloating" mainloop (it would only take care of starting the app and changing scenes).
谢谢。
推荐答案
p>问题解决。回答我自己的问题,所以它可以有助于他人。
每个类都有一个sceneloop方法来运行和替换mainloop。这可以解决问题并将任何问题隔离到场景中,而不影响其他组件。
Problem solved. Answering my own question so it can be helpful to others. Each class have a sceneloop method that runs and "replace" the mainloop. This allows for unbloating and isolating any problem into the scene without affecting the other components.
mainloop:
while True:
for event in pg.event.get():
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
pg.quit()
sys.exit()
scene.startscreen.draw()
scene.startscreen.sceneloop()
pg.display.update()
一个sceneloop示例。这是Scene_Start(Scene)类中的一个方法,实例化为如上面在主循环中所示的开始屏幕。
A sceneloop example. This is a method inside the Scene_Start(Scene) class, instanced as startscreen as show above in the main loop.
def sceneloop(self):
while self.scene_loop == True:
for event in pg.event.get():
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
pg.quit()
sys.exit()
if 'click' in self.bcreate.handleEvent(event): #CREATE
startcreate.draw()
startcreate.sceneloop()
if 'click' in self.bstart.handleEvent(event): #START
pg.quit()
sys.exit()
if 'click' in self.bload.handleEvent(event): #LOAD
startload.draw()
startload.sceneloop()
if 'click' in self.boptions.handleEvent(event): #OPTIONS
startoptions.draw()
startoptions.sceneloop()
if 'click' in self.bquit.handleEvent(event): #QUIT
pg.quit()
sys.exit()
self.update()
这篇关于Python + Pygame + PygButton检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!