0

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()
mkrieger1
  • 14,486
  • 4
  • 43
  • 54
  • 2
    What exactly was the problem when you tried to implement continuous shooting? – mkrieger1 Aug 03 '20 at 16:16
  • The problem is when I hit the space bar then one laser comes out, not a continuous stream of lasers – Rishi Priyansh Aug 03 '20 at 16:58
  • Did you search for "pygame key held down" or similar? I found these two questions which have answers that should solve your problem: [question 1](https://stackoverflow.com/questions/22093662/how-to-efficiently-hold-a-key-in-pygame), [question 2](https://stackoverflow.com/questions/30914765/pygame-detect-if-a-key-is-held-down) – mkrieger1 Aug 03 '20 at 17:14
  • I can't replicate the issue. When I hold space-bar in, a laser flys to the top of the screen, then a new laser starts at the ship, flys up... Is this *not* what you want? – Kingsley Aug 03 '20 at 22:23
  • That's not what I want, with this code the laser flys up, till the laser hits the top, I can't shoot another laser. whereas I want to shoot a stream of lasers – Rishi Priyansh Aug 04 '20 at 11:34

1 Answers1

0

In order to do this you will need to track multiple laser positions. It won't work the way you have it currently. I've created a class to represent the laser. It holds the x y coordinate and the an image for the laser. When space is pushed a new laser object is created and added to a list. Then we loop through the list and handle each laser. After the laser reaches the top of the screen we add it to a separate list and use that to remove old lasers from the original list. I've also added a timer in for a delay between each laser shot. Let me know if you have any questions. Using objects is crucial in game programming.

import pygame
import random

class projectile:
    def __init__(self,x,y,image):
        self.x = x
        self.y = y
        self.image = image

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)
    
laser_state

lasersBlast = []

lasersToRemove=[]

coolDown=0
coolDownTime=3

while run:
    
    if coolDown>0:
        coolDown-=1
    lasersToRemove=[]

#     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]:
       
        if coolDown<1:
            coolDown = coolDownTime
            tmpLaser = projectile(x_SHIP,500,random.choice(Lasers))
            lasersBlast.append(tmpLaser)
        # 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
    
    for l in lasersBlast:
        l.y-=LASER_CHANGE
        win.blit(l.image, (l.x, l.y))
        if l.y<0:
            lasersToRemove.append(l)
    for r in lasersToRemove:
        lasersBlast.remove(r)
    

    win.blit(YELLOW_SPACE_SHIP, (x_SHIP,y_SHIP))
    pygame.display.update()

    
pygame.quit()

Helpful Links:

Objects: https://www.w3schools.com/python/python_classes.asp

Lists: https://www.w3schools.com/python/python_lists.asp

Lolo
  • 57
  • 4