I am trying to create a game using pygame in python. I am not able to implement continuous shooting in pygame.
This is the code that I have written till now:
import pygame
import random
pygame.init()
x_SHIP = 50
y_SHIP = 500
x_LASER = 0
y_LASER = 500
LASER_CHANGE = 5
laser_state = 'ready'
vel = 5
bound_LEFT = 0
bound_RIGHT = 520
YELLOW_SPACE_SHIP = pygame.transform.scale(pygame.image.load('assets/pixel_ship_yellow.png'), (80, 72))
BLUE_LASER = pygame.transform.scale(pygame.image.load('assets/pixel_laser_blue.png'), (81, 90))
GREEN_LASER = pygame.transform.scale(pygame.image.load('assets/pixel_laser_green.png'), (81, 90))
RED_LASER = pygame.transform.scale(pygame.image.load('assets/pixel_laser_red.png'), (81, 90))
YELLOW_LASER = pygame.transform.scale(pygame.image.load('assets/pixel_laser_yellow.png'), (81, 90))
win = pygame.display.set_mode( (600, 600) )
pygame.display.set_caption('First Game')
Lasers = [BLUE_LASER, GREEN_LASER, RED_LASER, YELLOW_LASER]
run = True
Laser = random.choice(Lasers)
while run:
# Laser = random.choice(Lasers)
pygame.time.delay(10)
win.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] or keys[pygame.K_a]:
x_SHIP -= vel
if x_SHIP <= bound_LEFT:
x_SHIP = bound_LEFT
if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
x_SHIP += vel
if x_SHIP >= bound_RIGHT:
x_SHIP = bound_RIGHT
if keys[pygame.K_SPACE]:
global laser_state
if laser_state == "ready":
x_LASER = x_SHIP
laser_state = "fire"
if y_LASER <= 0:
y_LASER = 500
laser_state = 'ready'
Laser = random.choice(Lasers)
if laser_state == 'fire':
win.blit(Laser, (x_LASER, y_LASER))
y_LASER -= LASER_CHANGE
win.blit(YELLOW_SPACE_SHIP, (x_SHIP,y_SHIP))
pygame.display.update()
pygame.quit()