设置var1 = pygame.event.get()和var2 = pygame.event.get()代码稍后似乎导致了奇怪的结果。为什么?
在我的代码中,我四处乱逛,并创建了看起来像此
def checkformouseclick()的代码: eventlist = pygame.event.get() 因为我在eventlist中: 如果i.type == pygame.mousebuttondown: 打印(“ MouseButtondown”) 别的: 打印(“ Mousebutton not Down”)
主循环: CheckFormouseClick() second_event_list = pygame.event.get(): 对于second_event_list中的j: 如果j.type == pygame.quit: 然后,
我决定打印每个单独列表(即print(i.type),print(j.type))的每个“索引”,并发现出乎意料的事情正在发生。例如,我将通过单击屏幕并粉碎键盘来创建事件,但这些事件会显示在事件列表之一,而不是另一个事件列表中。为什么这样?
感谢您的任何答案,对不起,如果我是个白痴。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,在运行了一些实验性的代码线并阅读文档后,这是像我这样的未来初学者的答案。
假设您创建一个可变eventlist并将其设置为pygame.event.get()。为了愚弄发生的事情,Pygame将要查看事件队列并检查该队列中是否有任何事件。然后,它将从队列中获取任何事件,将它们放入列表中,现在变量正在引用该列表。但是,一旦完成此过程,队列就会清空。
因此,如果在程序中的同一循环中,我说neweventlist = pygame.event.get(),pygame将浏览队列,并且自从我上次通过排队以来的任何事件都会发生添加到列表中。
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.