pygame error using key.get_pressed()

Asked

Viewed 23 times

0

I was testing the pygame, and I came across a mistake. I made a red square appear on the screen, and I made the code for it to move too, set the WASD for locomotion, but when I ran the code, and pressed the configured keys (WASD) the square did not move. So I decided to change the keys configured by the arrows, when I ran the code, everything was normal the square was moving from the correct lane with the arrows, but not with the WASD.

My code with the WASD

import pygame
from pygame.locals import *
import sys
# Iniciando os pacotes do pygame
pygame.init() 
# tela onde passará o jogo
screen = pygame.display.set_mode([800,500]) 

x = 200
y = 200

while True:
    # verificando todos os eventos do usuário
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit() # se o usuário apertar para sair, a janela fecha
        
    screen.fill([0,0,0])
            
   # quadrado vermelho que irá aparecer na tela
    quadrado = pygame.draw.rect(screen, (255,0,0), (x,y,40,40))

    if pygame.key.get_pressed()[K_w]:
        y -= 20
    if pygame.key.get_pressed()[K_s]:
        y += 20
    if pygame.key.get_pressed()[K_a]:
        x -= 20
    if pygame.key.get_pressed()[K_d]:
        x += 20
    
    # atualiza os frames do jogo
    pygame.display.update()

my code with the arrows

import pygame
from pygame.locals import *
import sys
# Iniciando os pacotes do pygame
pygame.init() 
# tela onde passará o jogo
screen = pygame.display.set_mode([800,500]) 

x = 200
y = 200

while True:
    # verificando todos os eventos do usuário
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit() # se o usuário apertar para sair, a janela fecha
        
    screen.fill([0,0,0])
            
   # quadrado vermelho que irá aparecer na tela
    quadrado = pygame.draw.rect(screen, (255,0,0), (x,y,40,40))

    if pygame.key.get_pressed()[K_UP]:
        y -= 20
    if pygame.key.get_pressed()[K_DOWN]:
        y += 20
    if pygame.key.get_pressed()[K_LEFT]:
        x -= 20
    if pygame.key.get_pressed()[K_RIGHT]:
        x += 20
    
    # atualiza os frames do jogo
    pygame.display.update() 

So far I haven’t tried anything, because I have no idea what to try. I appreciate.

  • Here worked normal with the WASD keys

  • Strange, because I tried one last time, and it’s still not working.

  • Do you have any idea what it might be, and how to solve?

  • How is the code executed? Error?

  • No, it just showed the pygame version and the community link.

  • And how he executed?

  • When I hold a key (WASD) the square walks a little and chokes. After that it does not move anymore. Already with the arrows, the game flowed normally.

  • And how he executed?

  • I’m sorry, but I don’t understand your question.

  • How did you execute the code? Straight to the terminal?

  • Exactly, it was through the terminal.

  • Hi, I looked at my settings, and solved my problem, I’m sorry anything.

Show 7 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.