so I was making this space invader game on pygame and now I wanted to add levels to the game so I made a def and put the entire code inside it, but now when I run it works but it does not shoot the bullet. Ive looked around the code again and again but I'm not able to find what/where the mistake is.
#here is the code
import pygame
import random
import math
from pygame import mixer
import time
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("space invaders")
icon = pygame.image.load("rocket.png")
pygame.display.set_icon(icon)
def lvl2():
bg = pygame.image.load("background.png")
mixer.music.load("background.wav")
mixer.music.play(-1)
#player
pimg = pygame.image.load("space-invaders.png")
Px = 370
Py = 480
Px_change = 0
#enemy
eimg = []
ex = []
ey = []
ex_change = []
ey_change = []
num_of_enemies = 8
for i in range(num_of_enemies):
eimg.append(pygame.image.load("alien (1).png"))
ex.append(random.randint(0, 736))
ey.append(random.randint(50, 150))
ex_change.append(2)
ey_change.append(50)
#bullet
bimg = pygame.image.load("bullet.png")
bx = 0
by = 480
bx_change = 0
by_change = 10
b_state = "ready"
#score
score_value = 0
font = pygame.font.Font('freesansbold.ttf', 32)
over_font = pygame.font.Font('freesansbold.ttf', 64)
textx = 10
texty = 10
def show_score(x, y):
score = font.render("score : "+ str(score_value), True, (0,225,0))
screen.blit(score, (x, y))
def game_over_text():
over_text = over_font.render("GAMEOVER", True, (0,225,0))
screen.blit(over_text, (200, 250))
def player(x,y):
screen.blit(pimg, (x, y))
def enemy(x,y,i):
screen.blit(eimg[i], (x, y))
def fire(x, y):
global b_state
b_state = "fire"
screen.blit(bimg, (x+16, y+10))
def collision(ex, ey, bx, by):
distance = math.sqrt((math.pow(ex-bx,2)) + (math.pow(ey-by,2)))
if distance <= 27:
return True
else:
return False
running = True
while running:
screen.fill((0, 0, 0))
screen.blit(bg,(0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a or event.key == pygame.K_LEFT:
Px_change = -4
if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
Px_change = +4
if event.key == pygame.K_SPACE:
if b_state is "ready":
b_sound = mixer.Sound("laser.wav")
b_sound.play()
bx = Px
fire(bx, by)
if event.type == pygame.KEYUP:
if event.key == pygame.K_a or event.key == pygame.K_d or event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
Px_change = 0
Px += Px_change
if Px <= 0:
Px = 0
if Px >= 735:
Px = 735
#game over
for i in range(num_of_enemies):
if ey[i] > 440:
for j in range(num_of_enemies):
ey[j] = 2000
game_over_text()
#pygame.QUIT
break
ex[i] += ex_change[i]
if ex[i] <= 0:
ex_change[i] = 2
ey[i] += ey_change[i]
elif ex[i] >= 736:
ex_change[i] = -2
ey[i] += ey_change[i]
#colllison
coll = collision(ex[i],ey[i],bx,by)
if coll:
by = 480
b_state = "ready"
score_value += 1
kill_sound = mixer.Sound("explosion.wav")
kill_sound.play()
ex[i] = random.randint(0, 736)
ey[i]= random.randint(50, 150)
enemy(ex[i], ey[i], i)
if by <= 0:
by = 480
b_state = "ready"
if b_state is "fire":
fire(bx, by)
by -= by_change
player(Px, Py)
show_score(textx, texty)
pygame.display.update()
lvl2()
if you know how to solve this problem please do help me out
thankyou