Python Forum
Why the heck is this not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why the heck is this not working
#1
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
Reply
#2
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")
    
99 percent of computer problems exists between chair and keyboard.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020