如何在 pygame 中发射子弹?

how to shoot bullets in pygame?

我正在制作一个 space 入侵者类型的游戏,想问问我如何制作它,以便下面代码中的子弹在射击后不会跟随 space 飞船。

import pygame, random, math, sys

pygame.init()
window = pygame.display.set_mode((400, 700))
background = pygame.image.load('sky(background).png')

character = pygame.image.load('start-up.png')
characterX = 165
characterY = 600
x_speed = 0

def character_blit(x, y):
    window.blit(character, (x, y))

bullet = pygame.image.load('torpedo (1).png')
bulletY = 600
y_speed_bullet = 0

def bullet_blit(x, y):
    window.blit(bullet, (x, y))

comets = pygame.image.load('asteroid.png')
comet_x = random.randint(0, 336)
comet_y = 0
y_speed = 0.3

def comet_blit(x, y):
    window.blit(comets, (x, y))

def iscollision(x1, y1, x2, y2):
    distance = math.sqrt((math.pow(x1 - x2, 2))
                         + (math.pow(y1 - y2, 2)))
    if distance < 47:
        return True
    else:
        return False


transparent = (0, 0, 0, 0)
score = 0
comets_that_hit_earth = 0
run = True
while run:
    window.fill((0, 0, 0))
    window.blit(background, (0, 0))
    bulletX = characterX + 20
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                x_speed = -0.3
            if event.key == pygame.K_RIGHT:
                x_speed = 0.3
            if event.key == pygame.K_SPACE:
                y_speed_bullet = y_speed * -1 * 2.3
                bulletX = 
    collision = iscollision(bulletX, bulletY, comet_x, comet_y)

    if collision:
        score += 1
        comet_y = -64
        comet_x = random.randint(0, 336)
        bulletY = 600
    elif bulletY <= -24:
        bulletY = 600

    collision_player_and_comet = iscollision(characterX, characterY, comet_x, comet_y)
    if collision_player_and_comet:
        sys.exit()
    characterX += x_speed
    comet_y += y_speed
    bulletY += y_speed_bullet
    if characterX <= -64:
        characterX = 400
    elif characterX >= 464:
        characterX = 0
    if comet_y >= 764:
        comet_y = 0
        y_speed += 0.005
        comets_that_hit_earth += 1
        comet_x = random.randint(0, 336)
    bullet_blit(bulletX, bulletY)
    character_blit(characterX, characterY)
    comet_blit(comet_x, comet_y)
    pygame.display.update()

我试图将bulletX 更改为射击时的bulletX 但无法管理。我需要为子弹使用完全不同的系统还是可以用我的来做。

您只需要设置一次 bulletX = characterX + 20 - 当子弹发射时。目前你将它设置为每个 while run 循环迭代,这就是它的位置不断更新的原因。因此,如果您删除它,并仅在需要时更新 X 位置,这就是您的代码部分的样子:

if event.key == pygame.K_SPACE:
    y_speed_bullet = y_speed * -1 * 2.3
    bulletX = characterX + 20

虽然您需要一些东西来跟踪当前是否发射了子弹,但按住或反复按下 space 不会重置子弹的位置。

不要在应用程序循环中连续设置x坐标。

设置y_speed_bullet == 0时的x坐标。这导致子弹被固定在飞船旁边,但发射时沿着一条直线:

if y_speed_bullet == 0:
    bulletX = characterX + 20

设置按下SPACE时子弹的起始位置:

if event.key == pygame.K_SPACE:
    y_speed_bullet = y_speed * -1 * 2.3
    bulletX = characterX + 20

当子弹从 window 或击中彗星时设置 y_speed_bullet = 0


run = True
while run:
    window.fill((0, 0, 0))
    window.blit(background, (0, 0))

    if y_speed_bullet == 0:
        bulletX = characterX + 20

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                x_speed = -0.3
            if event.key == pygame.K_RIGHT:
                x_speed = 0.3
            if event.key == pygame.K_SPACE:
                y_speed_bullet = y_speed * -1 * 2.3
                bulletX = characterX + 20

    collision = iscollision(bulletX, bulletY, comet_x, comet_y)
    if collision:
        score += 1
        comet_y = -64
        comet_x = random.randint(0, 336)
    if collision or bulletY <= -24:
        bulletY = 600
        y_speed_bullet = 0