Open MaciekMatyszczyk123 opened 1 year ago
I've the same problem too, currently in Create Bullets For Shooting.
Here's my code: `import pygame as pg
import random as rd
pg.init()
screen = pg.display.set_mode((800, 600))
background = pg.image.load('background.png')
pg.display.set_caption("Space Invaders") icon = pg.image.load('ufo.png') pg.display.set_icon(icon)
playerImg = pg.image.load('player.png') playerX = 370 playerY = 480 playerX_change = 0
enemyImg = pg.image.load('enemy.png') enemyX = rd.randint(0, 800) enemyY = rd.randint(50, 150) enemyX_change = 4 enemyY_change = 40
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))
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()
`
I have everything the same in code and bullets are still not appearing
I've already tried screen.blitting it, but nothing showed up
Did you figure it out? I have the same problem for the past 4 days
import pygame import random
pygame.init() running = True
screen = pygame.display.set_mode((1200, 800)) image = pygame.image.load('rocket.png') pygame.display.set_icon(image)
background = pygame.image.load('background.jpg')
player_image = pygame.image.load('spaceship.png') playerX = 550 playerY = 650 playerchange = 50
enemy_image = pygame.image.load('enemies.png') enemyX = random.randint(0, 1200) enemyY = random.randint(50, 150) enemychangeX = 0.3 enemychangeY = 30
bullet_image = pygame.image.load('bullet.png') bulletX = 0 bulletY = 650 bulletchangeX = 0 bulletchangeY = 10 bullet_state = False
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))
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()
I have everything the same in code and bullets are still not appearing