Search code examples
pythonpygamebullet

how to shoot bullets in pygame?


I'm making a space invaders type game and wanted to ask how i can make it so that the bullet in the following code doesn't follow the spaceship once its shot.

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()

I tried to change the bulletX to the bulletX that it was when it was shot but couldn't manage it. Do I need to use a completely different system for the bullet or can i do it with mine.


Solution

  • You just need to set bulletX = characterX + 20 once - when the bullet is fired. Currently you set it to that with each while run loop iteration, which is why its position is getting updated constantly. So, if you removed that, and updated the X position only when needed, this is how that part of your code could look:

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

    Although you'll want something to keep track of if the bullet is currently fired or not, so that holding down or repeatedly pressing space doesn't reset the bullet's position.