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


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 905 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