Python Forum

Full Version: Why the heck is this not working
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey, so I'm new here and don't plan to use this website again.
The only reason I'm here is because I've spent ages trying to get this code to work and it won't
I think there's something wrong with my computer.

Pygame doesn't seem to register when I do things.
I've altered this code like 99999 times and it hasn't shown a sign of working once. It just lets me type and nothing else happens. Here's what it is at the moment:
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")
please help asap
1. pygame.key.get_pressed() get all key states. Either pygame.event.get() or pygame.event.pump() must be run before call this.

2. Your using key states in event loop.
while True:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_w:
                print("w is pressed")
            elif event.key == pygame.K_s:
                print("s is pressed")
3. Using key states.
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pass

    pressed = pygame.key.get_pressed()
    if pressed[pygame.K_w]:
        print("w is pressed")