tygzy
tygzy

Reputation: 716

Pygame KEYDOWN not being recognised

I am making a game in python pygame and I am on setting up the keyinput and have came across a part where I don't know what to do at all.

All of the variables are defined as there are no errors, but no key detections are being made at all so I don't know what to do.

I have had a look at other questions and tried there answers but haven't solved the problem yet

class Player(object):


    def __init__(self, x, y, velocity):
        self.x = x
        self.y = y

        self.width = 32
        self.height = 32

        self.velocity = velocity
        self.render()
        self.tick()

    def movement(self):
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                print("done")
                if event.key == pygame.K_w:
                    self.y -= self.velocity
                elif event.key == pygame.K_s:
                    self.y += self.velocity
                elif event.key == pygame.K_d:
                    self.x += self.velocity
                elif event.key == pygame.K_a:
                    self.x -= self.velocity
                elif event.key == pygame.K_ESCAPE:
                    pygame.quit()
            elif event.type == pygame.QUIT:
                pygame.quit()

    def tick(self):
        self.movement()

    def render(self):
        pygame.draw.rect(window.WINDOW, (255, 0, 0), (self.x, self.y, 
        self.width, self.height))
        pygame.display.update()

There are no errors but when it is supposed to print out "done" it doesn't so I think it has something to do with the KEYDOWN at the start or before that.

Upvotes: 1

Views: 799

Answers (1)

Tankiso Thebe
Tankiso Thebe

Reputation: 170

Okay your code isn't complete, but I can see you're trying to move an image or object up,down,left,right on the pygame display. I recreated your code from what you gave here, and everything seems to be running fine I'm not sure about K_w,K_s,K_d,K_a keys though since I don't have the full code and you are concerned with it not printing 'done'. NOTE that I changed them to print something out instead of actually moving an object. the only errors I found is with your exit pygame.K_ESCAPE and pygame.QUIT events which is:

Traceback (most recent call last): File "main.py", line 13, in for event in pygame.event.get(): pygame.error: video system not initialized

Solve this using pygame.quit() together with sys.exit(), first import sys on top of your code.

this how it looks and It prints out done on KEYDOWN:

<code>
from pygame.locals import *
import pygame
import sys

pygame.init()

screen = pygame.display.set_mode((200,200))




while 1:
    for event in pygame.event.get():
        print(event) # logs every event happening on the pygame display

        if event.type == pygame.KEYDOWN:

            print("done")
            if event.key == pygame.K_w:
                print("self.y -= self.velocity") # not doesn't decrement it prints for testing 
            elif event.key == pygame.K_s:
                print("self.y += self.velocity") # not doesn't increment it prints for testing
            elif event.key == pygame.K_d:
                print("self.x += self.velocity") # not doesn't increment it prints for testing
            elif event.key == pygame.K_a:
                print("self.x -= self.velocity") # not doesn't decrement it prints for testing
            elif event.key == pygame.K_ESCAPE:
                pygame.quit()
                sys.exit() # add this
            elif event.type == QUIT:
                pygame.quit()
                sys.exit() # add this
</code>

Upvotes: 1

Related Questions