Why does this not work
import pygame
pygame.init()
while True:
pressed = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if pressed[pygame.K_w]:
print("w is pressed")
elif pressed[pygame.K_s]:
print("s is pressed")
I installed pygame and python today. It should theoretically be the latest of both pygame and python. this code has gone through many iterations and none of them have worked. I even tried:
print(pygame.key.get_pressed()[pygame.K_w])
and that printed only 0. Even when pressing and holding w. I've tried everything. I tried using:
pygame.event.pump()
but that did nothing.
no errors or anything, just nothing happens. it just prints what I type on the screen.
I've never posted on stackoverflow before, so sorry if I did something wrong.
Edit: I altered the code from somewhere from here
import pygame
pygame.init()
while True:
for event in pygame.event.get() :
if event.type == pygame.KEYDOWN :
if event.key == pygame.K_SPACE :
print ("Space bar pressed down.") #Here you should put you program in the mode associated with the pressed SPACE key
elif event.key == pygame.K_ESCAPE :
print ("Escape key pressed down.")
elif event.type == pygame.KEYUP :
if event.key == pygame.K_SPACE :
print ("Space bar released.")
elif event.key == pygame.K_ESCAPE :
print ("Escape key released.") #Time to change the mode again
nothing. absolutely nothing.