0

I recently started with pygame and im trying to create movement but when I move my rectangle the speed changes at various times. it becomes slower and faster at various times

import pygame

pygame.init() # inisializing pygame

WIN = pygame.display.set_mode((500, 500)) # the width and hieght of the window

pygame.display.set_caption('pygame tutorial') # give the window a title

x = 50
y = 50 # character x and y positions
width = 40
height = 60  # rectangle width and height
vel = 5 # velocity (speed)

run = True # boolean variable

while run: # forever loop
    for event in pygame.event.get(): # specifing the event
        if event.type == pygame.QUIT: # pygame.QUIT makes the red x work so you can close the window
            run = False  # run = false so the while loop stops
        key = pygame.key.get_pressed()
        if key [pygame.K_a]:
            x -= vel
        if key[pygame.K_d]:
            x += vel
        if key[pygame.K_w]:
            y -= vel
        if key[pygame.K_s]:
            y += vel
        WIN.fill((0,0,0))

    pygame.draw.rect(WIN, (255, 0, 0), (x, y, width, height))
    '''
    create a rectangle with 3 arguements, the window, the rgb colour, and the x,y positions width and height
    '''
    pygame.display.update()


pygame.quit()
humor 0150
  • 35
  • 5
  • you should use `clock.tick(FramesPerSecond)` to reduce speed to no more then `FramesPerSecond` – furas Feb 26 '22 at 00:27
  • you have wrong indentation. You runs `WIN.fill((0,0,0))` inside `for`-loop and it may run it more times when system sends more events - but you need it only once in `while run` - like `pygame.draw.rect(...)` and `pygame.display.update()` – furas Feb 26 '22 at 00:30
  • the same problem with `key` and `if` - they shouldn't be inside `for`-loop but after `for`-loop – furas Feb 26 '22 at 00:32

1 Answers1

0

You have wrong indentations so some code is executed inside many times in for-loop but it should be executed only once after for-loop

After changing indentations code work too fast and I needed to add clock.tick(60) to reduce speed to max 60 frames per second. This way it should run with the same speed on computers with older and newer CPU.

WIN = pygame.display.set_mode((500, 500)) # the width and hieght of the window

pygame.display.set_caption('pygame tutorial') # give the window a title

x = 50
y = 50 # character x and y positions
width = 40
height = 60  # rectangle width and height
vel = 5 # velocity (speed)

run = True # boolean variable

clock = pygame.time.Clock()

while run: # forever loop
    for event in pygame.event.get(): # specifying the event
        if event.type == pygame.QUIT: # pygame.QUIT makes the red x work so you can close the window
            run = False  # run = false so the while loop stops

    # --- after loop --
    
    key = pygame.key.get_pressed()
    if key [pygame.K_a]:
        x -= vel
    if key[pygame.K_d]:
        x += vel
    if key[pygame.K_w]:
        y -= vel
    if key[pygame.K_s]:
        y += vel
    WIN.fill((0,0,0))

    pygame.draw.rect(WIN, (255, 0, 0), (x, y, width, height))
    '''
    create a rectangle with 3 arguments, the window, the rgb colour, and the x,y positions width and height
    '''
    pygame.display.update()

    clock.tick(60)  # reduce speed to max 60 frames per seconds
    
pygame.quit()
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
furas
  • 134,197
  • 12
  • 106
  • 148