'Two "For event in pygame.event.get()"
I have done def for to make two functions. I do not want to join these because I want to be able to make something where I can do one without doing the other. The problem is I have two for event in pygame.event.get()s and whenever I try to use one after the other as the first pygame.event.get() gets rid of all events after it is called, so I can't use other events in the second function. For example, in
def Func1():
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
#Do something
def Func2():
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
#Do something
If I do any of these, the second wont work:
- do Func1, and the next line do Func2 (Func2 won't work)
- do Func2, and the next line do Func1 (Func1 won't work)
- do Func1 alone (Will work)
- do Func2 alone (Will work)
I did some experimenting to find out that the for event in pygame.event.get(): gets rid of all avents after it is called, which is why I am having a problem. Anyone have any ideas how to bypass this? Thanks and tell me if you need more information.
Solution 1:[1]
I just encounter this issue recently. I might don't know which way is the best way to resolve this issue with refactoring the code, but there is a simple way to get the same effect by using pygame.key.get_pressed() to handler the keyboard event while a key is pressed. So that you don't worry about calling the event.get() twice in one loop.
def function():
for event in pygame.event.get():
if event.type==pygame.KEYDOWN and event.key == pygame.K_UP:
#do something
run = True
while run:
key_pressed = pygame.key.get_pressed()
if key_pressed[K_ESCAPE]:
run=False
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
