简体   繁体   中英

why isnt event.type == pygame.KEYDOWN working

alright so I have been trying to start learning game development in the past couple days and I finally started working on a project. The problem is that for some reason my controls aren't working even when I made everything like it is supposed to be. Please help. (also this is the entire code because I dont know where I am wrong)

#instalize the game VERY IMPORTANT
pygame.init()

# this is the window
screen = pygame.display.set_mode((800, 600))
#title and icon
pygame.display.set_caption("Space invaders")
icon = pygame.image.load('pictures/ufo.png')
pygame.display.set_icon(icon)

#player
playerImg = pygame.image.load('pictures/player.png')
playerX = 370
playerY = 480
PlayerX_change = 0


def player(x,y):
    screen.blit(playerImg, (x,y))


#game loop
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running= False

                # if keystroke is pressed move right or left
            if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_LEFT:
                        playerX_change = -0.1
                    if event.key == pygame.K_RIGHT:
                        playerX_change = 0.1
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    playerX_change = 0.1





    #RGB
    screen.fill((0, 0, 0))

    playerX += PlayerX_change
    player(playerX, playerY)
    pygame.display.update()

It is a matter of Indentation . You cannot nest events:

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

        # INDENTATION
        #<--|

        # if keystroke is pressed move right or left
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                playerX_change = -0.1
            if event.key == pygame.K_RIGHT:
                playerX_change = 0.1
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                playerX_change = 0.1

    # [...]

Your identation is not correct. I have corrected it and attached the code.

#instalize the game VERY IMPORTANT
pygame.init()

# this is the window
screen = pygame.display.set_mode((800, 600))
#title and icon
pygame.display.set_caption("Space invaders")
icon = pygame.image.load('pictures/ufo.png')
pygame.display.set_icon(icon)

#player
playerImg = pygame.image.load('pictures/player.png')
playerX = 370
playerY = 480
PlayerX_change = 0


def player(x,y):
    screen.blit(playerImg, (x,y))


#game loop
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running= False

        # if keystroke is pressed move right or left
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                playerX_change = -0.1
            if event.key == pygame.K_RIGHT:
                playerX_change = 0.1
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                playerX_change = 0.1


    #RGB
    screen.fill((0, 0, 0))

    playerX += PlayerX_change
    player(playerX, playerY)
    pygame.display.update()

Your indentation is incorrect

Try usinng this;

    if event.type == pygame.QUIT:
        running= False

    # if keystroke is pressed move right or left
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_LEFT:
            playerX_change = -0.1
        if event.key == pygame.K_RIGHT:
            playerX_change = 0.1
    if event.type == pygame.KEYUP:
        if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
            playerX_change = 0.1

Also if you use event.KEYDOWN it not checks multiple keys although you can use pygame.key.get_pressed()

Use:

keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
   code:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM