Back to index

freevial  1.2
preguntador.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002  
00003 #
00004 # Freevial
00005 # Questions Asker
00006 #
00007 # Copyright (C) 2007, 2008 The Freevial Team
00008 #
00009 # By Carles Oriol i Margarit <carles@kumbaworld.com>
00010 # By Siegfried-Angel Gevatter Pujals <siggi.gevatter@gmail.com>
00011 # By Nil Oriol <nil@kumbaworld.com>
00012 #
00013 # This program is free software: you can redistribute it and/or modify
00014 # it under the terms of the GNU General Public License as published by
00015 # the Free Software Foundation, either version 3 of the License, or
00016 # (at your option) any later version.
00017 #
00018 # This program is distributed in the hope that it will be useful,
00019 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00020 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021 # GNU General Public License for more details.
00022 #
00023 # You should have received a copy of the GNU General Public License
00024 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
00025 #
00026 
00027 import sys
00028 import time
00029 import pygame
00030 from math import *
00031 
00032 from common.freevialglob import *
00033 from common.events import EventHandle, waitForMouseRelease
00034 from questions import get_databases
00035 
00036 
00037 class Preguntador:
00038 
00039        def __init__( self, game ):
00040               
00041               self.game = game
00042               
00043               game.skin.set_domain( 'preguntador' )
00044               
00045               self.color_de_fons = game.skin.configGetRGB( 'color_de_fons' )
00046               self.color_de_text = game.skin.configGetRGB( 'color_de_text' )
00047               
00048               self.mida_font = game.skin.configGetInt('mida_font')
00049               self.altlinies = self.mida_font + 5
00050               
00051               self.mida_text = game.skin.configGetInt('mida_text')
00052               self.mida_text_autor = game.skin.configGetInt('mida_text_autor')
00053               
00054               self.postextx= game.skin.configGetInt('postextx')
00055               self.postexty = game.skin.configGetInt('postexty')
00056               self.mascara_de_fons = game.skin.configGet('mascara_de_fons')
00057               self.retalla_sel = game.skin.configGet('retalla_sel')
00058               
00059               self.solucio_ok = game.skin.configGet('solucio_ok')
00060               self.solucio_nook = game.skin.configGet('solucio_nook')
00061               
00062               self.lletraA = game.skin.configGet('lletraA')
00063               self.lletraB = game.skin.configGet('lletraB')
00064               self.lletraC = game.skin.configGet('lletraC')
00065               self.lletraAoff = game.skin.configGet('lletraAoff')
00066               self.lletraBoff = game.skin.configGet('lletraBoff')
00067               self.lletraCoff = game.skin.configGet('lletraCoff')
00068               
00069               self.itr1 = game.skin.configGet('itr1')
00070               self.itr2 = game.skin.configGet('itr2')
00071               
00072               self.so_ticking2 = game.skin.configGet( 'so_ticking2')
00073               self.so_ticking2_vol = game.skin.configGet( 'so_ticking2_vol')
00074               game.skin_mostra_punt_de_categoria = game.skin.configGetBool( 'mostra_punt_de_categoria')
00075               self.so_drum2 = game.skin.configGet( 'so_drum2')
00076               self.so_drum2_vol = game.skin.configGet( 'so_drum2_vol')
00077               
00078               self.so_sub = game.skin.configGet( 'so_sub')
00079               self.so_sub_vol = game.skin.configGet( 'so_sub_vol')
00080               
00081               self.so_ok = game.skin.configGet( 'so_ok')
00082               self.so_ok_vol = game.skin.configGet( 'so_ok_vol')
00083               
00084               self.so_nook = game.skin.configGet( 'so_nook')
00085               self.so_nook_vol = game.skin.configGet( 'so_nook_vol')
00086               
00087               self.mostraautor = game.skin.configGetBool( 'mostraautor')
00088 
00089               self.use_mask = game.skin.configGetBool( 'use_mask')
00090               
00091               self.preguntadorYpos = 190
00092               self.ypos = 0
00093               self.mou_fons = 0
00094               
00095               self.color_de_fons = self.color_de_fons
00096               self.color_de_text = self.color_de_text
00097               
00098               self.mida_font = self.mida_font
00099               self.altlinies = self.altlinies
00100               self.postextx = self.postextx
00101               self.postexty = self.postexty
00102               
00103               self.mascara_de_fons = game.skin.LoadImage( 'mascara_de_fons' )
00104               self.retalla_sel = game.skin.LoadImage( 'retalla_sel' )
00105               
00106               self.solucio_ok = game.skin.LoadImage( 'solucio_ok' )
00107               self.solucio_nook = game.skin.LoadImage( 'solucio_nook' )
00108               
00109               self.fons = range(0, 6)
00110               for num in range(0, 6):
00111                      self.fons[num] = loadImage(get_databases( num ).image)
00112                      sfcmask = loadImage( 'filtre_c' + str(num+1) + '.png' )
00113                      self.fons[num].blit( sfcmask, (0,0))
00114               
00115               self.mascara = pygame.Surface((655, 150), pygame.SRCALPHA, 32)
00116               
00117               self.lletres = [
00118                                                  [ game.skin.LoadImage( 'lletraA'), game.skin.LoadImage( 'lletraAoff') ], 
00119                                                  [ game.skin.LoadImage( 'lletraB'), game.skin.LoadImage( 'lletraBoff') ],                          
00120                                                  [ game.skin.LoadImage( 'lletraC'), game.skin.LoadImage( 'lletraCoff') ],
00121                                           ]
00122               
00123               self.info = [ game.skin.LoadImage( 'itr1'), game.skin.LoadImage( 'itr2') ]   
00124               
00125               self.so_ticking2 = game.skin.LoadSound( 'so_ticking2', 'so_ticking2_vol')
00126               self.so_drum2 = game.skin.LoadSound( 'so_drum2', 'so_drum2_vol')
00127               self.so_sub = game.skin.LoadSound( 'so_sub', 'so_sub_vol')
00128               self.so_ok = game.skin.LoadSound( 'so_ok', 'so_ok_vol')
00129               self.so_nook = game.skin.LoadSound( 'so_nook', 'so_nook_vol')
00130               
00131               
00132               self.categoria = None
00133               self.current_question = None
00134               self.num_asked_questions = 0
00135               self.show_answers = 0
00136               self.selected = 0
00137               
00138               
00139               self.help_overlay = createHelpScreen( 'preguntador' )
00140               
00141               self.help_on_screen = helpOnScreen( HOS_PREGUNTADOR_RUN )
00142               self.help_on_screen.sec_timeout = 10
00143 
00144        ###########################################
00145        #
00146        # Funció per veure el nombre de linies que té una frase a mostrar
00147        # basant-nos en que el separador és el caracter #
00148        def numlinies( self, cadena ):
00149               
00150               return cadena.count('#')
00151 
00152        ###########################################
00153        #
00154        # Cercador de preguntes a l'atzar
00155        # si la categoria és 0 no té en compte el valor
00156        def atzar( self, categoria ):
00157               
00158               self.categoria = categoria #- 1
00159               self.current_question = get_databases(self.categoria).question()
00160               self.num_asked_questions += 1
00161               
00162               self.selected = 0
00163               self.initialize_question()
00164        
00165        ###########################################
00166        #
00167        # Inicialitzador de nova pregunta
00168        #
00169        def initialize_question( self ):
00170 
00171               self.sfc_pregunta  = self.preguntadorPintatext( self.current_question['text'], self.game.skin.configGetInt("question_width") )
00172 
00173               self.sfc_resposta = range(0, 3)
00174               for num in xrange(0, 3):
00175                      self.sfc_resposta[ num ] = self.preguntadorPintatext( self.current_question[ 'opt' + str(num + 1) ], self.game.skin.configGetInt("answer_width") )
00176 
00177               self.sfc_apregunta = self.game.skin.render_text( str(self.current_question['author']), (self.color_de_text), (self.mida_text_autor) )
00178               self.sfc_apregunta.set_alpha( 64 ) 
00179 
00180               self.temps_inici_pregunta = time.time()
00181               self.segons = 61
00182               self.so_drum2.play()
00183               self.so_drum2.stop()
00184 
00185               self.show_answers = 0
00186 
00187        ###########################################
00188        #
00189        # Funció per pintar el text i les preguntes sobre una nova superficie
00190        # usant el color del text i el sobrejat
00191        def preguntadorPintatext( self, textapintar, maxample = 0 ):
00192 
00193               nalt = 0
00194               
00195               cadenes = textapintar.split('#')
00196               sfc_pregunta = range(0, len(cadenes) )
00197               sfc_shad = range(0, len(cadenes) )
00198 
00199               nlinia = 0
00200 
00201               for cadena in cadenes:
00202                      sfc_pregunta[nlinia] = self.game.skin.render_text( cadena if cadena != "" else " ", self.color_de_text, self.mida_font, 1, '', maxample - 2)
00203                      sfc_shad[nlinia] = self.game.skin.render_text( cadena if cadena != "" else " ", self.color_de_fons, self.mida_font, 1, '', maxample - 2)
00204                      nalt += sfc_pregunta[nlinia].get_height() + 2                              
00205                      nlinia += 1
00206               
00207               sfc = pygame.Surface( ( 1024 if maxample == 0 else maxample, nalt ), pygame.SRCALPHA, 32 )
00208 
00209               nalt = 0
00210               nlinia = 0
00211               for cadena in cadenes:
00212                      sfc.blit( sfc_shad[nlinia], (0 + 2, nalt + 2))
00213                      sfc.blit( sfc_pregunta[nlinia], (0, nalt ))
00214                      nalt += sfc_pregunta[nlinia].get_height() + 2
00215                      nlinia += 1
00216                      
00217               return sfc
00218 
00219        ###########################################
00220        #
00221        # Bucle principal del programa
00222        #
00223        def juguem( self , selcat):
00224               
00225               self.game.skin.set_domain( 'preguntador' )
00226               
00227               max_time = self.game.skin.configGetInt( 'max_time' )
00228               
00229               self.nom_equip_sfc = self.game.skin.render_text( self.game.teams[self.game.current_team].nom, self.game.skin.configGetRGB( "team_name_color" ), 30, 1 )   
00230               self.nom_equip_sfc = pygame.transform.rotate ( self.nom_equip_sfc, 90 )
00231               self.nom_equip_sfc.set_alpha( 64 )
00232               
00233               compos = 768
00234               
00235               self.help_on_screen.sec_timeout = 10
00236 
00237               self.frate = frameRate( Global.fps_limit )
00238 
00239               self.atzar( selcat )
00240 
00241               if not Global.SOUND_MUTE: pygame.time.wait( 2500 )
00242               
00243               waitForMouseRelease( )
00244               
00245               self.game.skin.LoadSound( 'so_fons', 'so_fons_vol', 1).play(1)
00246 
00247               mostra_punt_de_categoria = False
00248               mostra_ajuda = mostra_credits = 0
00249 
00250               self.game.screen.fill( (0,0,0,0) )
00251 
00252               # remaining seconds until end of answer time
00253               self.segons = max_time + 1
00254               
00255               if (self.game.teams[self.game.current_team].figureta & bitCategoria( selcat )) == 0 and self.game.skin_mostra_punt_de_categoria == True:
00256                      mostra_punt_de_categoria = True
00257                      figureta_no = loadImage('points/freevial_tot' + str( self.game.teams[self.game.current_team].figureta).zfill(2) + '.png')
00258                      figureta_si = loadImage('points/freevial_tot' + str( self.game.teams[self.game.current_team].figureta | bitCategoria ( selcat )).zfill(2) + '.png')
00259                      match_point = True if (self.game.teams[self.game.current_team].figureta | bitCategoria ( selcat ) == 63) else False
00260               
00261               mostra_comentaris = False
00262               sfc_comentaris = None
00263 
00264               self.help_on_screen.activitat( )
00265        
00266               while 1:
00267                      
00268                      acaba = 0
00269                      
00270                      # Iterador d'events
00271                      for event in pygame.event.get():
00272                             
00273                             eventhandle = EventHandle(event)
00274                             if eventhandle.handled: continue
00275                             
00276                             self.help_on_screen.activitat(event)
00277                             
00278                             if eventhandle.keyUp('q', 'ESCAPE') and not Global.LOCKED_MODE:
00279                                    if not mostra_ajuda and not mostra_credits:
00280                                           if not (Global.SOUND_MUTE or Global.MUSIC_MUTE):
00281                                                  pygame.mixer.fadeout(500)
00282                                           if self.show_answers == 0:
00283                                                  self.show_answers = 1
00284                                                  self.selected = 0
00285                                           acaba = 1
00286                                    else:
00287                                           mostra_ajuda = mostra_credits = 0
00288                             
00289                             if eventhandle.keyUp('F1', 'h'):
00290                                    mostra_ajuda ^= 1
00291                                    mostra_credits = 0
00292 
00293                             if eventhandle.keyUp('F2'):
00294                                    mostra_ajuda = 0
00295                                    mostra_credits ^= 1
00296                             
00297                             if self.show_answers == 0:
00298                                    if eventhandle.keyUp('a', 'i'):
00299                                           if eventhandle.isKey('a'): acaba = 1      
00300                                           self.selected = 1
00301                                           self.so_sub.play()
00302                                    
00303                                    if eventhandle.keyUp('b', 'o'):    
00304                                           if eventhandle.isKey('b'): acaba = 1
00305                                           self.selected = 2
00306                                           self.so_sub.play()
00307                                    
00308                                    if eventhandle.keyUp('c', 'p'):    
00309                                           if eventhandle.isKey('c'): acaba = 1
00310                                           self.selected = 3
00311                                           self.so_sub.play()
00312                                    
00313                                    if eventhandle.keyUp('DOWN', 'TAB') or eventhandle.isClick ( 5 ): 
00314                                           self.selected += 1
00315                                           if self.selected == 4:
00316                                                  self.selected = 1
00317                                           self.so_sub.play()
00318                                    
00319                                    if eventhandle.keyUp(event, 'UP') or eventhandle.isClick ( 4 ): 
00320                                           self.selected -= 1
00321                                           if self.selected <= 0:
00322                                                  self.selected = 3    
00323                                           self.so_sub.play()
00324                             
00325                             if eventhandle.keyUp('z'):  
00326                                    self.mostraautor ^= 1
00327                             
00328                             for num in range(1, 7):
00329                                    if eventhandle.keyUp(str(num), 'KP' + str(num)):
00330                                           self.atzar( num-1 )
00331                             
00332                             if eventhandle.isRelease('primary') or eventhandle.keyUp('RETURN', 'SPACE', 'KP_ENTER'):
00333                                    if self.selected != 0 or self.segons <= 0:
00334                                           acaba = 1
00335                             
00336                             if eventhandle.keyUp('F3') and self.show_answers == 3 and len(self.current_question['comment']) > 5:     
00337                                    mostra_comentaris ^= 1
00338 
00339                      # Si hem premut a return o s'ha acabat el temps finalitzem
00340                      if acaba == 1 or self.segons <= 0:
00341                             if not Global.MUSIC_MUTE:
00342                                    pygame.mixer.music.fadeout(2500)
00343                             self.help_on_screen.sec_timeout = 3
00344                             if self.show_answers == 0:
00345                                    self.show_answers = 3
00346                                    if self.current_question['answer'] == self.selected:
00347                                           self.so_ok.play()
00348                                    else:
00349                                           self.so_nook.play()
00350                                    notes = self.current_question['comment'].split('#') if self.current_question['comment'] != "" else "."
00351                                    sfc_comentaris = createTextSurface( notes, (128,255,255), 25 )
00352                             elif acaba == 1:
00353                                    if not Global.LOCKED_MODE or mostra_comentaris == True or len( self.current_question['comment'] ) <= 5:
00354                                           if not (Global.MUSIC_MUTE or Global.SOUND_MUTE):
00355                                                  pygame.mixer.fadeout(2500)
00356                                           return self.categoria if ( self.current_question['answer'] == self.selected) else -1
00357                                    else:
00358                                           compos = 768
00359                                           mostra_comentaris = True;
00360 
00361                      # Animem el fons
00362                      self.ypos += 2
00363                      if self.ypos >= Global.screen_y: self.ypos %= Global.screen_y
00364                             
00365                      # Pintem el fons animat
00366                      self.game.screen.blit( self.fons[self.categoria], (0,0), (0, (768 - self.ypos), Global.screen_x, min(200, self.ypos)))
00367                      if self.ypos < 200:
00368                             self.game.screen.blit( self.fons[self.categoria], (0, min( 200, self.ypos)), (0, 0, Global.screen_x, 200 - min( 200, self.ypos)))
00369                      
00370                      # i el sombrejem per fer l'efecte de desapariió
00371                      # també pintem el logotip del peu a l'hora que esborrem el fons de self.game.screen
00372                      self.game.screen.blit( self.mascara_de_fons, (0, 0) )
00373                      
00374                      if self.use_mask:
00375                             # preparem el sobrejat de l'opció seleccionada
00376                             ympos = self.ypos + 300
00377                             ympos %= 768
00378                             self.mascara.blit( self.fons[ self.categoria], (0,0), (0, (768 - ympos), Global.screen_x, min( 200, ympos )))
00379                      
00380                             if ympos < 200: 
00381                                    self.mascara.blit( self.fons[ self.categoria], (0, min( 200, ympos)), (0, 0, Global.screen_x, 200 - min( 200, ympos)))
00382                      
00383                             # i el mesclem amb la mascara per donar-li forma
00384                             self.mascara.blit( self.retalla_sel, (0,0))
00385                      else:
00386                             self.mascara = self.retalla_sel
00387                                    
00388                      # pintem l'ombrejat on correspongui       
00389                      if self.selected == 1: self.game.screen.blit( self.mascara, ( self.postextx, 260))
00390                      if self.selected == 2: self.game.screen.blit( self.mascara, ( self.postextx, 260+150))
00391                      if self.selected == 3: self.game.screen.blit( self.mascara, ( self.postextx, 260+300))
00392                      
00393                      # mostrem l'autor i el nombre de pregunta
00394                      if self.mostraautor:
00395                             self.game.screen.blit( self.sfc_apregunta, (1024 - ( self.sfc_apregunta.get_width() + 25), 94) )
00396                             
00397                      # mostrem la pregunta
00398                      self.game.screen.blit( self.sfc_pregunta, (self.postextx, self.postexty) )   
00399                             
00400                      # i les solucions                  
00401                      linia_act = 270
00402                             
00403                      for num in range(0, 3):
00404                             self.game.screen.blit( self.lletres[num][(self.selected != num + 1)], ( self.postextx, linia_act + (150 * num)) )
00405                             self.game.screen.blit( self.sfc_resposta[ num ], (self.postextx + 180 , linia_act + 20 + (150 * num)) )         
00406                             
00407                      # comprovem l'estat del temps
00408                      segons_act = max_time - int( (time.time() - self.temps_inici_pregunta) )
00409                      if segons_act < 0: 
00410                             segons_act = 0
00411                             self.segons = 0
00412                             
00413                      # si no estem en l'estat de mostrar les soŀlucions mostrem el temps restant
00414                      if self.show_answers == 0:
00415                             if self.segons != segons_act:
00416                                    # el segon actual ha canviat
00417                                    self.segons = segons_act 
00418                                    self.pinta_segons = self.game.skin.render_text( str( self.segons ).zfill(2), (255,255,255), 600)
00419                                    # s'acaba el temps indiquem'ho amb so
00420                                    if self.segons < 20:
00421                                           self.so_ticking2.set_volume( (20 - float( self.segons )) / 20.0  ) 
00422                                           self.so_ticking2.play()
00423                             
00424                                    # pintem els segons que queden, posant-los cada cop menys transparents
00425                             self.pinta_segons.set_alpha( (max_time - segons_act) * 100 / max_time)
00426                             self.game.screen.blit( self.pinta_segons, ( 300 , 150) )
00427                      
00428                      # Pintem les solucions
00429                      linia_act = 270
00430                      posn = 700
00431                      posnook = 700 + cos(time.time()) * 25
00432                      posnook2 = 700 - cos(time.time()) * 25
00433                      posok = 700 + cos(time.time() * 2) * 50
00434                      
00435                      if self.show_answers > 0:
00436                                    
00437                             for num in range (0, 3):
00438                                    if self.current_question['answer'] == (num + 1):
00439                                           if self.selected != (num + 1):     
00440                                                  self.game.screen.blit( self.solucio_ok, (posnook2, linia_act + (150 * num)) )
00441                                           else:
00442                                                  self.game.screen.blit( self.solucio_ok, (posok, linia_act + (150 * num)) )
00443                                           
00444                                    else:
00445                                           if self.selected == (num + 1):
00446                                                  self.game.screen.blit( self.solucio_nook, (posnook, linia_act + (150 * num)) )
00447                                    
00448                             if len( self.current_question['comment'] ) > 5:
00449                                    self.game.screen.blit( self.info[0] if (int(time.time() * 3) % 3) == 0 else self.info[1], (self.postextx, 150) )
00450                             
00451                      if mostra_punt_de_categoria:
00452                             if match_point:
00453                                    t = time.time()
00454                                    for compta in range( 0, 16 ) :
00455                                           self.game.screen.blit( figureta_no if (int(time.time() * 2) % 2) == 0 else figureta_si, (500 + cos(t+(float(compta)/15)) * 400, 110 + sin((t + (float(compta)/10)) * 2) * 25) )
00456                             else:
00457                                    self.game.screen.blit( figureta_no if (int(time.time() * 2) % 2) == 0 else figureta_si, (880, 130) )
00458 
00459                      self.game.screen.blit( self.nom_equip_sfc, (20, 748 - self.nom_equip_sfc.get_height()))
00460 
00461                      if mostra_comentaris and sfc_comentaris is not None:
00462                             if compos > 0: compos -= 100
00463                             self.game.screen.blit( sfc_comentaris, (0, compos))
00464                      elif sfc_comentaris is not None:
00465                             if compos < 768: 
00466                                    compos += 100
00467                                    self.game.screen.blit( sfc_comentaris, (0, compos))
00468                      
00469                      self.help_on_screen.draw( self.game.screen, (350, 740), HOS_PREGUNTADOR_END if self.show_answers else HOS_PREGUNTADOR_RUN )
00470                      
00471                      if mostra_ajuda: self.game.screen.blit( self.help_overlay, (0,0))
00472                      if mostra_credits: self.game.screen.blit( self.game.sfc_credits, (0,0))
00473                      
00474                      self.frate.next( self.game.screen )
00475                      
00476                      # Exchange self.game.screen buffers
00477                      pygame.display.flip()
00478               
00479               pygame.mixer.music.stop()
00480 
00481               return 0