Pygame Beginner Tutorial Code doesn't work

Was following a tutorial by freeCodeCamp on YouTube on pygame and the darned spaceship won’t fire the bullet. I am an absolute beginner to this and would be overjoyed if someone could look at my code and figure out what I’m missing. I tried the comments and other sources but to no avail.

The link for the video:

import random

import pygame

need to add this line to initialize pygame

pygame.init()

this is used to create a screen, have to remember to add double brackets for it to work

screen = pygame.display.set_mode((800, 600))

Background

background = pygame.image.load(‘background.png’)

Title and Icon

pygame.display.set_caption(“Space Invaders”)
icon = pygame.image.load(‘ufo.png’)
pygame.display.set_icon(icon)

Player

playerImg = pygame.image.load(‘player.png’)
playerX = 380
playerY = 490
pMove = 0

Enemy

enemyImg = pygame.image.load(‘enemy.png’)
enemyX = random.randint(0, 800)
enemyY = random.randint(50, 150)
eMoveX = 2
eMoveY = 20

Bullet

Ready - You can’t see the bullet on the screen

Fire - Can see when SPACE is pressed

bulletIMG = pygame.image.load(‘bullet.png’)
bulletX = 0
bulletY = 400
bMoveX = 0
bMoveY = 20
bullet_state = “ready”

def player(x, y):
screen.blit(playerImg, (x, y))

def enemy(x, y):
screen.blit(enemyImg, (x, y))

def fire_bullet(x, y):
global bullet_state
bullet_state = “fire”
screen.blit(bulletIMG, (x + 16, y + 10))

Game Loop

running = True
while running:
screen.fill((0, 0, 0))
# background Image
screen.blit(background, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

    # checks if keystroke is pressed
if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_LEFT:  # Left Arrow Key
        pMove = -5
    if event.key == pygame.K_RIGHT:  # Right Arrow Key
        pMove = 5
    if event.key == pygame.K_SPACE:  # Spacebar
        fire_bullet(playerX, bulletY)
    if event.key == pygame.K_SPACE:
        if bullet_state is "ready":
            bulletSound = mixer.Sound("laser.wav")
            bulletSound.play()
            # Get the current x cordinate of the spaceship
            bulletX = playerX
            fire_bullet(bulletX, bulletY)

if event.type == pygame.KEYUP:
    if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
        pMove = 0

# player Boundary
playerX += pMove
if playerX <= 0:
    playerX = 0
elif playerX >= 735:
    playerX = 735

# enemy Movement and Boundary
enemyX += eMoveX

if enemyX >= 735:
    eMoveX = -3
    enemyY += eMoveY
elif enemyX <= 0:
    eMoveX = 3
    enemyY += eMoveY

# Bullet Movement
if bulletY <= 0:
    bulletY = 480
    bullet_state = "ready"

if bullet_state is "fire":
    fire_bullet(playerX, bulletY)
    bulletY -= bMoveY

player(playerX, playerY)
enemy(enemyX, enemyY)
pygame.display.update()  # update line should always be included
1 Like

Interesting code, i am going to try to check it out myself, thanks!

1 Like

Still gotten around testing the code. Though the video is providing working code, so you could look at it side-by-side.

The only thing I found weird is that in you have two checks for the Spacebar. Apart from that, I looks like it should work.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.