0

I did some research and found this: Setting a fixed FPS in Pygame, Python 3 and this: pygame clock.tick() vs framerate in game main loop. It is similar to what I am asking.

So the clock.tick(FPS) caps the program to run at that FPS. The reason you do this is so you can control the FPS of the program and it makes it easier for time stuff like waits.

But how can I unlimited FPS and still control my FPS for time stuff like waits? From my understanding, this is not possible, due to the fact that clock.tick(FPS) caps the FPS and while not adding it in means unlimited FPS but you not being able to control the FPS.

So it seems to be a question of weather to cap you FPS for control or have your program run as fast as possible, but without control.

In conclusion, what I am asking is four questions;

  1. Pros and cons of capping your FPS
  2. Pros and cons of not capping your FPS and going on with the natural FPS of your program
  3. Is it possible to have unlimited FPS and still control my FPS for time stuff like waits?
  4. If so, how?
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Wolfmann Games
  • 142
  • 1
  • 13
  • This question is sort of nonsensical, you're asking "How do I do X and not do X". Can you perhaps clarify what you "control my FPS for time stuff like waits" means? – Blckknght Jun 14 '21 at 21:28
  • Why do you want to run the program as fast as possible? Do you think you'll see and difference? Pygame is pixel-based. You're just wasting CPU time. – Rabbid76 Jun 14 '21 at 21:34
  • Why wouldn't you run the program as fast as possible? – Wolfmann Games Jun 14 '21 at 21:35
  • just wondering the pros and cons of each option – Wolfmann Games Jun 14 '21 at 21:36
  • There are no pros. You're just wasting CPU time and won't get any noticeable difference. The downside is that you don't have a constant frame rate and you have to calculate all animations and movements depending on the time that has passed in the last frame. – Rabbid76 Jun 14 '21 at 21:36
  • ok thanks! :) That is all I wanted to know! – Wolfmann Games Jun 14 '21 at 21:45

1 Answers1

3

You have to calculate the movement per frame depending on the frame rate.

pygame.time.Clock.tick returns the number of milliseconds since the last call. When you call it in the application loop, this is the number of milliseconds that have passed since the last frame. When you call it without a parameter (framerate=0), the FPS are unlimited.

Define the distance in pixels that the player should move per second (move_per_second). Then compute the distance per frame in the application loop:

move_per_second = 500 # 500 is just an example and means 500 pixels/second

run = True
clock = pygame.time.Clock()
while run:
    ms_frame = clock.tick() # get the milliseconds passed since the last frame
    
    move_per_frame = move_per_second * ms_frame / 1000  
    
    # [...]

However, with this approach, you are wasting CPU time and you won't see a noticeable difference. The downside is that you don't have a constant frame rate and you have to calculate all animations and movements depending on the time that has passed in the last frame.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174