Skip to main content
added 3 characters in body
Source Link

I'm not sure if there's a recommended way, as such, for writing components but I often reuse buttons and other stuff for the game and pause menu which are initialized with Surfaces for mouse-states (hover, mouse-down, etc) and a function for click-events.

And of course, I have a vector class that I reuse all the time.

You definitely don't really need a module for basic timing and fps calculation though. Pygame's Clock-class is ideal for that. You just create a clock-object, call tick() once per frame and it returns the delta-time and keeps track of fps for you:

def main_loop():
    clock = pygame.time.Clock()     
    counter = 0

    while game_is_running:          
        handle_events()

        lapse = clock.tick()        
        update_physics_and_stuff(lapse * BASE_SPEED_FACTOR)
        render()
        
        # once every second, display the fps
        counter += lapse           
        if counter > 1000:
            pygame.display.set_caption('fps: %s'%1.2f' % clock.get_fps())
            counter = 0

I'm not sure if there's a recommended way, as such, for writing components but I often reuse buttons and other stuff for the game and pause menu which are initialized with Surfaces for mouse-states (hover, mouse-down, etc) and a function for click-events.

And of course, I have a vector class that I reuse all the time.

You definitely don't really need a module for basic timing and fps calculation though. Pygame's Clock-class is ideal for that. You just create a clock-object, call tick() once per frame and it returns the delta-time and keeps track of fps for you:

def main_loop():
    clock = pygame.time.Clock()     
    counter = 0

    while game_is_running:          
        handle_events()

        lapse = clock.tick()        
        update_physics_and_stuff(lapse * BASE_SPEED_FACTOR)
        render()
        
        # once every second, display the fps
        counter += lapse           
        if counter > 1000:
            pygame.display.set_caption('fps: %s' % clock.get_fps())
            counter = 0

I'm not sure if there's a recommended way, as such, for writing components but I often reuse buttons and other stuff for the game and pause menu which are initialized with Surfaces for mouse-states (hover, mouse-down, etc) and a function for click-events.

And of course, I have a vector class that I reuse all the time.

You definitely don't really need a module for basic timing and fps calculation though. Pygame's Clock-class is ideal for that. You just create a clock-object, call tick() once per frame and it returns the delta-time and keeps track of fps for you:

def main_loop():
    clock = pygame.time.Clock()     
    counter = 0

    while game_is_running:          
        handle_events()

        lapse = clock.tick()        
        update_physics_and_stuff(lapse * BASE_SPEED_FACTOR)
        render()
        
        # once every second, display the fps
        counter += lapse           
        if counter > 1000:
            pygame.display.set_caption('fps: %1.2f' % clock.get_fps())
            counter = 0
Source Link

I'm not sure if there's a recommended way, as such, for writing components but I often reuse buttons and other stuff for the game and pause menu which are initialized with Surfaces for mouse-states (hover, mouse-down, etc) and a function for click-events.

And of course, I have a vector class that I reuse all the time.

You definitely don't really need a module for basic timing and fps calculation though. Pygame's Clock-class is ideal for that. You just create a clock-object, call tick() once per frame and it returns the delta-time and keeps track of fps for you:

def main_loop():
    clock = pygame.time.Clock()     
    counter = 0

    while game_is_running:          
        handle_events()

        lapse = clock.tick()        
        update_physics_and_stuff(lapse * BASE_SPEED_FACTOR)
        render()
        
        # once every second, display the fps
        counter += lapse           
        if counter > 1000:
            pygame.display.set_caption('fps: %s' % clock.get_fps())
            counter = 0