0

The code below is generally used to make a rectangle and move it in the pygame window. But it isn't working. The rectangle shows but it shows no sign of movement in it. Please tell me what's wrong in this code.

import pygame
pygame.init()

win = pygame.display.set_mode((800,800))

pygame.display.set_caption("nyumph")

x = 50
y = 50
width = 40
height = 60
vel = 5

run = True
while run:
    pygame.time.delay(100)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()

    if keys[pygame.K_LEFT]:
        x -= vel
    if keys[pygame.K_RIGHT]:
        x += vel
    if keys[pygame.K_UP]:
        y -= vel
    if keys[pygame.K_DOWN]:
        y += vel

    win.fill((0, 0, 0))    

    pygame.draw.rect(win,(0, 0, 255), (x, y, width, height))  
    pygame.display.update()

pygame.quit()

This is the code I typed but only the rectangle appeared. It didn't move. Please tell me what's wrong in this.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • 2
    The code seems to work fine. Does your original code have to proper [Indentation](https://docs.python.org/3/reference/lexical_analysis.html). Note the movement has to be done in the application loop rather than the event loop. – Rabbid76 Jun 14 '20 at 14:25
  • Thanks Rabbid76. It just seems the "if" problem. The indentations were fine. Now when I press the keys the rectangle moves! Thanks again! –  Jun 14 '20 at 14:34

0 Answers0