0

Here is an example code:

import pygame
import sys

pygame.init()
screen = pygame.display.set_mode((1000, 500))

clock = pygame.time.Clock()

square = pygame.Surface((350, 350))
square.fill((0, 0, 0))
x_pos = 1000

while True:
    screen.fill((255, 255, 255))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    if x_pos <= -350:
        x_pos = 1000
    x_pos -= 5
    screen.blit(square, (x_pos, 50))

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

Questions:

1 - Does the .tick() method defines how many fps your game runs at? If not, what does it actually do?

2 - The higher the value passed in the .tick() method, the faster the square on the example goes to the left, why does that happen?

3 - Assuming that the .tick() method defines how many fps your game is going to run at, in some games like valorant, league of legends and any other game, when the fps is higher, the game only looks smoother, not faster like in the examples, is there any reason for that?

Thanks!

  • This happens because your "physics" is tied to the game-loop. You perform one "physics simulation step" for every frame you render. The physics and rendering should be decoupled - physics simulation steps should be tied to real-world time. – Paul M. Feb 09 '22 at 00:10

1 Answers1

0
  1. pygame.tick() is just fps. You can read more about it at https://www.pygame.org/docs/ref/time.html#pygame.time.Clock.tick

  2. You can think of FPS as how many times the while loops runs every second. If you run x_pos += 1 every frame, the higher the FPS, the more times that line of code will run; In return, the more times that line of code runs, the faster the square moves.

  3. The reason games look smoother at higher FPS is because of the refresh rate on your monitor (Hz). The refresh rate is literally how many times your monitor updates, meaning if your FPS is lower than your Hz, it will look clunky. Any FPS above your monitors refresh rate won't affect your experience in any way.

    The reason other games don't get faster with higher FPS is because of delta time, which takes the time elapsed between each frame and changes the move speed based on how many milliseconds passed. (Feel free to add more/correct me in comments)

    If you're curious about how to implement delta time in pygame, I recommend watching https://www.youtube.com/watch?v=XuyrHE6GIsc

Zan3y
  • 50
  • 6