以前在我的射弹模块中,我有一个单独处理每个射击方向的类(一个用于向上、向下、向左和向右射击的类),这就完成了它的工作。然而,现在我正在尝试将射击速度和其他东西纳入课程中,有 4 个单独的课程太混乱了,所以我试图减少它,以便我只有一个课程来发射所有射弹。
但是,既然我已经这样做了,当我发射弹丸时,它只会在我按住开火按钮(如果向左开火,则为“a”键)的情况下移动。此外,如果我向左开火,然后向右开火,之前向左移动的弹丸将开始向右移动。
我的问题是;我该如何处理弹丸,以便当我发射弹丸时,它不再接受更新并且不能直线移动?
这是我的工作代码;
游戏主模块
import pygame
from constants import *
from player import Player
from Projectile import Projectiles
pygame.init()
screen = pygame.display.set_mode([500, 500])
pygame.display.set_caption('Labyrinth')
# Spawn player
player = Player(50, 50)
all_sprites_list = pygame.sprite.Group()
all_sprites_list.add(player)
projectile_list = pygame.sprite.Group()
clock = pygame.time.Clock()
done = False
# ----- Event Loop
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == ord('a'):
player.changespeed(-3, 0)
elif event.key == ord('d'):
player.changespeed(3, 0)
elif event.key == ord('w'):
player.changespeed(0, -3)
elif event.key == ord('s'):
player.changespeed(0, 3)
elif event.key == pygame.K_LEFT:
projectile = Projectiles(0, 0)
projectile.rect.x = player.rect.x
projectile.rect.y = player.rect.y
elif event.key == pygame.K_RIGHT:
projectile = Projectiles(0, 0)
projectile.rect.x = player.rect.x
projectile.rect.y = player.rect.y
elif event.key == pygame.K_UP:
projectile = Projectiles(0, 0)
projectile.rect.x = player.rect.x
projectile.rect.y = player.rect.y
elif event.key == pygame.K_DOWN:
projectile = Projectiles(0, 0)
projectile.rect.x = player.rect.x
projectile.rect.y = player.rect.y
try:
if projectile:
projectile_list.add(projectile)
慕婉清6462132
相关分类