本文介绍了Pygame 如何检查按键的第二次按下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在使用 pygame 在 python 中制作游戏.我有一段代码可以在太空主题游戏中从船上发射激光.代码如下:

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,

喜欢这个.

但我不想那样.我想如果玩家按下空格键一次发射左边的激光,如果玩家再次按下空格键而不是发射右边的激光,当他再次按下空格键时,它会再次发射左边的激光.我希望你明白.

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.

推荐答案

左右激光需要分开的位置和状态

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

这篇关于Pygame 如何检查按键的第二次按下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 05:46