Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Bullet firing
#1
I'm working on a python game for uni and Im having an issue. I cant seem to get the bullets to fire from my spaceship and I dont know why? Ive checked that the keyboard input is working by running a console test print and it is. Ive then checked that the function is being called by the same method and it is as well so not sure whats going on any help would be much appreciated. Heres the code

import pygame
import random
pygame.init()
# set screen size
screen = pygame.display.set_mode((800, 600))
# load images
pygame.display.set_caption("Space Invaders")
icon = pygame.image.load('spaceship.png')
backgroundImg = pygame.image.load('background.jpg')
bulletImg = pygame.image.load('bullet.png')
enemyImg = pygame.image.load('enemy.png')
playerImg = pygame.image.load('player.png')
# set player position
playerX = 400
playerY = 500
player_change = 0
# set enemy position
enemyX = random.randint(0, 736)
enemyY = random.randint(50, 200)
enemy_change = 1
enemyY_change = 20
# set bullet position
bulletX = playerX
bulletY = 500
bullet_change = 0
bulletY_change = 10
bullet_state = 'ready'
# define player function
def player(x, y):
    screen.blit(playerImg, (int(x), int(y)))
# define enemy function
def enemy(x, y):
    screen.blit(enemyImg, (int(x), int(y)))
# define bullet fire function
def fire_bullets(x, y):
    print("test")
    global bullet_state
    bullet_state = 'fire'
    screen.blit(bulletImg, (x + 16, y + 10))
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                player_change += 1.5
            if event.key == pygame.K_LEFT:
                player_change += -1.5
            if event.key == pygame.K_SPACE:
                fire_bullets(playerX, bulletY)
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT:
                player_change = 0
    screen.fill((0, 0, 255))
    screen.blit(backgroundImg, (0, 0))
    playerX += player_change
    enemyX += enemy_change
    player(playerX, playerY)
    enemy(enemyX, enemyY)
    pygame.display.update()
    if playerX <= 0:
        playerX = 0
    elif playerX >= 736:
        playerX = 736
    if enemyX <= 0:
        enemy_change += 1
        enemyY += enemyY_change
    elif enemyX >= 736:
        enemy_change += -1
        enemyY += enemyY_change
    if bullet_state is 'fire':
        fire_bullets(playerX, bulletY)
        bulletY -= bulletY_change
Reply
#2
Your code is all over the place and hard to dissect. It is much easier to have a 1) Bullet class 2) use pygame rects for coordinates 3) and arbitrary surfaces so that we dont have to have the resources to run the program. Then you can have a bullets list of Bullet objects in the Player class to contain all the bullets and their respective coordinates. In this tutorial i do just that.
Recommended Tutorials:
Reply
#3
(Aug-07-2020, 09:56 PM)metulburr Wrote: Your code is all over the place and hard to dissect. It is much easier to have a 1) Bullet class 2) use pygame rects for coordinates 3) and arbitrary surfaces so that we dont have to have the resources to run the program. Then you can have a bullets list of Bullet objects in the Player class to contain all the bullets and their respective coordinates. In this tutorial i do just that.
Interesting approach! But wouldn't using arbitrary surfaces limit testing certain visual aspects later on?
buran write Apr-09-2025, 01:51 PM:
Spam link removed
Reply
#4
Thumbs Up 
Hey! Sounds like you're super close to getting it working. Thanks for dropping the code too—it really helps.

So the reason your bullets aren’t showing might be because you're setting bullet_state to 'fire' and calling fire_bullets(playerX, bulletY), but you're not actually updating the bulletY position outside of that condition to make it move every frame. Once the bullet is "fired," it should keep moving up the screen on every game loop iteration. But in your current code, it seems like bulletY doesn’t reset when the bullet goes off the screen, and you don’t have logic that handles the bullet’s ongoing movement unless space is being pressed.

Here's what you can try to fix it:

Move bulletY -= bulletY_change into the main loop (not just inside the spacebar press condition).

Reset bulletY and bullet_state back to "ready" when the bullet leaves the screen.

Only allow a new bullet to be fired if bullet_state is "ready", to avoid multiple bullets stacking weirdly.

Let me show you how you can fix this:

if bullet_state == 'fire': fire_bullets(playerX, bulletY) bulletY -= bulletY_change if bulletY <= 0: bulletY = 500 bullet_state = 'ready'

And update your spacebar press code like this:

if event.key == pygame.K_SPACE: if bullet_state == 'ready': bulletX = playerX # Set bulletX to player's current position fire_bullets(bulletX, bulletY)

So the idea is: only shoot if the bullet is ready, then start moving it up every frame until it leaves the screen. Once it’s gone, reset it so it can be fired again.

Let me know if you want help adding multiple bullets or sound effects later—that’s a fun upgrade!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I want to add a bullet function to my game. how would i go about it? Iyjja 5 2,759 Jan-09-2024, 05:42 PM
Last Post: Iyjja

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020