0

I'm kinda new to Pygame and I'm looking to start a new small project, I've only really done one before but I'm stuck on it. There's a small rectangle at the bottom of the screen and the idea is to have it move. I don't have any syntax errors showing but nothing is happening. the code is below:

import pygame

pygame.init()
x = 190
vel = 5
screen = pygame.display.set_mode((400, 400))
while True:

    
    
    keys = pygame.key.get_pressed()
      
    if keys[pygame.K_RIGHT]:
        x -= vel
          
    if keys[pygame.K_RIGHT]:
        x += vel
        
    bg = (50,50,50)
    white = (255,255,255)
    screen.fill(bg)
    pygame.draw.rect(screen, white, pygame.Rect(x, 390, 20, 10))
    pygame.display.update()

When I run the code it should have it so that the rectangle moves left and right in increments of 5 pixels, but nothing happens.

Liam
  • 6,009
  • 4
  • 39
  • 53
  • You use `keys[pygame.K_RIGHT]` twice. Use `keys[pygame.K_LEFT]` for the left direction and `keys[pygame.K_RIGHT]` for the right direction. Also see [How can I make a sprite move when key is held down](https://stackoverflow.com/questions/9961563/how-can-i-make-a-sprite-move-when-key-is-held-down). – Rabbid76 Jan 08 '23 at 16:33
  • The problem is that you don't process pygame's event queue. You should simply call `pygame.event.pump()` after processing the movement of the player. reference- https://stackoverflow.com/questions/17938170/pygame-key-get-pressed-is-not-working – Yoel Nisanov Jan 08 '23 at 16:42

0 Answers0