[英]Checking for specific event in pygame event queue
我希望能够在 pygame pygame.USERVENT+1
中检查用户定义的事件。 我有我的主循环,其中填充了 window,并将事物绘制到 window。 我还有一个 for 循环来检查诸如退出或调整 window 等事件。 然后我有一个称为draw_cursor()
的 function 。 I am trying to check for the specific event of pygame.USEREVENT+1
in this function so I could use it to draw an object however I cannot seem to get this event without using the event queue in the main function to check if pygame.USERVENT+1
在事件队列中。
import pygame
pygame.init()
width, height = 1024, 648
window = pygame.display.set_mode((width, height), pygame.RESIZABLE)
pygame.time.set_timer(pygame.USEREVENT, 1000)
def display_cursor():
if pygame.event.poll().type == pygame.USEREVENT+1:
print("This should print when USERVENT is in the event queue")
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.VIDEORESIZE:
width, height = event.w, event.h
window = pygame.display.set_mode((event.w, event.h), pygame.VIDEORESIZE)
"""if event.type == pygame.USEREVENT+1:
print("This will print when USEREVENT in the event queue")"""
display_cursor()
window.fill((0,0,255))
pygame.display.flip()
注释掉的部分是当pygame.USERVENT+1
在事件队列中时实际打印的部分。 我使用pygame.event.poll().type
因为这将事件作为数字返回,pygame.USERVENT+1 是数字 24。我认为这会起作用pyame.event.poll().type
应该返回数字 24当pygame.USEREVENT+1
在事件队列中时。 我还查看了: https://www.pygame.org/docs/ref/event.html#pygame.event.peek也以类似的方式检查事件是否在事件队列中。 我试过if pygame.event.peek(pygame.USEREVENT+1):
而不是if pygame.event.poll().type == pygame.USEREVENT+1:
打印出 IENT+142DA30A7Z 想要什么它仍然没有。
我希望能够仅在此display_cursor()
function 中检查特定事件pygame.USERVEENT+1
而我没有使用pygame.event.get()
中的一个事件,因为它找到了一个事件。将我的程序减慢到 window 几乎没有响应的程度。 有没有办法我只能在单独的 function 中检查用户定义的事件,而不必将事件 object 传递给这个 function 以阻止它减速? 是否还有我缺少的参数或类似的 function 完全符合我的想法pygame.event.poll()
或pygame.event.peek()
pygame.event.get()
获取所有消息并将它们从队列中删除。 调用pygame.event.get()
后队列中没有更多消息。 因此pygame.event.poll()
几乎总是在display_cursor
中返回pygame.NOEVENT
类型的事件 object。
您需要在事件循环中调用display_cursor
。 该事件必须是 function 的参数:
def display_cursor(event):
if event.type == pygame.USEREVENT+1:
print("This should print when USERVENT is in the event queue")
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.VIDEORESIZE:
width, height = event.w, event.h
window = pygame.display.set_mode((event.w, event.h), pygame.VIDEORESIZE)
"""if event.type == pygame.USEREVENT+1:
print("This will print when USEREVENT in the event queue")"""
display_cursor(event)
或者,您可以获取事件列表并将事件列表传递给 function:
def display_cursor(event_list ):
for event in event_list :
if event.type == pygame.USEREVENT+1:
print("This should print when USERVENT is in the event queue")
while True:
event_list = pygame.event.get()
for event in event_list:
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.VIDEORESIZE:
width, height = event.w, event.h
window = pygame.display.set_mode((event.w, event.h), pygame.VIDEORESIZE)
"""if event.type == pygame.USEREVENT+1:
print("This will print when USEREVENT in the event queue")"""
display_cursor(event_list)
如果您正在寻找更细微的方法,请参阅Pygame:如何以多态方式编写事件循环
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.