为什么子弹不会朝玩家面向的方向发射并在 PyGame 内保持可见?

Why won't the bullets fire in the direction the player is facing and stay visible in PyGame?

我正在尝试使用 PyGame 在 Python 中制作自上而下的射击游戏,我希望根据玩家的方向将子弹绘制到屏幕上。我可以绘制子弹,但只有当玩家处于那个特定方向时(有一个方向变量,但子弹仅在变量与子弹射击的状态匹配时显示)。

这是我认为相关的代码

    global direction
    global bullets
    global bullet_speed
    direction = None
    bullets = []
    bullet_speed = []

    player_x = 100
    player_y = 100

    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
           if event.key == pygame.K_LEFT:
              direction = 'left'
           if event.key == pygame.K_SPACE:
              bullets.append([player_x,player_y])

    gameDisplay.fill(BLACK)

    for draw_bullets in bullets:
        if direction = 'up':
           pygame.draw.rect(gameDisplay,WHITE,(draw_bullet[0] + (player_x / 2), draw_bullet[1] + 5, bullet_width, bullet_height))
           bullet_speed.append(speed)
        if direction = 'down':
           pygame.draw.rect(gameDisplay,WHITE,(draw_bullet[0] + (player_x / 2),draw_bullet[1] + (player_height + 5), bullet_width, bullet_height))

    pygame.display.update()

我不想在鼠标指向的地方发射子弹,这是所有其他问题的答案。我只希望每颗子弹都朝玩家指向的方向(向上、向下、向左、向右)发射,并继续朝它开始的任何方向前进。如果有人可以帮助我,我将不胜感激。我也不使用 OOP。

我建议重新设计子弹,使它们包含一个矩形样式列表(或更好 pygame.Rect) consisting of the position and their size and another list, their velocity vector。现在要更新子弹的位置,您只需将速度添加到该位置即可。

这是带有一些注释的更新代码。我强烈建议使用 pygame.Rects 而不是列表,因为它们对于碰撞检测非常有用。

#!/usr/bin/env python3

import pygame as pg
from pygame.math import Vector2


pg.init()

BACKGROUND_COLOR = pg.Color(30, 30, 30)
BLUE = pg.Color('dodgerblue1')
ORANGE = pg.Color('sienna1')


def game_loop():
    screen = pg.display.set_mode((800, 600))
    screen_rect = screen.get_rect()
    clock = pg.time.Clock()
    # Use a pg.Rect for the player (helps with collision detection):
    player = pg.Rect(50, 50, 30, 50)
    player_velocity = Vector2(0, 0)

    bullets = []
    bullet_speed = 7
    bullet_velocity = Vector2(bullet_speed, 0)

    running = True
    while running:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                running = False
            if event.type == pg.KEYDOWN:
                if event.key == pg.K_LEFT:
                    player_velocity.x = -5
                    bullet_velocity = Vector2(-bullet_speed, 0)
                elif event.key == pg.K_RIGHT:
                    player_velocity.x = 5
                    bullet_velocity = Vector2(bullet_speed, 0)
                elif event.key == pg.K_UP:
                    player_velocity.y = -5
                    bullet_velocity = Vector2(0, -bullet_speed)
                elif event.key == pg.K_DOWN:
                    player_velocity.y = 5
                    bullet_velocity = Vector2(0, bullet_speed)
                elif event.key == pg.K_SPACE:
                    # A bullet is a list consisting of a rect and the
                    # velocity vector. The rect contains the position
                    # and size and the velocity vector is the
                    # x- and y-speed of the bullet.
                    bullet_rect = pg.Rect(0, 0, 10, 10)
                    bullet_rect.center = player.center
                    bullets.append([bullet_rect, bullet_velocity])
            if event.type == pg.KEYUP:
                if event.key == pg.K_LEFT or event.key == pg.K_RIGHT:
                    player_velocity.x = 0
                elif event.key == pg.K_UP or event.key == pg.K_DOWN:
                    player_velocity.y = 0

        # Move the player.
        player.x += player_velocity.x
        player.y += player_velocity.y

        # To move the bullets, add their velocities to their positions.
        for bullet in bullets:
            bullet[0].x += bullet[1].x  # x-coord += x-velocity
            bullet[0].y += bullet[1].y  # y-coord += y-velocity

        # Stop the player at the borders of the screen.
        player.clamp_ip(screen_rect)

        screen.fill(BACKGROUND_COLOR)
        pg.draw.rect(screen, BLUE, player)
        # Draw the bullets.
        for bullet in bullets:
            pg.draw.rect(screen, ORANGE, bullet[0])

        pg.display.update()
        clock.tick(60)

game_loop()
pg.quit()