Python + Pygame + PygButton detecting

感情迁移 提交于 2019-12-25 04:25:15

问题


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 ].

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.

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).

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.

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).

I'm just stuck and can't see a way to resolve this without scrapping everything and starting again.

Help?

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).

Thank You.


回答1:


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.

The 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()

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()


来源:https://stackoverflow.com/questions/22284615/python-pygame-pygbutton-detecting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!