-1

how do i slow this down? right now it instantly goes down/left/up/right. I know it has to do with the time module but i have no idea how to implement it. Basically I want to SLOW down the process from 10 y to 490 y. Not add a delay but slow down the process like making it go in slow motion. I even tried the pygame clock with 120fps but it did not work

import time
import pygame
from pygame.locals import *

clock = pygame.time.Clock()
fps = 120

pygame.init()
surface = pygame.display.set_mode((600, 600))
background = pygame.image.load('back.png')
surface.blit(background, (0, 0))
block = pygame.image.load('block.png').convert()
block_y = 0
block_x = 0
surface.blit(block, (block_x, block_y))

def draw():
    surface.blit(background, (0, 0))
    surface.blit(block, (block_x, block_y))
    pygame.display.flip()

pygame.display.flip()
running = True
while running:
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                running = False
            if event.key == K_UP:
                block_y -= 10
                draw()
            if event.key == K_DOWN:
                block_y += 10
                draw()
            if event.key == K_LEFT:
                block_x -= 10
                draw()
            if event.key == K_RIGHT:
                block_x += 10
                draw()
        elif event.type == QUIT:
            running = False
        elif event.type == KEYUP:
            clock.tick(fps)
            if event.key == K_UP:
                block_y -= 10
                draw()
            if event.key == K_DOWN:
                block_y += 490
                draw()
            if event.key == K_LEFT:
                block_x -= 100
                draw()
            if event.key == K_RIGHT:
                block_x += 510
                draw()
    ```
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • 1
    The position [change] is 10 pixels per frame, not 10 pixels per second. [Consider using clock.tick(fps) in here, https://stackoverflow.com/questions/10942011/speed-of-an-object-in-pygame/12231013]. – FaranAiki Jul 02 '21 at 04:44
  • Does this answer your question? [Speed of an object in pygame?](https://stackoverflow.com/questions/10942011/speed-of-an-object-in-pygame) – FaranAiki Jul 02 '21 at 04:47

1 Answers1

0

This is probably not a great solution as it is not frame rate independent (see FaranAiki’s comment) but here you go (change speed to, uh, change speed. You may also want to decrease your frame rate):

import time
import pygame
from pygame.locals import *

clock = pygame.time.Clock()
fps = 120
speed=1
pygame.init()
surface = pygame.display.set_mode((600, 600))
background = pygame.image.load('back.png')
surface.blit(background, (0, 0))
block = pygame.image.load('block.png').convert()
block_y = 0
block_x = 0
surface.blit(block, (block_x, block_y))

def draw():
    surface.blit(background, (0, 0))
    surface.blit(block, (block_x, block_y))
    pygame.display.flip()

pygame.display.flip()
running = True
while running:
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                running = False
            if event.key == K_UP:
                block_y -= speed
                draw()
            if event.key == K_DOWN:
                block_y += speed
                draw()
            if event.key == K_LEFT:
                block_x -= speed
                draw()
            if event.key == K_RIGHT:
                block_x += speed
                draw()
        elif event.type == QUIT:
            running = False
        elif event.type == KEYUP:
            clock.tick(fps)
            if event.key == K_UP:
                block_y -= 10
                draw()
            if event.key == K_DOWN:
                block_y += 490
                draw()
            if event.key == K_LEFT:
                block_x -= 100
                draw()
            if event.key == K_RIGHT:
                block_x += 510
                draw()
Ezra
  • 471
  • 3
  • 14