如何在 while 循环中延迟特定事件? [英] How do you delay specific events in a while loop?

查看:77
本文介绍了如何在 while 循环中延迟特定事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我一直在使用 pygame 在 python 中开发一个简单而直接的 RPG,但是我在延迟特定事件时遇到了一些问题.运行下面的代码,一切都会立即发生.

Recently I've been working with a simple and straightforward RPG in python with pygame, but I'm having some problems delaying specific events. Running the code below, everything happens at once.

if event.key == pygame.K_SPACE and buttonHighlight == 0:

    FireAnimation() #displays a fire image

    #DELAY HERE

    if player[6] == 'Magic': #you deal damage to the enemy
        enemy[0] = enemy[0]-(((player[1])+((player[1])*1)-enemy[4]))
    else:
        enemy[0] = enemy[0]-(((player[1])+((player[1])*1)-enemy[3]))

    #DELAY HERE

    StarAnimation() #displays a star image

    #DELAY HERE

    if enemy[6] == 'Magic': #enemy deals damage to you
        player[0] = player[0]-(((enemy[1])+((enemy[1])*1)-player[4]))
    else:
        player[0] = player[0]-(((enemy[1])+((enemy[1])*1)-player[3]))

其余的代码并不真正相关,我只是想指出我想延迟的地方.运行这个,两个图像显示,玩家和敌人同时受到伤害.谢谢!

The rest of the code isn't really relevant, I just wanted to point out where I want to delay. Running this, both images displays, the player and the enemy takes damage at the same time. Thanks!

我忘了提到我已经尝试过 pygame.time.delay/wait 和 time.sleep,但所有这些都延迟了整个操作!它只是在我使用它时将所有内容向前推进,因此所有内容都在几秒钟后同时发生

I forgot to mention that I already have tried pygame.time.delay/wait and time.sleep, but all those delays the whole operation! It simply pushes everything forward when I use it, so everything happens at the same time several seconds later

推荐答案

您可以创建两个新事件(FIRE_ANIMATION_STARTSTAR_ANIMATION_START),然后将它们发布到事件队列中延迟(使用 pygame.time.set_timer(eventid, milliseconds)).然后在您的事件循环中,您只需检查它.

You can create two new events (FIRE_ANIMATION_START, STAR_ANIMATION_START) which you post to the event queue with a delay (with pygame.time.set_timer(eventid, milliseconds)). Then in your event loop you just check for it.

FIRE_ANIMATION_START = pygame.USEREVENT + 1
STAR_ANIMATION_START = pygame.USEREVENT + 2

# ... Your code ...

for event in pygame.event.get():

    if event.key == pygame.K_SPACE and buttonHighlight == 0:
        pygame.time.set_timer(FIRE_ANIMATION_START, 10)    # Post the event every 10 ms.
        pygame.time.set_timer(STAR_ANIMATION_START, 1000)  # Post the event every 1000 ms.

    elif event.code == FIRE_ANIMATION_START:
        pygame.time.set_timer(FIRE_ANIMATION_START, 0)     # Don't post the event anymore.
        FireAnimation() #displays a fire image
        if player[6] == 'Magic': #you deal damage to the enemy
            enemy[0] = enemy[0]-(((player[1])+((player[1])*1)-enemy[4]))
        else:
            enemy[0] = enemy[0]-(((player[1])+((player[1])*1)-enemy[3]))

    elif event.code == STAR_ANIMATION_START:
        pygame.time.set_timer(STAR_ANIMATION_START, 0)     # Don't post the event anymore.
        StarAnimation() #displays a star image
        if enemy[6] == 'Magic': #enemy deals damage to you
             player[0] = player[0]-(((enemy[1])+((enemy[1])*1)-player[4]))
        else:
             player[0] = player[0]-(((enemy[1])+((enemy[1])*1)-player[3]))

文档 pygame.time.set_timer(eventid, 毫秒).此外,由于代码现在存在,因此其中存在错误.事件的属性因事件类型而异,因此在访问属性 event.keyKEYDOWN 还是 USEREVENTcode> 或 event.code.可以在此处找到不同的类型和属性.

Documentation for pygame.time.set_timer(eventid, milliseconds). Also, as the code is right now it has bugs in it. The attributes for the events differs between different event types, so always make sure to check whether an event is KEYDOWN or USEREVENT before accessing the attributes event.key or event.code. The different types and attributes can be found here.

这篇关于如何在 while 循环中延迟特定事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆