I made a game and used pygame module as the keyboard input. however, it does not seem to respond. there is no error, but my keypress does nothing when I run the code.
I tried using pygame.key but it does not work.
I have no idea what is wrong with this code.
import pygame
pygame.init()
class keypress():
def check(self):
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.KEYDOWN:
print("Hey, you pressed the key, '0'!")
if event.key == pygame.K_1:
print("Doing whatever")
else:
pass
a = keypress()
if __name__ == "__main__":
while True:
a.check()
When I press keys, it does basically nothing.
Pygame will not work without a screen/window defined.
With a window opened, and a couple of minor bugs in the input-handling fixed ~
pygame.K_0
, not KEYDOWN
againmain
loop.It works mostly how the code seems to describe:
import pygame
pygame.init()
WINDOW_WIDTH = 400
WINDOW_HEIGHT= 400
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
class keypress():
def check(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
# TODO - handle exiting
pass
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_0:
print("Hey, you pressed the key, '0'!")
elif event.key == pygame.K_1:
print("Doing whatever")
a = keypress()
if __name__ == "__main__":
while True:
a.check()