Snippets

Peter Finlayson pygame basic loop

Created by Peter Finlayson
from __future__ import division
from __future__ import print_function

import sys

import pygame

APP_NAME = sys.argv[0]
SCREEN_SIZE = (1280, 720)
MAX_FPS = 60


def main():
    pygame.init()
    screen = pygame.display.set_mode(SCREEN_SIZE)
    clock = pygame.time.Clock()
    
    running = True
    
    # set up game here
    
    
    # main loop
    while running:
        # game logic
        
        # event loop
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key in (pygame.K_ESCAPE,):
                    # default handler just exits on Esc
                    running = False
                else:
                    # unhandled event
                    print(event)
            elif event.type == pygame.QUIT:
                running = False
    
        # finish frame
        pygame.display.flip()
        frame_time = clock.tick(MAX_FPS)
        pygame.display.set_caption("%s - %d FPS" % (APP_NAME, int(1000 / frame_time)))

        
if __name__ == "__main__":
    try:
        main()
    finally:
        pygame.quit()

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.