Source

Fat x Fast / FatxFast / game.py

Full commit
# This file is part of FatxFast.
#
#    FatxFast is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    FatxFast is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with FatxFast.  If not, see <http://www.gnu.org/licenses/>.

# Versioning based on: 
# http://en.wikipedia.org/wiki/Versioning#Designating_development_stage
__author__ = "dryatu (c) 2013"
__version__ = "1.2.5"

import pygame
from FatxFast.scenes import scenemanager
from FatxFast import (fpstimer, config, fileio, graphics)
from FatxFast.fileio import LOGGER, VersionError, check_version
from FatxFast.input import Input, SystemListener, KeyListener
from FatxFast.resource import resource_manager
from FatxFast.audio import Audio
from FatxFast.physics import physics
import threading


FPS = 100
FRAME_TIME = 1000/FPS
TITLE = "Fat x Fast - "+__version__

class Game(KeyListener, SystemListener):

    def __init__(self, **kwargs):
        super(Game, self).__init__()

        self.threads = []

        # Config loading & version checking
        conf = resource_manager.load("config")
        try:
            check_version(conf)
        except (AttributeError, VersionError):
            conf = config.Config()
        self.config = conf

        # Audio preinit
        audio_enabled = True
        try:
            pygame.mixer.init(44100, -16, 2, 1024)
        except pygame.error:
            LOGGER.log_message("Audio system init failed. Audio disabled", 
                True)
            audio_enabled = False

        if __debug__:
            # For debug reasons one can start a graphing system
            try: 
                import matplotlib.pyplot as plt
            except ImportError:
                LOGGER.log_message("Graphing system failed to start. "+
                    "No matplotlib.pyplot available")
            else:
                from FatxFast.utils.graphifier import Graphifier
                self.graphifier = Graphifier(
                    50, (-1*physics.Physics.maxspeed,
                        1*physics.Physics.maxspeed))

        pygame.init() 
        self.audio = Audio(audio_enabled, config=self.config)
        self.graphics = graphics.Graphics(config=self.config)
        self.graphics.set_title(TITLE)
        self.graphics.update()
        
        self.input = Input()
        self.input.add_system_listener(self)
        self.input.add_key_listener(self)
        self.savemanager = fileio.SaveManager(resource_manager)    
        self.hsmanager = fileio.HighScoreManager(resource_manager)
        
        self.fpstimer = fpstimer.FpsTimer()
        self.clock = pygame.time.Clock()

        self.scenemanager = scenemanager.SceneManager(self)
        self.running = True
        if kwargs:
            self.scenemanager.change_scene("gamescene", **kwargs)
  
    def create_thread(self, callback, args=(), daemon=True):
        self.threads.append(threading.Thread(target=callback, args=args))
        self.threads[-1].daemon = daemon
        return len(self.threads)-1

    def draw(self):
        self.scenemanager.draw(self.graphics.get_screen())
        self.graphics.update()

    def key_pressed(self, key, utf8, mod):
        if key == pygame.K_RETURN and mod & pygame.KMOD_LALT:
            self.graphics.toggle_fullscreen()

    def update(self):
        for thread in list(self.threads):
            if not thread.is_alive():
                self.threads.remove(thread)
        dt = self.clock.tick(FPS)
        self.audio.update(dt)
        self.input.update()
        self.scenemanager.update(dt)

    def run_thread(self, thread_index):
        self.threads[thread_index].start()
            
    def quit(self):
        self.scenemanager.change_scene("exitscene")