1. schlangen
  2. gallox

Source

gallox / gallox / main.py

import pygame 

import sys
import os

pygame.mixer.pre_init(44100, -16, 2, 1024)
pygame.init()

sys.path.insert(0, os.path.abspath(os.path.join(__file__, "../..")))
import gallox
from gallox.ui.menu import Menu
from gallox.editor import Editor
from gallox.utils import Fader, load_sound
from gallox.world import World
from gallox.settings import *



class Main(object):
    def __init__(self):
        
        self.screen = pygame.display.set_mode((800,600), pygame.RESIZABLE)
        
        pygame.display.set_caption('Gallox - Gallaxy Logistics Expert')
        
        self.clock = pygame.time.Clock()
        
        self.menu = Menu(self.screen.get_size())
        self.menu.listen("exit", self.quit)
        self.menu.listen("start_game", self.start_game)
        self.menu.listen("start_editor", self.start_editor)
     
        
        self.active_object = None
                
        # "menu" or "game" or "editor" or "gamemenu"
        self.state = "menu"
#        self.fade_to(self.menu)
        self.start_game("testlevel")
#        self.start_editor()
        self.photo_sound = load_sound("photo.wav")
        self.c = 0
        
    def run(self):
        while self.state:
            self.c += 1
            if self.c == 100:
                self.c = 0
                pygame.display.set_caption("gallox with %.3f fps" % self.clock.get_fps())
            ticks = self.clock.tick(FPS)

            pygame.event.pump()
            events = pygame.event.get()
            
            
            events, drects = self.active_object.update(events)
            for event in events:
                if event.type == pygame.QUIT:
                    return
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        if self.state != "menu":
                            self.start_main_menu()
                        else:
                            self.quit()
                    elif event.key == pygame.K_F11:
                        pygame.image.save(self.screen, os.path.join(PROJECT_DIR, "screeny.png"))
                        self.photo_sound.play()
                if event.type == pygame.VIDEORESIZE:
                    pygame.display.set_mode(event.size, pygame.RESIZABLE)
                    drects = [pygame.Rect((0,0), event.size)]
            
            self.active_object.blit(self.screen)
            pygame.display.update(drects)
            
    def quit(self, **kw):
        self.state = "quit"
        self.active_object = Fader()
    
    def start_main_menu(self):
        self.state = "menu"
        self.fade_to(self.menu)
        
    def start_game(self, level_name, **kw):
        self.state = "game"
        self.fade_to(gallox.world.World(os.path.join(PROJECT_DIR, level_name),
                                        on_quit=self.start_main_menu))
        
    def start_editor(self, **kw):
        self.state = "editor"
        self.fade_to(Editor(self.screen.get_size(), 
                            quit_callback=self.start_main_menu))
        
    def fade_to(self, new_object, direct=False):
        if self.active_object and not direct:
            self.active_object = Fader(callback=self.fade_to,
                                       new_object=new_object, direct=True)
        if new_object:
            def set_new():
                self.active_object = new_object
            self.screen.fill((0,0,0))
            new_object.update([])
            new_object.blit(self.screen)
            self.active_object = Fader(callback=set_new, direction="in")
            
        
def run():
    Main().run()
    
if __name__ == "__main__":
    run()