attreyabhatt / Space-Invaders-Pygame

Creating the game Space Invaders using Pygame Module in Python
322 stars 332 forks source link

bullets are not appearing after pressing the spacebar #48

Open MaciekMatyszczyk123 opened 1 year ago

MaciekMatyszczyk123 commented 1 year ago

I have everything the same in code and bullets are still not appearing

ghost commented 1 year ago

I've the same problem too, currently in Create Bullets For Shooting.

ghost commented 1 year ago

Here's my code: `import pygame as pg

import random as rd

Initialize the pygame

pg.init()

create the screen

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

Background

background = pg.image.load('background.png')

Title and Icon

pg.display.set_caption("Space Invaders") icon = pg.image.load('ufo.png') pg.display.set_icon(icon)

Player

playerImg = pg.image.load('player.png') playerX = 370 playerY = 480 playerX_change = 0

Enemy

enemyImg = pg.image.load('enemy.png') enemyX = rd.randint(0, 800) enemyY = rd.randint(50, 150) enemyX_change = 4 enemyY_change = 40

Bullet

Ready = You can't see the bullet on the screen

Fire = The bullet is currently moving

bulletImg = pg.image.load('bullet.png') bulletX = 0 bulletY = 480 bulletX_change = 0 bulletY_change = 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:

# RGB = Red, Green, Blue
screen.fill((0, 0, 0))
# Background image
screen.blit(background, (0, 0))
for event in pg.event.get():
    if event.type == pg.QUIT:
        running = False

# If keystroke is pressed check whether right or left
if event.type == pg.KEYDOWN:
    if event.key == pg.K_LEFT:
        playerX_change = -5
    if event.key == pg.K_RIGHT:
        playerX_change = 5
    if event.key == pg.K_SPACE:
        fire_bullet(playerX,bulletY)

if event.type == pg.KEYUP:
    if event.key == pg.K_LEFT or event.key == pg.K_RIGHT:
        playerX_change = 0

# x = 5 + 0.1 -> 5 - 5 -0.1
# 5 - 5 + 0.1

# Checking for boundaries of spaceship, so it doesn't go out of bounds
playerX += playerX_change

if playerX <= 0:
    playerX = 0
elif playerX >= 736:
    playerX = 736

# Enemy movement
enemyX += enemyX_change

if enemyX <= 0:
    enemyX_change = 4
    enemyY += enemyY_change
elif enemyX >= 730:
    enemyX_change = -4
    enemyY += enemyY_change

# Bullet Movement
if bullet_state is "fire":
    fire_bullet(playerX, bulletY)
    bulletY -= bulletY_change

player(playerX, playerY)
enemy(enemyX, enemyY)

pg.display.update()

`

ghost commented 1 year ago

I have everything the same in code and bullets are still not appearing

I've already tried screen.blitting it, but nothing showed up

Huzaima2 commented 2 months ago

Did you figure it out? I have the same problem for the past 4 days

Huzaima2 commented 2 months ago

import pygame import random

pygame.init() running = True

Screen

screen = pygame.display.set_mode((1200, 800)) image = pygame.image.load('rocket.png') pygame.display.set_icon(image)

Background

background = pygame.image.load('background.jpg')

player

player_image = pygame.image.load('spaceship.png') playerX = 550 playerY = 650 playerchange = 50

Enemy

enemy_image = pygame.image.load('enemies.png') enemyX = random.randint(0, 1200) enemyY = random.randint(50, 150) enemychangeX = 0.3 enemychangeY = 30

Bullet

bullet_image = pygame.image.load('bullet.png') bulletX = 0 bulletY = 650 bulletchangeX = 0 bulletchangeY = 10 bullet_state = False

Drawing spaceship on the surface

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

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

def fire_bullet(x, y): global bullet_state bullet_state = True screen.blit(bullet_image, (x + 16, y + 10))

Game loop

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_LEFT:
            playerX = playerX - playerchange
        if event.key == pygame.K_RIGHT:
            playerX = playerX + playerchange
        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:
            playerX = playerX
# Player movement
if playerX <= 0:
    playerX = 0
if playerX >= 1072:
    playerX = 1072

# Enemy Movement
enemyX += enemychangeX
if enemyX <= 0:
    enemychangeX = 0.3
    enemyY += enemychangeY
elif enemyX >= 1072:
    enemychangeX = -0.3
    enemyY += enemychangeY

# Bullet movement
if bullet_state is True:
    fire_bullet(playerX, bulletY)
    bulletY -= bulletchangeY

screen.fill((0, 0, 0))
player(playerX, playerY)
enemy(enemyX, enemyY)
pygame.display.update()