0

I am trying to make a game in python with pygame and I can pretty much do it exept when I move the image using the arrow keys it sometimes becomes jerky and it jumps pixels instead of moving one pixel at a time.

I would really appreciate it if someone could help me.

Here is my code.

import pygame, sys

clock = pygame.time.Clock()

#Variables
isPlaying = True

windowSize = [600, 400]

playerLocation = [50, 50]

movingUp = False
movingDown = False

#Window
window = pygame.display.set_mode(windowSize)
windowCaption = pygame.display.set_caption('Game')

display = pygame.Surface((300, 200))

#Images
playerImage = pygame.image.load('Data/Images/Player.png')

#Rects
playerRect = pygame.Rect(50, 50, playerImage.get_width(), playerImage.get_height())

#MainLoop
while isPlaying == True:
    display.fill((255, 255, 255))

    display.blit(playerImage, playerLocation)

    if movingUp == True:
        playerLocation[1] -= 4
    if movingDown == True:
        playerLocation[1] += 4

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                movingUp = True
            if event.key == pygame.K_DOWN:
                movingDown = True
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_UP:
                movingUp = False
            if event.key == pygame.K_DOWN:
                movingDown = False

    surf = pygame.transform.scale(display, windowSize)
    window.blit(surf, (0, 0))

    pygame.display.update()
    clock.tick(60)

By the way I'm on raspberry pi.

2 Answers2

0

You could try setting it to only move a single pixel at a time. (It looks like you have it set to 4.)

Fiddling with the clock.tick is also an option. Your pi might not be able to run 60 FPS, so lowering it could help.

Grant Allan
  • 189
  • 1
  • 4
  • 10
0

You should look into using speed*time for moving images on a screen for smoother transitions. Your speed of 4 will translate 4 + or - regardless of how many repaints occurred on the screen like Grant is saying. Lowering the FPS will probably appear smooth but your object moving around is ignoring how much time has actually passed since the last loop so it will always be jerky.

Getting the DeltaTime or the time of the current frame minus the time since the last frame then multiplying that by a vector is what you want to do. In this case you're only moving up and down so a single number will be fine.

The math would be like this in milliseconds. (Current loop time = 2000 - Last loop time = 1000) / One second 1000 = Percentage of the time that's passed in a second * your speed. In this case your speed is 4 so you'll move a full 4 pixels between refreshes. Hopefully your frame rate isn't that bad but if there is twice as much time between frames it should move your object 8 pixels.

I don't do much with games but, I think that's how it usually works.

Byrd
  • 325
  • 1
  • 8