0
votes

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.

1
There isn't enough code in your question to allow anyone to answer. What you have basically look OK, so the problem must be elsewhere. Please edit your question and add enough code to show where this for is being executed.martineau
@martineau this is part of a file which doesn't have the main loop or window creation as it is part of a player file, I only included this because it seemed like from my code for this file it was all that was neededtygzy
Tyler: I meant that since your code superficially seems like it would work, the problem is likely in when/where/how it's being executed, which can't be determined by what's in your question.martineau
@martineau I have now added all the codetygzy
Tyler: That's better...but not sufficient. Is the Player.movement() method being called from a main event processing loop somewhere, as pygame apps typically have?martineau

1 Answers

1
votes

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>