我有一个Missile
类,它在按下空格键时获取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
的新实例并将其添加到列表中:我建议将新导弹也添加到所有精灵的组
不要“重置”导弹,而是使用^{} 将其移除:
见^{}
相关问题 更多 >
编程相关推荐