所以我在 Pygame 中创建了一个受东方启发的子弹地狱风格的游戏,让玩家以不同的射速和不同的方向发射子弹。我设法借用了一些代码,这些代码使我能够实现子弹的快速射击,然后通过创建多个不同的子弹对象来进行散弹射击。然而,虽然我希望能够为每个单独的子弹对象流调整重新加载速度,但我真的很难过。有谁可以帮我离开这里吗?
这是子弹按预期射击所涉及的代码,所有这些都具有相同的重新加载速度:
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”代表流是在正常开火期间还是在保持换档时使用,第二个数字表示该流用于的功率级别。)
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)
(组只是将子弹添加到几个精灵组的功能稍微更有效)
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()
如图所示,这导致任何模式的每颗子弹都以相同的速度发射,尽管在某些情况下以不同的移动速度。
编辑:所以感谢金斯利,我开始意识到我需要以某种方式在我的 Bullet 类中实现射击功能,以便可以通过每个具有 firerate 属性的 Bullet 对象来调整不同流的射速......但是如何我实现这个?