gpt4 book ai didi

Faster version of 'pygame.event.get()'. Why are events being missed and why are the events delayed?(更快版本的‘pygame.vent.get()’。为什么错过了比赛,为什么比赛被推迟了?)

转载 作者:bug小助手 更新时间:2023-10-28 13:55:12 25 4
gpt4 key购买 nike



I'm making an Asteroidz clone in pygame and have two for event in pygame.event.get() loops, one for checking an exit request and wether the game should have started by pressing spacebar, then further in the game as to try and limit the player from holding spacebar down and continuously shooting. The relevent code for my check_input function, which is run once every loop, is below;

我在pyGame中创建了一个Asteroidz克隆,并在pygame.vent.get()循环中有两个for Event,一个用于检查退出请求,以及游戏是否应该通过按空格键开始,然后在游戏中进一步尝试并限制玩家按住空格键并连续射击。我的check_input函数的相关代码如下所示,该函数在每个循环中运行一次;



def check_input(self):
for event in pygame.event.get(): #NOT CHECKING THIS FAST ENOUGH, WHOLE PROCESS IS TOO SLOW
if (event.type == pygame.KEYUP) and (event.key == pygame.K_SPACE):
print ('boop')
self.shootThrottle = 0

if self.shootThrottle == 0:
self.shootThrottle += 1
bullets.add(Bullet(self.shape[0][0],self.shape[0][1], self.angle))

key = pygame.key.get_pressed()

if key[pygame.K_LEFT]:
self.angle -= 7
self.rotate(-7)
elif key[pygame.K_RIGHT]:
self.angle += 7
self.rotate(7)

if self.angle > 360:
self.angle -= 360
elif self.angle < 0:
self.angle += 360

if key[pygame.K_UP]:
self.accelerate()
elif key[pygame.K_DOWN]:
self.decelerate()


I am using shootThrottle as a means to try stop bullets from being shot until spacebar has been let go. This system works, but due to the for event in pygame.event.get() being too slow, it doesn't function properly.

我正在使用ShootThrottle作为一种方法,试图阻止子弹被射出,直到空格键被释放。这个系统可以工作,但由于pygame.vent.get()中的for事件太慢,它不能正常工作。



Any help is massively appreciated!

我们非常感谢您的任何帮助!


更多回答
优秀答案推荐


[...] and have two for event in pygame.event.get() loops [..]"



That's the issue. pygame.event.get() get all the messages and remove them from the queue. See the documentation:

这就是问题所在。Get()获取所有消息并从队列中删除它们。请参阅文档:



This will get all the messages and remove them from the queue. [...]



If pygame.event.get() is called in multiple event loops, only one loop receives the events, but never all loops receive all events. As a result, some events appear to be missed.

如果在多个事件循环中调用pygame.vent.get(),则只有一个循环接收事件,但不是所有循环都接收所有事件。因此,一些活动似乎被错过了。


Get the events once per frame and use them in multiple loops or pass the list or events to functions and methods where they are handled:

每帧获取一次事件,并在多个循环中使用它们,或将列表或事件传递给函数和方法,在这些函数和方法中处理它们:


def handle_events(events):
for event in events:
# [...]

while run:

event_list = pygame.event.get()

# [...]

# 1st event loop
for event in event_list:
# [...]

# [...]

# 2nd event loop
for event in event_list:
# [...]

# [...]

# function which handles events
handle_events(event_list)

更多回答

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com