我在 pygame 中遇到子弹运动问题

I have problem with bullet motion in pygame

当我射击时,我可以控制飞弹。我怎样才能避免它。我想移动子弹,但我无法控制它。也许我应该修复我的代码。但我无法解决它。例如,子弹向右飞,但是当我按下 K_LEFT 键时,子弹改变了路线。

import pygame
pygame.init()
#Frames
FPS = 60
#Colors
WHITE = (255, 255, 255)
GREY = (105, 105, 105)
BLACK = (0, 0, 0)
GREEN = (0, 225, 0)
#Screen size
W = 1280
H = 680
#Player_img
p_up = pygame.image.load('Assets/tanks/green/up1.png')
p_d = pygame.image.load('Assets/tanks/green/down1.png')
p_r = pygame.image.load('Assets/tanks/green/right1.png')
p_l = pygame.image.load('Assets/tanks/green/left1.png')
#Scale_img
player_up = pygame.transform.scale(p_up, (40, 40))
player_d = pygame.transform.scale(p_d, (40, 40)) 
player_r = pygame.transform.scale(p_r, (40, 40))
player_l = pygame.transform.scale(p_l, (40, 40))
#Screen
sf = pygame.display.set_mode((W, H))
pygame.display.set_caption('TanksX')
sf.fill(WHITE)
#Clock
clock = pygame.time.Clock()
#For Game
Run = True
last_move = 0
#Sprites init
class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = player_up
        self.rect = self.image.get_rect()
        self.rect.centerx = W // 2
        self.rect.y = H // 2
        self.speedy = 2
        self.speedx = 2
        self.shoot_delay = 1000
        self.last_shot = pygame.time.get_ticks()
    def update(self):
        global last_move
        if keys[pygame.K_UP]:
            self.image = pygame.transform.rotate(player_up, 360)
            self.rect.y -= self.speedy
            last_move = 1
        elif keys[pygame.K_DOWN]:
            self.image = pygame.transform.rotate(player_up, 180)
            self.rect.y += self.speedy
            last_move = 2
        elif keys[pygame.K_RIGHT]:
            self.image = pygame.transform.rotate(player_up, -90)
            self.rect.x += self.speedx
            last_move = 3
        elif keys[pygame.K_LEFT]:
            self.image = pygame.transform.rotate(player_up, 90)
            self.rect.x -= self.speedx
            last_move = 4

        if keys[pygame.K_SPACE]:
            self.shoot()
    def shoot(self):
        now = pygame.time.get_ticks()
        if now - self.last_shot > self.shoot_delay:
            self.last_shot = now
            bullet1 = Bullet(self.rect.centerx, self.rect.centery)
            all_sprites.add(bullet1)
            bullets.add(bullet1)

class Bullet(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        a = 10
        b = 10
        self.image = pygame.Surface((a, b))
        self.rect = self.image.get_rect()
        self.rect.centerx = x
        self.rect.centery = y
        self.speedy = 10
        self.speedx = 10
    def update(self):
        global last_move
        if self.rect.x > W or self.rect.x < 0 or self.rect.y > H or self.rect.y < 0:
            self.kill()
        if last_move == 1:
            self.rect.y -= self.speedy
        if last_move == 2:
            self.rect.y += self.speedy
        if last_move == 3:
            self.rect.x += self.speedx
        if last_move == 4:
            self.rect.x -= self.speedx
#Groups
all_sprites = pygame.sprite.Group()
players = pygame.sprite.Group()
bullets = pygame.sprite.Group()
#Sprites
player1 = Player()
#Add Sprites to Groups
all_sprites.add(player1)
players.add(player1)
#Game
while Run:
    keys = pygame.key.get_pressed()
    for i in pygame.event.get():
        if i.type == pygame.QUIT:
            exit()

    all_sprites.draw(sf)
    all_sprites.update()
    pygame.display.flip()
    sf.fill(WHITE)
    clock.tick(FPS)

请帮助我,我将不胜感激。 我是 python 和 pygame 的新手。谢谢

子弹的移动不受玩家当前移动的影响。将参数 speedxspeedy 添加到 class Bullet:

class Bullet(pygame.sprite.Sprite):
    def __init__(self, x, y, speedx, speedy):
        pygame.sprite.Sprite.__init__(self)
        a = 10
        b = 10
        self.image = pygame.Surface((a, b))
        self.rect = self.image.get_rect()
        self.rect.centerx = x
        self.rect.centery = y
        self.speedx = speedx
        self.speedy = speedy
    def update(self):
        if self.rect.x > W or self.rect.x < 0 or self.rect.y > H or self.rect.y < 0:
            self.kill()
        self.rect.x += self.speedx
        self.rect.y += self.speedy

子弹的移动取决于子弹射击时玩家的方向。确定Player.shoot中子弹(speedxspeedy)的移动,依赖last_move,并将移动传递给Bullet的构造函数:

class Player(pygame.sprite.Sprite):
    # [...]

    def shoot(self):
        now = pygame.time.get_ticks()
        if now - self.last_shot > self.shoot_delay:
            speedx, speedy = 10, 0
            if last_move == 1:
                speedx, speedy = 0, -10
            if last_move == 2:
                speedx, speedy = 0, 10
            if last_move == 3:
                speedx, speedy = 10, 0
            if last_move == 4:
                speedx, speedy = -10, 0
            self.last_shot = now
            bullet1 = Bullet(self.rect.centerx, self.rect.centery, speedx, speedy)
            all_sprites.add(bullet1)
            bullets.add(bullet1)