生成对象问题 [英] Spawning Object issue

查看:57
本文介绍了生成对象问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在屏幕上生成对象.但他们似乎没有出现.

I am trying to get objects spawn on screen. But they don't seem to come up.

退出游戏时,精灵出现.只是在游戏过程中你看不到任何物体.

When exiting the game the sprites come up. It's only during the game you can't see any objects.

Exit = False 


while not Exit:
    clock.tick(60)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

            if event.type == QUIT:
            Exit = True

        elif event.type == USEREVENT +1:
            fireball.add(fire(screen, random.randint(50,1000), random.randint(50,800)))

    fireball.update()
    fireball.draw(screen)


    pygame.display.flip()

推荐答案

既然你没有告诉我们,我就假设 fireball 是一个 sprite.Group> 和 fire 一个 sprite.Sprite 的子类.从我所见,似乎是正确的猜测.
所以你想要的是在游戏过程中创建和添加 fire 实例到 fireball 组.

Since you do not tell us, I'm going to assume that fireball is a sprite.Group and fire a child class of sprite.Sprite. From the little I can see, seems to be the correct guess.
So what you want is to create and add fire instances to the fireball group during the game.

您可以通过在firefall.update()之前在主循环中添加以下几行来实现:

You can achieve it by adding the following lines in the main loop before firefall.update():

newev = pygame.event.Event(USEREVENT+1)
pygame.event.post(newev)

这将创建一个 USEREVENT+1 类型的自定义事件,该事件将被下一次迭代的事件检查循环捕获,因此执行以下行:fireball.add(fire(screen, random.randint(50,1000), random.randint(50,800)))

This will create a custom event of type USEREVENT+1 which will be catch by the event checking loop next iteration, executing hence the line: fireball.add(fire(screen, random.randint(50,1000), random.randint(50,800)))

也许你不想每次迭代都创建一个新的 fire 精灵.在这种情况下,您应该添加一些流量控制以在某些情况下跳过这些行.

Maybe you do not want to create a new fire sprite each iteration. In that case you should add some flow control to skip those lines under some conditions.

例如,如果你想要一个随机的方法,你可以这样做:

For example, if you want a random approach, you can do:

if random.random() < 0.1:
    newev = pygame.event.Event(USEREVENT+1)
    pygame.event.post(newev)

在这种情况下,主循环的每次迭代都有 10% 的概率提交自定义事件.调整适合您西装的概率.

In this case, each iteration of the main loop you have a 10% of probability to submit the custom event. Adjust the probability to your suits.

如果你想在给定的时间后添加一个新的 fire 精灵,你需要使用 pygame.time.get_ticks() 来测量时间.如果经过给定的时间,则提交事件.

If instead you want to add a new fire sprite after a given amount of time, you need to measure the time with pygame.time.get_ticks(). If a given amount of time is passed, the event is submitted.

checktime = pygame.time.get_ticks() - reftime
if checktime > 5000: #5 seconds, the time is in milliseconds
    reftime = pygame.time.get_ticks() #reset the reference time
    newev = pygame.event.Event(USEREVENT+1)
    pygame.event.post(newev)

当然记得在主循环之前第一次定义 reftime = pygame.time.get_ticks().您可以参考此答案,了解如何使用 pygame 测量时间的另一个示例.

And of course remember to define reftime = pygame.time.get_ticks() the first time before the main loop. You can refer to this answer for another example on how to measure time with pygame.

这篇关于生成对象问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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