0

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.

  • Once you've read an event from the queue, it's no longer in the queue! You really should have only one place in your program handling events. – jasonharper May 14 '22 at 02:22
  • Hello Jason Harper! What do you mean by "reading an event from the queue"? Do you mean if I iterate through the events in the queue, they automatically clear? – Julian Biju May 14 '22 at 02:46
  • If they DIDN'T automatically clear, you would receive the same events over and over, forever. – jasonharper May 14 '22 at 02:58

1 Answers1

0

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.