Skip to content
Advertisement

Pygame How to check for second press of a key

Hi guys i’m making a game in python using pygame. I have a piece of code that shoots lasers from a ship in a space themed game. Here is the code:

# Laser
laserImg = pygame.image.load('assets/PNG/Lasers/laserBlue01.png')
laserX = 0
laserY = 480
laserX_change = 0
laserY_change = 8
laser_state = "ready"

def fire_laser(x,y):
    global laser_state
    laser_state = "fire"
    screen.blit(laserImg, (x+20,y+9))
    screen.blit(laserImg, (x+70,y+9))

running = True

while running:
    screen.fill((0, 0, 0))

    screen.blit(background,(0,0))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

            if event.key == pygame.K_SPACE:
                if laser_state is "ready":
                    laser_sound = mixer.Sound('assets/Bonus/sfx_laser1.ogg')
                    laser_sound.play()
                    laserX = playerX
                    fire_laser(playerX, laserY)

    # Bullet movement
    if laserY <= 0:
        laserY = 475
        laser_state = "ready"
    
    if laser_state is "fire":
        fire_laser(laserX, laserY)
        laserY -= laserY_change

Well this piece of code shoots two lasers from the ship,

enter image description here

like this.

But i don’t want that. I want to make if player presses space once shoot the left laser if player presses space again than shoot the right laser and when he presses space again it will shoot left laser again. I hope you did understand.

Advertisement

Answer

You need separate positions and states for the left and the right laser

laserX = [0, 0]
laserY = [480, 480]
laser_state = ["ready", "ready"]

You will also need a variable that indicates which laser will fire next

next_laser = 0

Shoot the next laser when SPACE is pressed a nd change the next laser:

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_SPACE:
                if laser_state[next_laser] is "ready":
                    laser_sound = mixer.Sound('assets/Bonus/sfx_laser1.ogg')
                    laser_sound.play()
                    laserX[next_laser] = playerX
                    laser_state[next_laser] = "fire"
                    next_laser = (next_laser + 1) % 2

Move and draw both lasers dependent on its state

while running:
    # [...]

    for i in range(2):
        if laser_state[i] is "fire":
            laserY[i] -= laserY_change
            if laserY[i] <= 0:
                laserY[i] = 475
                laser_state[i] = "ready"
    
    for i in range(2):
        if laser_state[i] is "fire":
            screen.blit(laserImg, (laserX[i] + 20 + i*50, laserY[i]+9))

Minimal example:

import pygame
pygame.init()

screen = pygame.display.set_mode((400, 400))

laserImg = pygame.Surface((3, 12))
laserImg.fill((255, 255, 255))
laserX_change = 0
laserY_change = 8
laserX = [0, 0]
laserY = [330, 330]
laser_state = ["ready", "ready"]
next_laser = 0

playerX, playerY = 175, 330 

clock = pygame.time.Clock()
running = True
while running:
    clock.tick(100)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                if laser_state[next_laser] is "ready":
                    laserX[next_laser] = playerX
                    laser_state[next_laser] = "fire"
                    next_laser = (next_laser + 1) % 2

    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        playerX -= 5
        playerX = max(25, playerX)
    if keys[pygame.K_RIGHT]:
        playerX += 5
        playerX = min(375, playerX)

    for i in range(2):
        if laser_state[i] is "fire":
            laserY[i] -= laserY_change
            if laserY[i] <= 0:
                laserY[i] = 330
                laser_state[i] = "ready"
            
    screen.fill(0)
    for i in range(2):
        if laser_state[i] is "fire":    
            screen.blit(laserImg, (laserX[i] + i*50, laserY[i]+9))
    pygame.draw.rect(screen, (64, 127, 255), (playerX, playerY, 50, 50))
    pygame.display.flip()
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement