Python Forum
Why the heck is this not working - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Game Development (https://python-forum.io/forum-11.html)
+--- Thread: Why the heck is this not working (/thread-16153.html)



Why the heck is this not working - YTPizzer - Feb-16-2019

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


RE: Why the heck is this not working - Windspar - Feb-16-2019

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")