0

I am using PyCharm Community Edition on ubuntu 22.04 LTS. PyGame 2.1.2. I am trying to learn pygame but its not sensing what particular keys are being pressed.

import pygame
#pygame initialisation
pygame.init()

#create screen
screen = pygame.display.set_mode((800,600))

#Title and Icon
pygame.display.set_caption("Space Invaders")
icon = pygame.image.load('ufo.png')
pygame.display.set_icon(icon)

#Player
playerImg = pygame.image.load('player.png')
playerX = 370
playerY = 480
def player(x,y):
    screen.blit(playerImg, (x,y))

#Game Loop
running = True
while running:

    screen.fill((0, 0, 0))
    delx = 0
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.KEYDOWN:
            print('pressed')
            if event.type == pygame.K_LEFT:
                print('left')
            if event.type == pygame.K_RIGHT:
                print('right')

        if event.type == pygame.KEYUP:
            if event.type == pygame.K_LEFT or event.type == pygame.K_RIGHT :
                print('released')

    playerX += delx
    player(playerX,playerY)

    pygame.display.update()

Whenever i press the right or left arrow key only the "pressed" message appears on the terminal but left and right keys dont.

Is there a problem with my code?

  • 1
    Answer: Yes. Meta answer: You're asking the wrong question, please start with the [tour] and read [ask]. Practical suggestion: Inspect the code for use of `event.type`. Also, learn how to use a debugger (try to find a video tutorial for your environment). – Ulrich Eckhardt Jan 18 '23 at 05:51

0 Answers0