python - 如何在 Pygame 中的不同对象的事件中拥有多个计时器?

标签 python python-3.x class pygame

因此,我受东方启发,在 Pygame 中创建了一款子弹 hell 风格的游戏,其中玩家以不同的射速向多个不同的方向发射子弹。我设法借用了一些代码,使我能够实现子弹的快速射击,然后通过为分散射击创建多个不同的子弹对象来添加该代码。然而,虽然我希望能够调整每个单独的子弹对象流的重新加载速度,但老实说我对如何调整感到困惑。有人可以帮我吗?

以下是子弹按预期射击所涉及的代码,所有子弹都具有相同的装弹速度:

reloadSpeed = 50
shootingEvent = pygame.USEREVENT + 1
reloaded = True

(稍后在 while 循环中):

if key[pygame.K_z]:
        if reloaded:
            bulletSound.stop()
            player.shoot()
            bulletSound.play()
            reloaded = False
            pygame.time.set_timer(shootingEvent, reloadSpeed)

这是我的 Player 类中的射击函数,以及用于上下文的 Bullet 类:

def shoot(self):
        bullet1N = Bullet(self.rect.centerx, self.rect.top, -4, -10, lb)
        bulletCentreN = Bullet(self.rect.centerx, self.rect.top, 0, -10,db)
        bullet3N = Bullet(self.rect.centerx, self.rect.top, 4, -10, lb)
        bullet1N2 = Bullet(self.rect.centerx, self.rect.top, -2, -10, db)
        bullet2N2 = Bullet(self.rect.centerx, self.rect.top, -1, -10, db)
        bullet3N2 = Bullet(self.rect.centerx, self.rect.top, 1, -10, db)
        bullet4N2 = Bullet(self.rect.centerx, self.rect.top, 2, -10, db)

(等等,很多不同类型的子弹。第一个数字代表相应“功率”阈值中的数字流,因为我希望随着整个游戏中功率的增加而出现不同的子弹流;“N”或“S' 表示该流是用于正常开火期间还是按住Shift键时,第二个数字表示该流所使用的功率级别。)

        keystate = pygame.key.get_pressed()
        if power < 16:
            if keystate[pygame.K_LSHIFT]:
                Group(bulletCentreS)

            else:
                Group(bullet1N)
                Group(bulletCentreN)
                Group(bullet3N)

        if power >= 16 and power < 48:
            if keystate[pygame.K_LSHIFT]:
                Group(bullet1S2)
                Group(bullet2S2)


            else:
                Group(bullet1N2)
                Group(bullet2N2)
                Group(bullet3N2)
                Group(bullet4N2)

(组只是一个功能,可以更有效地将子弹添加到几个 Sprite 组中)

class Bullet(pygame.sprite.Sprite):
    def __init__(self, x, y, speedx, speedy, col):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((6, 6))
        self.image.set_alpha(100)
        self.image.fill(col)
        self.rect = self.image.get_rect()
        self.rect.bottom = y
        self.rect.centerx = x
        self.speedy = speedy
        self.speedx = speedx

    def update(self):
        self.rect.y += self.speedy
        self.rect.x += self.speedx
        # kill if it moves off the top of the screen
        if self.rect.bottom < 25:
            self.kill()
        if self.rect.x > WIDTH/2:
            self.kill()

如图所示,这会导致任何模式的每颗子弹都以相同的速率发射,尽管在某些情况下移动速度不同。

编辑:感谢 Kingsley,我开始意识到我需要以某种方式将射击功能实现到我的 Bullet 类中,以便可以通过每个具有射速属性的 Bullet 对象来调整不同流的射速......但我该如何实现呢?

最佳答案

一种简单的技术是记录“触发”事件的实时时间,并且在时间延迟到期之前不允许该类型的进一步操作。

获取可用时间戳的简单方法是 PyGame function pygame.time.getticks(),它返回自启动以来不断增加的毫秒数。

每当用户启动触发机制时,存储时间。

fire_time_type1 = pygame.time.getticks()  # time of last shot

还要决定发射率延迟应该是多少,例如:

RELOAD_DELAY_TYPE1 = 300   # milliseconds

修改用户界面,使触发过程仅在延迟结束后完成:

now = pygame.time.getticks()
if ( now > fire_time_type1 + RELOAD_DELAY_TYPE1 ):
    fire_weapon_type1()
    fire_time_type1 = now
else:
    # still waiting for re-load time delay
    # play weapon-too-hot sound, etc.
    pass

显然,这种机制可以应用于类中,因此延迟发生在对象内部,而不是像本示例那样发生在外部。

通过简单地存储更多的使用时间记录,可以针对不同的延迟轻松实现此方法。

编辑:

也许触发类可以维护不同类型的状态 子弹。

### Gun object encapsulates enough detail to track the number of shots
### from a given magazine size, enforcing a reload-delay once the
### magazine is empty
class Gun:
    def __init__( self, name, magazine_size, reload_delay=300 ):
        self.name     = name
        self.shots    = 0
        self.mag_size = magazine_size
        self.reload   = reload_delay    # milliseconds
        self.use_time = 0

    def hasBullet( self ):
       """ Check that either a bullet is available, or that the
           reload delay has expired """
       now = pygame.time.getticks()
       if ( self.shots >= self.mag_size and now > self.use_time + self.reload ):
           self.shots = 0    # reload
           print( "Debug: [%s] reloaded after %d millisecs" % (self.name, self.reload ) )
       return ( self.shots < self.mag_size )

    def trackFiring( self ):
        """ keep track of the magazine limit, and when the shot was fired """
        self.shots    += 1
        self.use_time  = pygame.time.getticks()  # time of last shot             
        print( "Debug: [%s] fired, %d rounds left" % (self.name, self.mag_size - self.shots ) )



### ...

nail_gun = Gun( 'nail', 200, 500 )  
rockets  = Gun( 'rocket', 3, 1000 )
bfg      = Gun( 'BFG (oh yes!)', 1, 5000 )

### ...

# Player activates rocket launcher~
if ( rockets.hasBullet() ):
    rockets.trackFiring()
    shoot( ... )

显然,子弹射击可以放入 Gun 类中,但这留给读者作为练习。

注意:我尚未测试编译此代码,预计会有小错误和遗漏。

关于python - 如何在 Pygame 中的不同对象的事件中拥有多个计时器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57867246/

相关文章:

python - 如何从 Google 代码进行 pip 安装?

python-3.x - 返回多维数组中N个最大值的索引(可以求一维的解,但不能求多维的)

python - Python 中的 Azure 持久函数 : why is function 2 not triggered?

javascript - React 如何以 `this` 不引用类本身的方式调用 ES6 类的渲染函数?

PHP对象类变量

python - 使用 pandas 或 numpy 建立时间序列数据索引

python - 使用 pyglet 和 pymunk 使 Sprite 角色自然跳跃

python - django-rest-framework + django-多态模型序列化

python - 运行时错误 : main thread is not in main loop; when I click the Exit button

python - 多处理:将类实例传递给 pool.map