HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

A way of keeping a constant game speed at any fps with Pygame

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
keepingconstantwithanywaygamefpspygamespeed

Problem

Normally the game speed with pygame is locked with the fps, so I attempted making a simple two classes that'd be super easy to use, to allow for the same game speed no matter what the fps is.

For each frame, it calculates the difference between the amount of ticks that have passed, and how many should have passed (I get that this way could be better, but people don't change this mid game right?). For example, the difference at 60fps at 60 ticks per second is 1, at 30fps, the difference becomes 2, at 120fps, one frame will have a difference of 0, and the next 1. I did my own version of ticks since I locked the actual ticks to the fps.

With the frames per second, I set it to calculate it every x seconds, so it's a bit more accurate and avoids times so small it causes zero division errors (0.1 seconds for 1753 frames is more accurate that 0.00005704506 seconds for 1 frame). If you don't limit the fps, the inbuilt fps function just returns 0.

There does seem to be a bit of a problem though, and I'm not sure what's causing it. I'm sure I've done the framerate calculation correctly (with a minimum time so it doesn't end up working with tiny floats), but setting the fps to 500 reads as 333, and 1000 reads as 500, which is really strange. Setting it to something really high or quite low returns something that looks a lot more correct.

I've not really used a with with a class containing another class before, so I'm not sure if I've done it right (I originally only had the one class, but realised __enter__ and __exit__ needed the class called each time), so any feedback would be good as to what I didn't do well.

```
from __future__ import division
import pygame
import time
import random

class GameTime(object):
def __init__(self, desired_fps, desired_ticks, clock):
self.start_time = time.time()
self.desired_fps = desired_fps
self.desired_ticks = desired_ticks
self.clock = clock

self.ticks = 0

self.framerate_cou

Solution

It'd be easy to add default values to desired_fps and desired_ticks rather than requiring they be manually supplied.

get_ticks is a bit of a misleading name. It implies a getter, but actually you're modifying an attribute as well as returning a value. I'd personally call it calculate_ticks so that it's less surprising to see both results happening. You should definitely write an explanation of what it does in the docstring though. You explained the function but didn't actually tell the user what it gives them or what effect it has.

def calculate_ticks(self, current_time):
    """Returns ticks from this frame and updates self.ticks

    Ensure the correct number of ticks have passed since the 
    start of the game.
    This doesn't use the inbuilt pygame ticks.
    """


And I'd do similar with your FPS calculation.

Code Snippets

def calculate_ticks(self, current_time):
    """Returns ticks from this frame and updates self.ticks

    Ensure the correct number of ticks have passed since the 
    start of the game.
    This doesn't use the inbuilt pygame ticks.
    """

Context

StackExchange Code Review Q#112357, answer score: 2

Revisions (0)

No revisions yet.