Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Delta Time Instead of Approximate Time Per Frame #18

Open
dreweso opened this issue Nov 24, 2023 · 2 comments
Open

Use Delta Time Instead of Approximate Time Per Frame #18

dreweso opened this issue Nov 24, 2023 · 2 comments
Labels
asteroids For issues related to the asteroids game. enhancement New feature or request help wanted Extra attention is needed

Comments

@dreweso
Copy link
Contributor

dreweso commented Nov 24, 2023

Currently the game uses approximate time per frame as a simple way to make it work at multiple frame rates.
A better practice is to use the actual time between frames, which is delta time (change in time).
I already implemented dt in the Game class, it's just not used.

I think this is what needs to be done:

  • Remove APPROX_TIME_PER_FRAME and multiply constants by dt instead. For example do ship.slow_down(arg * self.dt)
  • Use it everywhere in the game (ship, projectiles, asteroids)

At the moment if the game is running at a lower frame rate than it's supposed to, the change in time will be greater than the approximate predicted change in time defined in the constant APPROX_TIME_PER_FRAME. This should result in the ship going slower than it's supposed to.

@jaredscarr jaredscarr added enhancement New feature or request help wanted Extra attention is needed asteroids For issues related to the asteroids game. labels Nov 24, 2023
@jaredscarr
Copy link
Collaborator

This is for asteroids right? Do you think that it would be a good time to add a config file for global settings? or is this a utility class where the frame rate is calculated each time? Or something else?

@dreweso
Copy link
Contributor Author

dreweso commented Nov 24, 2023

Yes, I forgot to say this is for asteroids. A config file could be convenient for tuning the game. Currently there are some constants at the top of the program, but not everything can be controlled, so more could be added. I don't think a utility class needs to be added. Delta time is already implemented in Game.__init__ in main.py, just not used:

def __init__(self, title: str = "NSCCSC Asteroid Clone") -> None:
    ...
    self.dt = 0  # delta time
    self.clock = pygame.time.Clock()
    ...

In Game.run:

def run(self) -> None:
    ...
    while True:
        ...
        # use delta time, which is the amount of time that has passed between each frame.
        # this allows behaviour like velocity to be based on actual time passed instead of the amount of frames.
        seconds_per_millisecond = 1000
        self.dt = (
            self.clock.tick(FRAME_RATE) / seconds_per_millisecond
        )  # NOTE: not currently used

Here's an example of how it could be used in Game.handle_input:

def handle_input(self):
    ...
    if keys[pygame.K_w]:
        # multiply by dt to make the increase based on the amount of time that has passed between frames
        self.ship.add_forward_velocity(VELOCITY_INCREASE_ON_KEYPRESS * self.dt)
    ...

@dreweso dreweso changed the title Use Delta Time Instead of Approximate Timer Per Frame Use Delta Time Instead of Approximate Time Per Frame Dec 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
asteroids For issues related to the asteroids game. enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants