Back to index

freevial  1.2
freevial.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 
00003 #
00004 # Freevial
00005 #
00006 # Copyright (C) 2007, 2008 The Freevial Team
00007 #
00008 # By Carles Oriol i Margarit <carles@kumbaworld.com>
00009 # By Siegfried-Angel Gevatter Pujals <siggi.gevatter@gmail.com>
00010 # By Nil Oriol <nil@kumbaworld.com>
00011 #
00012 # This program is free software: you can redistribute it and/or modify
00013 # it under the terms of the GNU General Public License as published by
00014 # the Free Software Foundation, either version 3 of the License, or
00015 # (at your option) any later version.
00016 #
00017 # This program is distributed in the hope that it will be useful,
00018 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 # GNU General Public License for more details.
00021 #
00022 # You should have received a copy of the GNU General Public License
00023 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
00024 #
00025 
00026 import sys
00027 import os
00028 import pygame
00029 import gettext
00030 from math import *
00031 
00032 from skinner import setSkinName, Skin
00033 from common.globals import GlobalVar, Global
00034 
00035 # This is provisionally here for technical reasons...
00036 if '--database' in sys.argv:
00037        path = os.path.abspath(sys.argv[sys.argv.index( '--real' ) + 1], sys.argv[sys.argv.index( '--database' ) + 1])
00038        if not os.path.isdir( path ):
00039               print _('Could not find database "%s"...') % unicode(path, 'utf-8')
00040               sys.exit( 1 )
00041        Global.database = path
00042 
00043 from common.freevialglob import *
00044 from score import Score
00045 from preguntador import Preguntador
00046 from roda import Roda
00047 
00048 gettext.install('freevial', '/usr/share/locale', unicode=1)
00049 
00050 # Version information
00051 VERSION = '1.2'
00052 SERIES = 'gresca'
00053 
00054 
00055 class Freevial:
00056        
00057        def __init__( self ):
00058               
00059               Global.game = GlobalVar()
00060               
00061               Global.game.screen = ''
00062               Global.game.rounds = 0
00063               
00064               Global.game.teams = []
00065               
00066               Global.game.current_team = 0
00067               Global.game.sfc_credits = ''
00068        
00069        
00070        def inici( self ):
00071               
00072               # inicialize presentation surface
00073               Global.game.screen = pygame.display.set_mode( ( Global.screen_x, Global.screen_y), HWSURFACE, 32)
00074               pygame.display.set_caption('Freevial')
00075               pygame.display.set_icon( loadImage('freevial.png') )
00076               
00077               if not Global.DEBUG_MODE:
00078                      pygame.mouse.set_visible( False )
00079 
00080                      if Global.FS_MODE:
00081                             pygame.display.toggle_fullscreen()
00082               
00083               # inicialize sound and text systems
00084               if not (Global.SOUND_MUTE and Global.MUSIC_MUTE):
00085                      try:
00086                             pygame.mixer.pre_init(44100, -16, 2, 2048)
00087                             pygame.mixer.init()
00088                      except pygame.error, message:
00089                             print _('Sound initialization failed. %s' % message)
00090                             Global.SOUND_MUTE = True
00091                             Global.MUSIC_MUTE = True
00092               
00093               pygame.font.init()
00094               
00095               Global.game.sfc_credits = createHelpScreen( 'credits', alternate_text = True )
00096               
00097               initTextos()
00098               
00099               # initialize joystick, if there's one
00100               pygame.joystick.init()
00101               if pygame.joystick.get_count():
00102                      pygame.joystick.Joystick( 0 ).init()
00103               
00104               # initialize skin system
00105               Global.game.skin = Skin()
00106               Global.game.max_teams = Global.game.skin.configGetInt( 'max_teams', domain='game' )
00107               for num in range( 0, Global.game.max_teams ): Global.game.teams.append( Equip() )
00108        
00109        
00110        def juguem( self ):
00111               
00112               self.inici()
00113               
00114               score = roda = fespregunta = None
00115 
00116               while 1:
00117                      
00118                      if not score: score = Score( Global.game )
00119                      
00120                      Global.game.current_team = score.juguem()
00121                      
00122                      if Global.game.current_team != -1:
00123                             
00124                             Global.game.rounds += 1            
00125                             
00126                             if not roda: roda = Roda( Global.game )
00127                             
00128                             resultat = roda.juguem( )
00129                             
00130                             if resultat != -1:
00131                                           
00132                                    Global.game.teams[ Global.game.current_team ].preguntes_tot[ resultat - 1 ] += 1           
00133                                    
00134                                    if not fespregunta:  fespregunta = Preguntador( Global.game )
00135                                    
00136                                    resultat = fespregunta.juguem( resultat ) 
00137                                    
00138                                    if resultat > -1:
00139                                           Global.game.teams[ Global.game.current_team ].preguntes_ok[ resultat - 1 ] += 1
00140                                           Global.game.teams[ Global.game.current_team ].punts += 1
00141                                           
00142                                           fig_abans = Global.game.teams[ Global.game.current_team].figureta
00143                                           Global.game.teams[ Global.game.current_team].activaCategoria( resultat ) 
00144                                           
00145                                           if fig_abans != 63 and Global.game.teams[ Global.game.current_team].figureta == 63:
00146                                                  Global.game.teams[ Global.game.current_team].punts += 2
00147                                    else:
00148                                           Global.game.teams[ Global.game.current_team ].errors += 1
00149                                           
00150                                    Global.game.current_team = seguentEquipActiu( Global.game.teams, Global.game.current_team )
00151                      else:
00152                             sys.exit()
00153 
00154 
00155 if '-h' in sys.argv or '--help' in sys.argv:
00156        print
00157        print _('Usage: freevial [OPTIONS]')
00158        print
00159        print _('-d, --debug\t\tDebug mode.')
00160        print _('-m, --mute\t\tDisable all sounds and music.')
00161        print _('-f, --fullscreen\tStart in fullscreen mode.')
00162        print _('-l, --locked\t\tStart game in locked mode.')
00163        print _('-h, --help\t\tDisplay this message.')
00164        print _('-v, --version\t\tPrint information about the current version.')
00165        print _('--database <path>\tSet the absolute path to the database file / directory.')
00166        print _('--skin <path>\t\tSet the absolute path to the skin file / directory.')
00167        print _('--no-sound\t\tDisable sound.')
00168        print _('--no-music\t\tDisable music.')
00169        print _('--fps\t\t\tPrint framerate on screen.')
00170        print _('--info-db\t\tPrints information about the loaded database and exists.')
00171        print _('--psyco\t\t\tUse psyco, if available (this will use more memory).')
00172        print
00173 
00174        exit( 0 )
00175 
00176 if '-v' in sys.argv or '--version' in sys.argv:
00177        print
00178        print _('Freevial, a trivia platform for use on community events')
00179        print 'You are running version %s, which is part of the «%s» series.' % ( VERSION, SERIES )
00180        print
00181        print 'https://launchpad.net/freevial/%s' % SERIES
00182        print
00183        
00184        sys.exit( 0 )
00185 
00186 if '--info-db' in sys.argv or '--info-database' in sys.argv:
00187        total_categories = 0
00188        total_questions = 0
00189        categories = []
00190        
00191        for database in get_databases():
00192               total_categories += 1
00193               total_questions += len(database)
00194               categories.append((database.name, len(database)))
00195        
00196        print
00197        print 'Freevial - About the loaded database...\n'
00198        
00199        print 'Location:', os.path.abspath(Global.database)
00200        print 'Amount of categories:', total_categories
00201        print 'Amount of questions:', total_questions, '\n'
00202        
00203        for category in categories:
00204               print u'%s: %s questions' % (category[0], category[1])
00205        print
00206        
00207        sys.exit( 0 )
00208 
00209 if '-d' in sys.argv or '--debug' in sys.argv:
00210        Global.DEBUG_MODE = True
00211 
00212 if '-l' in sys.argv or '--locked' in sys.argv:
00213        Global.LOCKED_MODE = True
00214 
00215 if '--fullscreen' in sys.argv or '-f' in sys.argv:
00216        Global.FS_MODE = True
00217 
00218 if '--fps' in sys.argv:
00219        Global.DISPLAY_FPS = True
00220 
00221 if '-m' in sys.argv or '--mute' in sys.argv:
00222        Global.SOUND_MUTE = True
00223        Global.MUSIC_MUTE = True
00224 
00225 if '--no-sound' in sys.argv:
00226        Global.SOUND_MUTE = True
00227 
00228 if '--no-music' in sys.argv:
00229        Global.MUSIC_MUTE = True
00230 
00231 if '--skin' in sys.argv:
00232        path = os.path.abspath(os.path.join(sys.argv[sys.argv.index( '--real' ) + 1], sys.argv[sys.argv.index( '--skin' ) + 1]))
00233        setSkinName( path )
00234 
00235 # For technical reasons, the "--database" option is parsed somewhere
00236 # at the top of this file.
00237 
00238 try:
00239        if '--psyco' in sys.argv:
00240               try:
00241                      import psyco
00242                      psyco.profile()
00243               except ImportError:
00244                      print >> sys.stderr, _('Warning: Could not find psyco.')
00245        
00246        joc = Freevial()
00247        joc.juguem()
00248 
00249 except KeyboardInterrupt:
00250        print _('Manual exit.')
00251        sys.exit( 0 )