import os, sys
import getopt
import pygame
from pygame.locals import *
images_dir = os.path.join( "..", "imagens" )
class Background:
"""
Esta classe representa o ator "Fundo" do jogo.
"""
image = None
def __init__( self ):
screen = pygame.display.get_surface()
back = pygame.Surface( screen.get_size() ).convert()
back.fill( ( 0, 0, 0 ) )
self.image = back
def update( self, dt ):
pass
def draw( self, screen ):
screen.blit( self.image, ( 0, 0 ) )
class Game:
screen = None
screen_size = None
run = True
background = None
def __init__( self, size, fullscreen ):
"""
Esta é a função que inicializa o pygame, define a resolução da tela,
caption, e disabilitamos o mouse dentro desta.
"""
actors = {}
pygame.init()
flags = DOUBLEBUF
if fullscreen:
flags |= FULLSCREEN
self.screen = pygame.display.set_mode( size, flags )
self.screen_size = self.screen.get_size()
pygame.mouse.set_visible( 0 )
pygame.display.set_caption( 'Título da Janela' )
def handle_events( self ):
"""
Trata o evento e toma a ação necessária.
"""
for event in pygame.event.get():
t = event.type
if t in ( KEYDOWN, KEYUP ):
k = event.key
if t == QUIT:
self.run = False
elif t == KEYDOWN:
if k == K_ESCAPE:
self.run = False
def actors_update( self, dt ):
self.background.update( dt )
def actors_draw( self ):
self.background.draw( self.screen )
def loop( self ):
"""
Laço principal
"""
self.background = Background()
clock = pygame.time.Clock()
dt = 16
while self.run:
clock.tick( 1000 / dt )
self.handle_events()
self.actors_update( dt )
self.actors_draw()
pygame.display.flip()
print "FPS: %0.2f" % clock.get_fps()
def usage():
"""
Imprime informações de uso deste programa.
"""
prog = sys.argv[ 0 ]
print "Usage:"
print "\t%s [-f|--fullscreen] [-r <XxY>|--resolution=<XxY>]" % prog
print
def parse_opts( argv ):
"""
Pega as informações da linha de comando e retorna
"""
try:
opts, args = getopt.gnu_getopt( argv[ 1 : ],
"hfr:",
[ "help",
"fullscreen",
"resolution=" ] )
except getopt.GetoptError:
usage()
sys.exit( 2 )
options = {
"fullscreen": False,
"resolution": ( 640, 480 ),
}
for o, a in opts:
if o in ( "-f", "--fullscreen" ):
options[ "fullscreen" ] = True
elif o in ( "-h", "--help" ):
usage()
sys.exit( 0 )
elif o in ( "-r", "--resolution" ):
a = a.lower()
r = a.split( "x" )
if len( r ) == 2:
options[ "resolution" ] = r
continue
r = a.split( "," )
if len( r ) == 2:
options[ "resolution" ] = r
continue
r = a.split( ":" )
if len( r ) == 2:
options[ "resolution" ] = r
continue
r = options[ "resolution" ]
options[ "resolution" ] = [ int( r[ 0 ] ), int( r[ 1 ] ) ]
return options
def main( argv ):
fullpath = os.path.abspath( argv[ 0 ] )
dir = os.path.dirname( fullpath )
os.chdir( dir )
options = parse_opts( argv )
game = Game( options[ "resolution" ], options[ "fullscreen" ] )
game.loop()
if __name__ == '__main__':
main( sys.argv )