'Setting var1 = Pygame.event.get() and var2 = Pygame.event.get() later in the code seems to lead to wonky results. Why? [duplicate]
In my code I was messing around and created code that looked something like this
def checkformouseclick(): eventlist = pygame.event.get() for i in eventlist: if i.type == pygame.MOUSEBUTTONDOWN: print("Mousebuttondown") else: print("Mousebutton not down")
Main Loop: checkformouseclick() second_event_list = pygame.event.get(): for j in second_event_list: if j.type == pygame.QUIT: break mainloop
I then decided to print out each "index" of each individual list(i.e. print(i.type), print(j.type)) and found that unexpected things were happening. For example, I would create events by clicking on the screen and smashing my keyboard but these events would show up in one of the event lists but not the other. Why is that the case?
Thanks for any answers, sorry if I'm being an idiot.
Solution 1:[1]
Alright guys, after running a few experimental lines of code and reading up on documentation, here's the answer for future beginners like me.Disclaimer: I'm a business major not a computer scientist.
Assume you create a variable eventlist and set it equal to pygame.event.get(). To dumb down whats happening, pygame is going to take a look at the event queue and check to see if there are any events within this queue. It is then going to take any events from the queue, put them into a list, and now the variable is referencing that list. However, the queue is emptied once this process is complete.
Thus, if in this same loop lower down in the program I say neweventlist = pygame.event.get(), pygame is going to look through the queue and only any events that have occured since I last read through the queue are going to be added to the list.
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 |