Pygame Tutorial for Beginners - Python Game Development Course

Hi Team ,

I was watching and learning this awesome video on youtube , but stuck in (1:12:08) Creating Bullets for Shooting

The script is running fine but bullets fired only once from the player but afterwards it is not working like bullets are not coming from the player(Spaceship), already i have called it in while loop . Please suggest on this …

My Script is as below :

import pygame
import random

# Initialize the PyGame
pygame.init()
# Create the Screen
screen = pygame.display.set_mode((1000, 600))

# Title & Icon

pygame.display.set_caption("Space Warriors")
icon = pygame.image.load('C:/Python/Python37-32/Scripts/PyGame/spaceship.png')
pygame.display.set_icon(icon)

# Background Icon
backgroundimg = pygame.image.load('C:/Python/Python37-32/Scripts/PyGame/SpaceshipBackground.png')

# Player in the Screen
Playerimg = pygame.image.load('C:/Python/Python37-32/Scripts/PyGame/space-invaders1.png')
PlayerX = 500
PlayerY = 480
Player_Xchange = 0

# Enemy in the Screen
Enemyimg = pygame.image.load('C:/Python/Python37-32/Scripts/PyGame/ufo.png')
EnemyX = random.randint(0, 1000)
EnemyY = random.randint(40, 70)
Enemy_Xchange = 4
Enemy_Ychange = 40

# Bullet in the Screen
Bulletimg = pygame.image.load('C:/Python/Python37-32/Scripts/PyGame/bullets.png')
BulletX = 0
BulletY = 480
Bullet_Xchange = 0
Bullet_Ychange = 10
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:
    ##Updating the Screen Background Col
    screen.fill((0, 80, 20))
    ##Background Screen
    screen.blit(backgroundimg, (0, 0))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            # If KeyStroke is pressed check whether it is right or left
        if event.type == pygame.KEYDOWN:

            if event.key == pygame.K_LEFT:
                Player_Xchange = -5
            if event.key == pygame.K_RIGHT:
                Player_Xchange = 5
            if event.key == pygame.K_SPACE:
                fire_bullet(PlayerX, BulletY)
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                Player_Xchange = 0

    PlayerX += Player_Xchange
    if PlayerX <= 0:
        PlayerX = 0
    elif PlayerX >= 936:
        PlayerX = 936

    EnemyX += Enemy_Xchange
    if EnemyX <= 0:
        Enemy_Xchange = 4
        EnemyY += Enemy_Ychange
    elif EnemyX >= 936:
        Enemy_Xchange = -4
        EnemyY += Enemy_Ychange
    # Bullet Movement
    if Bullet_State is "fire":
        fire_bullet(PlayerX, BulletY)
        BulletY -= Bullet_Ychange

    player(PlayerX, PlayerY)
    enemy(EnemyX, EnemyY)

    pygame.display.update()

Many thanks in Advance.

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Hi Team ,

Also Stuck in the Video Creating Multiple Enemies …

Below is the error i am receiving :slight_smile:

Traceback (most recent call last):
  File "C:/Python/Python37-32/Scripts/PyGame/PyGame_Python.py", line 115, in <module>
    collision = isCollision(EnemyX[i], EnemyY[i], BulletX, BulletY)
  File "C:/Python/Python37-32/Scripts/PyGame/PyGame_Python.py", line 66, in isCollision
    distance = math.sqrt((math.pow(EnemyX - BulletX, 2)) + (math.pow(EnemyY - BulletY, 2)))
TypeError: unsupported operand type(s) for -: 'list' and 'int'

Hi Team ,

Any update on the error i am facing . Can you please help in this regard ?

Many thanks in advance.

Regards ,
Subasish