one-file-projects/game.py

52 lines
1.2 KiB
Python

#!/usr/bin/env python
import pygame
from pygame.locals import *
import math
class Game:
def __init__(self,width, height,fps):
self._running = True
self._surface = None
self._clock = None
self._width = width
self._height = height
self._fps = fps
def on_init(self):
pygame.init()
self._surface = pygame.display.set_mode((self._width,self._height), pygame.HWSURFACE | pygame.DOUBLEBUF)
self._clock = pygame.time.Clock()
self._running = True
return True
def on_event(self,event):
if event.type == pygame.QUIT:
self._running = False
def on_update(self, dtime):
pass
def on_render(self, surface):
pass
def on_draw(self):
self.on_render(self._surface)
pygame.display.flip()
def on_cleanup(self):
pygame.quit()
def on_execute(self):
if not self.on_init():
self._running = False
while self._running:
self._clock.tick(self._fps)
for event in pygame.event.get():
self.on_event(event)
self.on_update(1.0 / self._fps)
self.on_draw()
self.on_cleanup()