user1688175
user1688175

Reputation:

pygame - Key pressed

I am kinda stuck with a supposed to be simple code to check if the user has pressed "w" or "s".

Below you can see my code:

import pygame
pygame.init()

while True:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN and event.key == pygame.K_w:
            print('Forward')
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_s:
            print('Backward')  

Am I forgetting something here?

Thanks!

Upvotes: 2

Views: 28997

Answers (6)

Bli231957
Bli231957

Reputation: 1

This might work:

from pygame import *

init()
if key.get_pressed()[K_w] == True:
    print('Forwards')
if key.get_pressed()[K_s] == True:
    print('Backwards')`

Upvotes: 0

user7090116
user7090116

Reputation:

Sample code to understand how key press events work.


import pygame
from pygame.locals import *

def main():
    pygame.init()
    pygame.display.set_caption("Move Box With Arrow Keys")
    wndsize = (320, 240)
    display = pygame.display.set_mode(wndsize)

    x = 5
    y = 5

    black = (0, 0, 0)
    white = (255, 255, 255)

    rectpos = (x, y)
    rectdim = (50, 50)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RIGHT:
                    x += 5
                    print("K_RIGHT")
                if event.key == pygame.K_LEFT:
                    x -= 5
                    print("K_LEFT")
                if event.key == pygame.K_UP:
                    y -= 5
                    print("K_UP")
                if event.key == pygame.K_DOWN:
                    y += 5
                    print("K_DOWN")
                
                # Don't allow coords below 0
                if 0 > x:
                    x = 5
                if 0 > y:
                    y = 5
                
                # Don't allow overflow
                if x + rectdim[0] > wndsize[0]:
                    x = wndsize[0] - rectdim[0] - 5
                
                if y + rectdim[1] > wndsize[1]:
                    y = wndsize[1] - rectdim[1] - 5
                
                rectpos = (x, y)
    
        display.fill(white)
        rect = pygame.draw.rect(display, black, ((rectpos), (rectdim)))
        pygame.display.update()

main()

Upvotes: 0

eli chan
eli chan

Reputation: 21

it is no good to ask the gamer to keep hitting w to get the response. If you want to read the "pressed" state. You could consider the followings:

  from pygame import *
    import time
    flag = False # The flag is essential.
    DONE = False
    screen = display.set_mode((500,500))   # 1180, 216
    count=0
    while not DONE:
        event.pump() # process event queue
        keys = key.get_pressed() # It gets the states of all keyboard keys.
        #print("%d"%count,keys)
        count+=1
        if keys[ord('w')]: # And if the key is K_DOWN:
            print("%d w down"%count)
        if keys[ord('s')]: # And if the key is K_DOWN:
            print("%d s down"%count)
        time.sleep(0.1)

Upvotes: 1

Pyboss
Pyboss

Reputation: 261

This is what seems to me the most simple and understandable way to do it:

import pygame
pygame.init()
pygame.display.set_mode((300, 300))
running = True

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            pygame.quit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_w:
                print('Forward')
            elif event.key == pygame.K_s:
                print('Backward')

Instead of using the sys.exit() method I prefer to just use pygame.quit()

Upvotes: 1

ryanpattison
ryanpattison

Reputation: 6251

A window needs to be created to receive key presses, the following works.

import pygame
import sys
pygame.init()

pygame.display.set_mode((100, 100))

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_w:
                print('Forward')
            elif event.key == pygame.K_s:
                print('Backward')

Upvotes: 3

user4408809
user4408809

Reputation:

Try this:

import pygame
pygame.init()
key = pygame.key.get_pressed()
while True:
    for event in pygame.event.get():
        if event.type == KEYDOWN and event.key == pygame.K_w:
            print('Forward')
        elif event.type == KEYDOWN and event.key == pygame.K_s:
            print('Backward')  

Upvotes: 0

Related Questions