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
– Woss
Strange, because I tried one last time, and it’s still not working.
– Christyan_714
Do you have any idea what it might be, and how to solve?
– Christyan_714
How is the code executed? Error?
– Woss
No, it just showed the pygame version and the community link.
– Christyan_714
And how he executed?
– Woss
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.
– Christyan_714
And how he executed?
– Woss
I’m sorry, but I don’t understand your question.
– Christyan_714
How did you execute the code? Straight to the terminal?
– Woss
Exactly, it was through the terminal.
– Christyan_714
Hi, I looked at my settings, and solved my problem, I’m sorry anything.
– Christyan_714