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()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…