0

Is it possible in pygame to have "for event in pygame.event.get():" multiple times in the same loop, or do you have to only use it once? I was learning pygame, and I decided to define 2 functions that both check for events, but when one was called, the other one didn't work. Please keep in mind that I am a noob. This obviously isn't the actual code, but it's the jist of what I'm doing.

import pygame

class someclass:
    def __init__(self):
        some declaration
    def somefunction(self):
        for event in pygame.event.get():
            if event.type == someevent:
                some code
    def mainloop(self):
        someinstance = somesubclass()
        while True:
            someinstance.somefunction2()
            self.somefunction()

class somesubclass:
    def __init__(self):
        some declaration
    def somefunction2(self):
        for event in pygame.event.get():
            if event.type == someevent
                somecode

maininstance = someclass()
maininstance.mainloop()

somefunction doesn't execute

4

2 回答 2

1

If you wish to check for event's in many parts of your application, although not advisable - since when the application gets bigger, you will not know what a certain button does.

This would be the way:

import pygame

class MainClass:
    def __init__(self):
        self.someinstance = SomeSubclass()
    def check_events(self):
        for event in pygame.event.get():
            self.someinstance.somefunction2(event)
            if event.type == someevent:
                some code
    def mainloop(self):
        someinstance = somesubclass()
        while True:
            self.somefunction()

class SomeSubclass:
    def __init__(self):
        some declaration
    def somefunction2(self,event):
        if event.type == someevent
            somecode

The goal is to propagate the event to every object, and it will be the object that will decide, what to do.

于 2013-11-03T18:25:01.700 回答
0

Your problem is in event system.

If you get event then event is destroy so you can't get it again - it is normal.

You get all events in somefunction2 and somefunction has nothing to do.

于 2013-11-03T18:21:47.440 回答