好的,所以我有一个用pygame制作的游戏。游戏的目的是在屏幕底部有一个人向上射击,向上击中敌人。这个人可以左右移动。
要拍摄,请按空格键。
射击时,我希望在枪管的末端出现一个小的精灵,这样看起来好像火焰像真正的枪击一样散发出来。
在按空格键时,我设法使火焰精灵出现,但我也希望精灵在0.3秒后消失(一旦消失,我会调整时间)。
我实际上该如何做?我在这里感到迷茫
我尝试过Google搜索,并在这里查看stackoverflow,但找不到所需的内容。
game.py
# -- SNIP --
def run_game():
# -- SNIP ---
# Start the main loop for the game.
while True:
# Watch for keyboard and mouse events.
gf.check_events(ai_settings, screen, stats, sb, play_button, man,
enemies, bullets)
if stats.game_active:
man.update()
gf.update_bullets(ai_settings, screen, stats, sb, man, enemies,
bullets)
gf.update_enemies(ai_settings, stats, screen, sb, man, enemies,
bullets)
gf.update_screen(ai_settings, screen, stats, sb, man, enemies,
bullets, play_button)
run_game()
game_functions.py
# -- SNIP --
def check_events(ai_settings, screen, stats, sb, play_button, man, enemies,
bullets):
"""Respond to keypresses and mouse events."""
# -- SNIP --
elif event.type == pygame.KEYDOWN:
check_keydown_events(event, ai_settings, screen, stats, sb, man,
enemies, bullets)
# -- SNIP --
def check_keydown_events(event, ai_settings, screen, stats, sb, man, enemies, bullets):
"""Respond to key presses."""
if event.key == pygame.K_RIGHT:
man.moving_right = True
man.orientation = "Right"
elif event.key == pygame.K_LEFT:
man.moving_left = True
man.orientation = "Left"
# Draw sprite if space is pressed
elif event.key == pygame.K_SPACE:
fire_bullet(ai_settings, bullets, screen, man)
man.fire_screen.blit(man.fire, man.fire_rect)
# --- SNIP ---
# -- SNIP --
man.py
class Man(Sprite):
# -- SNIP --
# Load the man image and get its rect.
self.image = pygame.image.load('images/man_gun_large.bmp')
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
# Gun fire sprite
self.fire = pygame.image.load('images/fire.bmp')
self.fire_rect = self.fire.get_rect()
self.screen_fire_rect = self.image.get_rect()
self.fire_screen = self.image
self.ai_settings = ai_settings
# Start each new man at the bottom center of the screen.
self.rect.centerx = self.screen_rect.centerx
self.rect.bottom = self.screen_rect.bottom
# Load the fire sprite at the end of the mans gunbarrel
self.fire_rect.centerx = self.screen_fire_rect.left + 20
self.fire_rect.top = self.screen_fire_rect.top
# ---SNIP---
# Movement flags
self.moving_right = False
self.moving_left = False
self.orientation = False
def blitme(self):
"""Draw the man at its current location."""
self.screen.blit(self.image, self.rect)
if self.orientation == "Right":
self.screen.blit(self.image, self.rect)
elif self.orientation == "Left":
self.screen.blit(pygame.transform.flip(self.image, True, False), self.rect)
答案 0 :(得分:1)
使用pygame.time.get_ticks()
管理项目符号的生命周期。
添加一个列表,其中包含子弹寿命终止的元组和子弹本身。
bullets = []
当子弹产生时,然后计算结束时间并将信息附加到列表中:
def check_keydown_events(event, ai_settings, screen, stats, sb, man, enemies, bullets):
# [...]
# Draw sprite if space is pressed
elif event.key == pygame.K_SPACE:
bullet = # [...]
end_time = pygame.time.get_ticks() + 300 # 300 millisconds = 0.3 seconds
bullets.append( (end_time, bullet) )
检查子弹是否已达到使用寿命,并决定保留子弹或.kill
:
current_time = pygame.time.get_ticks()
current_bullets = bullets
bullets = []
for end_time, bullet in current_bullets:
if current_time > end_time:
bullet.kill()
else:
bullets.append( (end_time, bullet) )