Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/Saludame.activity/game.py
blob: bbf054896de5d5d9fa8f5e4d2f94174ac5a07754 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# -*- coding: utf-8 -*-

from game_manager import GameManager
import pygame
import logging

if __name__ == "__main__":
    import gettext
    gettext.textdomain("org.ceibaljam.Saludame")
    gettext.bindtextdomain("org.ceibaljam.Saludame", "locale/")
    
    gettextold = gettext.gettext
    def _(string):
        string = gettextold(string)
        if isinstance(string, unicode):
            return string.upper()
        else:
            return unicode(string.decode("utf-8")).upper()
    gettext.gettext = _

from gettext import gettext as _
import os

log = logging.getLogger('saludame')
log.setLevel(logging.DEBUG)

# Variables globales
MAX_FPS = 16            # Max frames per second
SLEEP_TIMEOUT = 30      # Seconds until the PauseScreen if no events show up
pause = False
running = True

main_class = None

set_library_function = None

class Main():
    def __init__(self):
        self.windows_controller = None
        global main_class
        main_class = self
    
    def main(self, from_sugar):
        """Main function of the game.
        
        This function initializes the game and enters the PyGame main loop.
        """
        global running, pauses
        
        if from_sugar:
                import gtk

        import app_init
        import challenges_creator
        import customization
        import sound_manager
        import saludame_windows_controller
        
        # Optimizes sound quality and buffer for quick loading
        pygame.mixer.quit()     # When executting from sugar pygame it's already initialized
        pygame.mixer.pre_init(22050, -16, 1, 512)
        
        pygame.mixer.music.set_endevent(pygame.USEREVENT)
        
        # Inits PyGame module
        pygame.init()
        
        if not from_sugar:
            target_size = (1200, 800)#(1000, 650)   # In regular computers the native resolution is too high (5/6)
            screen = pygame.display.set_mode(target_size)
        
        screen = pygame.display.get_surface()
        assert screen, "No screen"
        
        pygame.display.update()
        
        # This clock is used to keep the game at the desired FPS.
        clock = pygame.time.Clock()
        
        # windows_controller asociado al screen
        self.windows_controller = saludame_windows_controller.SaludameWindowsController(screen, None)
        
        # Initialize sound_manager, game_manager, character, actions and menu.
        sound_manager.SoundManager()
        
        app_loader = app_init.AppLoader(self.windows_controller)
        bars_loader = app_loader.get_status_bars_loader()
        game_man = app_loader.get_game_manager()
        
        self.windows_controller.game_man = game_man
        self.windows_controller.create_windows_and_activate_main(app_loader, clock, bars_loader)

        game_man.load_game()
          
        frames = 0
        
        # Main loop
        update = True # The first time the screen need to be updated
        
        while running:
            
            if from_sugar:
                # Pump GTK messages.
                while gtk.events_pending():
                    gtk.main_iteration()
    
            if not pause:
                # Waits for events, if none the game pauses:
                # http://wiki.laptop.org/go/Game_development_HOWTO#Reducing_CPU_Load
                milliseconds = clock.tick(MAX_FPS) # waits if the game is running faster than MAX_FPS
                
                events = pygame.event.get()
                
                if events:
                    for event in events:
                        if event.type == pygame.QUIT:
                            running = False
                            game_man.save_game()
                        elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE and not from_sugar:
                            running = False
                            game_man.save_game()
                        elif event.type == pygame.MOUSEBUTTONDOWN:
                            self.windows_controller.handle_mouse_down(pygame.mouse.get_pos())
                        elif event.type == pygame.MOUSEBUTTONUP:
                            self.windows_controller.handle_mouse_up(event.pos)
                        elif event.type == pygame.MOUSEMOTION:
                            self.windows_controller.handle_mouse_motion(event.pos)
                        elif event.type == pygame.VIDEOEXPOSE:
                            self.windows_controller.reload_main = True
                        elif event.type == pygame.USEREVENT and event.code == 0: # Music ended
                            sound_manager.instance.start_playing()
                            
                self.windows_controller.handle_mouse_over(pygame.mouse.get_pos())
                
                self.windows_controller.update(frames)
        
                frames += 1

                game_man.signal()
                
        # Una vez que sale del loop manda la senal de quit para que cierre la ventana
        pygame.quit()

if __name__ == "__main__":
    Main().main(False)