在 pygame 中发射多枚射弹
Shooting multiple projectiles in pygame
我有一个 Missile
class 在按下空格键时获取 Player
的位置。 .fire()
利用这个位置发射导弹。一次只能拍一张,怎么拍多张?我听说使用列表会有帮助。
class Missile(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
# Get image of the whole spritesheet
self.imgMaster = pygame.image.load("Royalty-Free-Game-art-Spaceships-from-Unlucky-Studio.png")
self.imgMaster.convert()
misImgSize = (45, 77)
# Create a surface to draw a section of the spritesheet
self.image = pygame.Surface(misImgSize)
self.image.blit(self.imgMaster, (0,0), ( (71, 1956) ,(misImgSize)) )
self.image.set_colorkey( self.image.get_at((1,1)))
self.image = pygame.transform.scale( self.image, (15, 35) )
# Get rect of sprite
self.rect = self.image.get_rect()
# Place missile off-screen at first
self.rect.center = (-100, -100)
self.dy = 0
def fire(self, player_pos):
self.rect.center = player_pos # Move Bomb to cannon.
self.dy = BOMB_SpeedY # Set its velocity.
def update(self):
self.rect.centery -= self.dy
def reset(self):
self.rect.center = (-100, -100) # This location is off-screen!
self.dx = 0
事件处理程序中的代码:
clock = pygame.time.Clock()
# - Set a loop that keeps running until user quits or proceeds
keepGoing = True
while keepGoing:
# Set FPS of the game - 30 frames per second/tick
clock.tick(CLOCK_TICK)
# Every 30 ticks is a second
tickCount += 1
# Handle any events
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
print("hi!")
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
missile.fire(player.get_pos())
您必须为导弹创建一个组。当按下 SPACE 时,创建一个新的 Missile
实例并将其添加到列表中:
missilesGroup = pygame.sprite.Group()
keepGoing = True
while keepGoing:
# [...]
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
print("hi!")
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
missile = Missile()
missile.fire(player.get_pos())
missilesGroup.add(missile)
我建议也将新导弹添加到所有 Sprites 的 Group 中。
不要“重置”导弹,而是使用 kill
将其移除:
class Missile(pygame.sprite.Sprite):
# [...]
def reset(self):
self.kill()
见kill
remove the Sprite from all Groups
我有一个 Missile
class 在按下空格键时获取 Player
的位置。 .fire()
利用这个位置发射导弹。一次只能拍一张,怎么拍多张?我听说使用列表会有帮助。
class Missile(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
# Get image of the whole spritesheet
self.imgMaster = pygame.image.load("Royalty-Free-Game-art-Spaceships-from-Unlucky-Studio.png")
self.imgMaster.convert()
misImgSize = (45, 77)
# Create a surface to draw a section of the spritesheet
self.image = pygame.Surface(misImgSize)
self.image.blit(self.imgMaster, (0,0), ( (71, 1956) ,(misImgSize)) )
self.image.set_colorkey( self.image.get_at((1,1)))
self.image = pygame.transform.scale( self.image, (15, 35) )
# Get rect of sprite
self.rect = self.image.get_rect()
# Place missile off-screen at first
self.rect.center = (-100, -100)
self.dy = 0
def fire(self, player_pos):
self.rect.center = player_pos # Move Bomb to cannon.
self.dy = BOMB_SpeedY # Set its velocity.
def update(self):
self.rect.centery -= self.dy
def reset(self):
self.rect.center = (-100, -100) # This location is off-screen!
self.dx = 0
事件处理程序中的代码:
clock = pygame.time.Clock()
# - Set a loop that keeps running until user quits or proceeds
keepGoing = True
while keepGoing:
# Set FPS of the game - 30 frames per second/tick
clock.tick(CLOCK_TICK)
# Every 30 ticks is a second
tickCount += 1
# Handle any events
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
print("hi!")
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
missile.fire(player.get_pos())
您必须为导弹创建一个组。当按下 SPACE 时,创建一个新的 Missile
实例并将其添加到列表中:
missilesGroup = pygame.sprite.Group()
keepGoing = True
while keepGoing:
# [...]
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
print("hi!")
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
missile = Missile()
missile.fire(player.get_pos())
missilesGroup.add(missile)
我建议也将新导弹添加到所有 Sprites 的 Group 中。
不要“重置”导弹,而是使用 kill
将其移除:
class Missile(pygame.sprite.Sprite):
# [...]
def reset(self):
self.kill()
见kill
remove the Sprite from all Groups