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

Improve the game framerate management #22

Open
naulan-chrzaszcz opened this issue Feb 1, 2023 · 1 comment
Open

Improve the game framerate management #22

naulan-chrzaszcz opened this issue Feb 1, 2023 · 1 comment
Labels
enhancement New feature or request high priority This issue is very important to resolve !

Comments

@naulan-chrzaszcz
Copy link
Member

Old code

class Fps(object):
    def __init__(self):
        self.prev_time = time.time()
        self.clock = Clock()
        self.font = Font()

        self.benchmark = True

        self.get()

        self.t = 0
        self.dt = self.manage(fps=120)

        fps = round(self.clock.get_fps())
        self.countFps = 1
        self.sumFps = 0
        self.avg = fps//self.countFps
        self.max = fps
        self.min = 1_000_000

    def manage(self, fps):
        """ Manage the delta time (Time between two frame) """
        self.clock.tick(fps)     # Cadence the clock of the game
        self.dt = float(time.time() - self.prev_time)     # The time between old frame and the new frame
        self.prev_time = time.time()     # Previous frame
        return self.dt * 60     # 60 is fps we need to have in game

    def average(self):
        """ 
           Make a average of fps getted
           @return average of fps, max fps, min fps
        """
        fps = round(self.clock.get_fps())
        self.sumFps += fps
        self.countFps += 1
        self.avg = self.sumFps//self.countFps

        if fps > self.max:
            self.max = fps
        if self.min > fps > 0:
            self.min = fps

        return self.avg,self.max,self.min

    def get(self):
        """ get the framerate in game """
        return round(self.clock.get_fps())

    def draw(self,surface):
        """ Display on a surface all informations about the framerate in game """
        msg = [self.font.font.render(f'{self.get()} Fps',0,(255,255,255)),self.font.font.render(f'avg:  {self.avg} Fps',0,(255,255,255)),self.font.font.render(f'max: {self.max} Fps',0,(255,255,255)),self.font.font.render(f'min: {self.min} Fps',0,(255,255,255)),self.font.font.render(f'var: {round(self.dt,3)} ms',0,(255,255,255))]
        self.font.outline(surface,msg[1],(5,5))
        self.font.outline(surface,msg[0],(5,msg[0].get_height() + 7))
        self.font.outline(surface,msg[2],(msg[0].get_width() * 2.5,5))
        self.font.outline(surface,msg[3],(msg[0].get_width() * 2.5,msg[2].get_height() * 1.5))
        self.font.outline(surface,msg[4],(5,msg[1].get_height() * 2.5))
        surface.blit(msg[1],(5,5))
        surface.blit(msg[0],(5,msg[0].get_height() + 7))
        surface.blit(msg[2],(msg[0].get_width() * 2.5,5))
        surface.blit(msg[3],(msg[0].get_width() * 2.5,msg[2].get_height() * 1.5))
        surface.blit(msg[4],(5,msg[1].get_height() * 2.5))

We need to improve the code of Fps class when #7 #19 is done

I suppose wrong things

  • The Fps class must be a Singleton. Why ? Because we need to have dt variable on many class like Entity object or Scene class
  • The code style not correspond with actual code
  • The draw function is maybe on a wrong place.
  • The average function return also a min value and max value (Weird)
  • Unknow self.t variable ?
@naulan-chrzaszcz naulan-chrzaszcz added enhancement New feature or request high priority This issue is very important to resolve ! labels Feb 1, 2023
@naulan-chrzaszcz
Copy link
Member Author

You understand what this class make @TheblackReaper060303 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request high priority This issue is very important to resolve !
Projects
None yet
Development

No branches or pull requests

1 participant