Back to index

freevial  1.2
score.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002  
00003 #
00004 # Freevial
00005 # Teams and Puntuation
00006 #
00007 # Copyright (C) 2007, 2008 The Freevial Team
00008 #
00009 # By Carles Oriol <carles@kumbaworld.com>
00010 # By Siegfried-Angel Gevatter Pujals <siggi.gevatter@gmail.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.path
00028 from random import randint
00029 import pygame
00030 from math import *
00031 from pygame.locals import *
00032 
00033 from common.globals import Global
00034 from common.freevialglob import *
00035 from common.events import EventHandle
00036 from common.events import waitForMouseRelease
00037 from common.dialog_question import fesPregunta
00038 from endscreen import Visca
00039 from selcat import *
00040 
00041 
00042 class Score:
00043 
00044        def __init__( self, game ):
00045               
00046               self.game = game
00047               game.skin.set_domain( 'score' )           
00048               
00049               self.help_overlay = createHelpScreen( 'score' )
00050               
00051               self.help_on_screen = helpOnScreen( HOS_SCORE_MODE0 )
00052               
00053               self.score_color_text = game.skin.configGetRGB( 'color_text' )
00054               self.score_mida_text = game.skin.configGetInt( 'mida_text')
00055               
00056               self.score_fons = game.skin.configGet( 'background')
00057               self.score_mascara_de_fons = game.skin.configGet( 'background_mask')
00058               self.score_element = game.skin.configGet( 'element')
00059               self.score_element_sel = game.skin.configGet( 'sel_element')
00060               self.score_element_sobre = game.skin.configGet( 'element_sobre')
00061               self.score_element_sel_offsetx = game.skin.configGetInt( 'sel_element_offsetx')
00062               self.score_element_sel_offsety = game.skin.configGetInt( 'sel_element_offsety')
00063               self.score_teams_offsetx = game.skin.configGetInt( 'teams_offsetx')
00064               self.score_teams_offsety = game.skin.configGetInt( 'teams_offsety')
00065               self.score_resultat_visible = game.skin.configGet( 'resultat_visible')
00066               self.score_figureta_visible = game.skin.configGet( 'figureta_visible') 
00067               self.score_figureta_mode = game.skin.configGet( 'figureta_mode') # 0 - del 0 al 63 combinacions 1 - del 0 al 5 figures individuals
00068               self.score_figureta_mascara = game.skin.configGet( 'figureta_mask')
00069               
00070               self.score_figureta_offsetx = game.skin.configGetInt( 'figureta_offsetx')
00071               self.score_figureta_offsety = game.skin.configGetInt( 'figureta_offsety')
00072               
00073               self.score_figureta_individual_pos = game.skin.configGetEval( 'figureta_individual_pos' )
00074               
00075               self.score_figureta_show_hide = game.skin.configGet( 'figureta_show_hide') # 0 - Es mostren les parts aconseguides, 1 - S'amaguen les parts aconseguides
00076               
00077               self.score_so_sub = game.skin.configGet( 'sub_sound')
00078               self.score_so_sub_vol = game.skin.configGet( 'sub_sound_vol')
00079               self.score_so_sub2 = game.skin.configGet( 'sub_sound2')
00080               self.score_so_sub2_vol = game.skin.configGet( 'sub_sound2_vol')
00081               
00082               self.score_ok = game.skin.configGet( 'ok')
00083               self.score_ok_vol = game.skin.configGet( 'ok_vol')
00084               
00085               self.score_locked = game.skin.configGet( 'locked')
00086               self.score_locked_pos = (game.skin.configGetInt( 'locked_pos_X'),game.skin.configGetInt( 'locked_pos_Y'))
00087               
00088               self.score_so_de_fons = game.skin.configGet( 'background_sound')
00089               self.score_so_de_fons_vol = game.skin.configGet( 'background_sound_vol')
00090 
00091               self.score_desplaca_el_fons = game.skin.configGet( 'move_background') # True o False = no hi ha scroll vertical
00092               self.score_ones_al_fons = game.skin.configGet( 'background_waves') # True o False = quiet
00093               
00094               
00095               self.score_caixes = game.skin.configGetEval( "boxes_coord" )
00096               
00097               self.show_corrects = game.skin.configGetBool( "show_corrects" )
00098               if self.show_corrects:
00099                      self.corrects_coord = game.skin.configGetEval( "corrects_coord" )
00100                      self.total_corrects = game.skin.configGetInt( "total_corrects" )
00101               
00102               self.final_stats = game.skin.configGetBool( "final_stats" )
00103               
00104               #------------------------------------------
00105               
00106               self.ypos = 0
00107               self.mou_fons = 0
00108               #-----------------------------------------------
00109               
00110               self.figureta = game.skin.LoadImageRange( 'figureta_mask', 64, 2)            
00111        
00112               self.mascara_de_fons = game.skin.LoadImage( 'background_mask' )
00113               self.fons = game.skin.LoadImage( 'background' )
00114               self.element_score = game.skin.LoadImage( 'element' )
00115               self.seleccio_score = game.skin.LoadImage( 'sel_element' )
00116               self.so_sub = game.skin.LoadSound( 'sub_sound', 'sub_sound_vol' )
00117               self.so_sub2 = game.skin.LoadSound( 'sub_sound2', 'sub_sound2_vol' )
00118               self.so_ok = game.skin.LoadSound( 'ok', 'ok_vol' )
00119               self.sfc_llum = game.skin.LoadImage( 'locked' )
00120               
00121               if self.show_corrects:
00122                      self.correct_done_image = game.skin.LoadImage( 'correct_done_image' ) if game.skin.configGet( "correct_done_image" ) != "" else None
00123                      self.correct_notdone_image = game.skin.LoadImage( 'correct_notdone_image' ) if game.skin.configGet( "correct_notdone_image" ) != "" else None
00124               
00125               self.sfc_cursor = game.skin.render_text( "_", (self.score_color_text), self.score_mida_text, 1)
00126        
00127        def barra_pos( self, total, posicio, color, ample, alt ):
00128 
00129               sfc = pygame.Surface( ( ample, alt), pygame.SRCALPHA, 32 )
00130               pygame.draw.rect(sfc, color, (0,0,ample-1,alt-1), 2)
00131 
00132               ample_rect = ample - 8
00133 
00134               pygame.draw.rect(sfc, (color[0], color[1], color[2], 64), (4, 4, ample_rect, alt - 8))
00135               if total != 0 and posicio != 0: 
00136                      pos_ample = ( posicio * ample_rect ) / total 
00137                      pygame.draw.rect(sfc, color, (4, 4, pos_ample, alt - 8))
00138 
00139               return sfc
00140        
00141        def show_end_screen( self, startsound = False ):
00142               
00143               winner_team = self.game.skin.teamsGuanyador( self.game.teams )
00144               
00145               if count_not_empty(Global.game.teams, attr = 'name') == 1:
00146                      winner_team = 1
00147               #######print Global.game.teams, count_not_empty(Global.game.teams, attr = 'name'), winner_team
00148               if winner_team > -1:
00149                      if startsound:
00150                             self.so_ok.play()
00151                      visca = Visca( self.game )
00152                      resultat = visca.juguem( self.game, Global.game.teams[ winner_team ].nom )
00153                      self.game.skin.LoadSound( 'background_sound', 'background_sound_vol', music = 1 ).play( -1 )
00154               else:
00155                      print "\a"
00156        
00157        def juguem( self ):
00158               
00159               self.game.skin.set_domain( 'score' )
00160               frate = frameRate( Global.fps_limit )
00161               
00162               waitForMouseRelease( )
00163               
00164               self.game.screen.fill( (0,0,0,0) )
00165               
00166               ypos = escriu = atzar = mou_fons = mostra_ajuda = mostra_credits = show_stats = 0
00167               element_seleccionat = self.game.current_team
00168               nou_grup = 1 if ( teamsActius( self.game.teams ) == 0 ) else 0
00169               
00170               # Modes: 0 (choosing teams), 1 (playing),  2 (game ended)
00171               mode = 1
00172               
00173               if nou_grup: mode = 0
00174               if self.game.skin.teamsGuanyador( self.game.teams ) != -1: 
00175                      mode = 2
00176                      show_stats = 1
00177                      element_seleccionat = self.game.skin.teamsGuanyador( self.game.teams )
00178                      self.so_ok.play()
00179               else:
00180                      self.game.skin.LoadSound( 'background_sound', 'background_sound_vol', music = 1 ).play( -1 )
00181               
00182               surten = 0
00183               mostrada_victoria = False
00184               
00185               self.help_on_screen.activitat()
00186               
00187               while 1:
00188                      
00189                      if mode == 2:
00190                             if frate.segons() < 4.1 and int(frate.segons()) > surten:
00191                                    surten = int( frate.segons() )
00192                                    self.so_ok.play(1)
00193 
00194                             if frate.segons() > 4.1 and not mostrada_victoria:
00195                                    self.show_end_screen()
00196                                    mostrada_victoria = True
00197                      
00198                      # Event iterator
00199                      for event in pygame.event.get():
00200                             
00201                             eventhandle = EventHandle(event)
00202                             if eventhandle.handled: continue
00203                             
00204                             self.help_on_screen.activitat(event)
00205                             
00206                             if eventhandle.keyUp('F1') or (not escriu and eventhandle.keyUp('h')):
00207                                    mostra_ajuda ^= 1
00208                                    mostra_credits = 0
00209                             
00210                             if eventhandle.keyDown('F2'):
00211                                    mostra_credits ^= 1
00212                                    mostra_ajuda = 0
00213                             
00214                             if escriu and not mostra_ajuda and not mostra_credits:
00215                             
00216                                    if eventhandle.isClick('primary') or eventhandle.keyUp('RETURN', 'ESCAPE', 'KP_ENTER'):
00217                                           escriu = 0
00218                                           if self.game.teams[element_seleccionat].nom == '' and eventhandle.isKey('ESCAPE'):
00219                                                  self.game.teams[element_seleccionat].actiu = 0
00220                                    
00221                                    elif eventhandle.isDown():
00222                                           
00223                                           if eventhandle.isKey('BACKSPACE'):
00224                                                  if len(self.game.teams[element_seleccionat].nom) > 0:
00225                                                         newname = self.game.teams[element_seleccionat].nom[:-1]
00226                                           else:
00227                                                  newname = self.game.teams[element_seleccionat].nom + eventhandle.str()
00228                                           
00229                                           if newname != None:
00230                                                  sfc = self.game.skin.render_text( newname, (self.score_color_text), self.score_mida_text, 1)
00231                                                  
00232                                                  if sfc.get_width() < 340:
00233                                                         # Name isn't too long, accept the new character
00234                                                         self.game.teams[element_seleccionat].nom = newname
00235                                                         self.game.teams[element_seleccionat].sfc_nom = sfc
00236                             
00237                             else:
00238                                    
00239                                    if eventhandle.keyUp('q', 'ESCAPE'):
00240                                           if not mostra_ajuda and not mostra_credits:
00241                                                  if not Global.LOCKED_MODE:
00242                                                         if fesPregunta( self.game.screen , valorText( HOS_QUIT ), (valorText( HOS_YES ), valorText( HOS_NO )), color = self.game.skin.configGetRGB( "game_question_color", "game" ) ) == 0:
00243                                                                if not Global.MUSIC_MUTE:
00244                                                                       pygame.mixer.music.fadeout( 500 )
00245                                                                       pygame.time.wait( 500 )
00246                                                                return -1
00247                                           else:
00248                                                  mostra_ajuda = mostra_credits = 0
00249                                    
00250                                    if mode == 0:
00251                                           
00252                                           if eventhandle.keyUp('RIGHT', 'LEFT'):
00253                                                  element_seleccionat += 1 if (0 == (element_seleccionat % 2)) else -1 
00254                                                  self.so_sub.play()
00255                                           
00256                                           if eventhandle.keyUp('DOWN'): 
00257                                                  element_seleccionat = (element_seleccionat + 2) % self.game.skin.configGetInt( "max_teams", "game" )
00258                                                  self.so_sub.play()
00259                                           
00260                                           if eventhandle.keyUp('UP'): 
00261                                                  element_seleccionat = (element_seleccionat - 2) % self.game.skin.configGetInt( "max_teams", "game" )
00262                                                  self.so_sub.play()
00263                                           
00264                                           if eventhandle.keyUp('a'):
00265                                                  nou_grup = 1
00266                                           
00267                                           if eventhandle.keyUp('n'):
00268                                                  if self.game.teams[element_seleccionat].actiu:
00269                                                         escriu ^= 1
00270                                                  else:
00271                                                         nou_grup = 1
00272                                           
00273                                           if eventhandle.keyUp('PAGEDOWN') and teamsActius( self.game.teams ) >= 1:
00274                                                  element_seleccionat = seguentEquipActiu( self.game.teams, element_seleccionat )
00275                                                  self.so_sub.play()
00276                                           
00277                                           if eventhandle.keyUp('PAGEUP') and teamsActius( self.game.teams ) >= 1:
00278                                                  element_seleccionat = anteriorEquipActiu( self.game.teams, element_seleccionat )
00279                                                  self.so_sub.play() 
00280                                           
00281                                           if eventhandle.keyUp('r') and teamsActius( self.game.teams ) > 0:
00282                                                  atzar = randint(15, 50)
00283                                                  mode = 1
00284                                    
00285                                    if eventhandle.keyUp('z'): 
00286                                           if self.game.teams[element_seleccionat].actiu:
00287                                                  self.game.teams[element_seleccionat].punts += 1
00288                                    
00289                                    if eventhandle.keyUp('x'): 
00290                                           if self.game.teams[element_seleccionat].actiu and self.game.teams[element_seleccionat].punts > 0:
00291                                                  self.game.teams[element_seleccionat].punts -= 1
00292                                    
00293                                    if self.game.teams[element_seleccionat].actiu:
00294                                           for num in range(1, 7):
00295                                                  if eventhandle.keyUp(str(num), 'KP' + str(num)):
00296                                                         self.game.teams[element_seleccionat].canviaCategoria( num-1 )
00297                                    
00298                                    if eventhandle.isClick('primary') or eventhandle.keyUp('RETURN', 'SPACE', 'KP_ENTER'):
00299 
00300                                           if mode == 1:
00301                                                  if not Global.MUSIC_MUTE:
00302                                                         pygame.mixer.music.fadeout( 2000 )
00303                                                  return element_seleccionat
00304 
00305                                           elif mode == 0:
00306                                                  if self.game.teams[element_seleccionat].actiu and eventhandle.keyUp('SPACE') :
00307                                                         atzar = int( randint(25, 60) )
00308                                                         mode = 1
00309                                                  else:
00310                                                         if self.game.teams[element_seleccionat].actiu: escriu ^= 1
00311                                                         else: nou_grup = 1
00312                                           else:
00313                                                  if fesPregunta( self.game.screen , valorText( HOS_NEW_GAME ), (valorText( HOS_YES ), valorText( HOS_NO )), color = self.game.skin.configGetRGB( "game_question_color", "game" )) == 0:
00314                                                         mode = 0
00315                                                         show_stats = 0 
00316                             
00317                                                         for equip in self.game.teams:
00318                                                                for num in range(0, 6): 
00319                                                                       equip.preguntes_tot[num] = 0
00320                                                                       equip.preguntes_ok[num] = 0
00321                                                                equip.punts = 0
00322                                                                equip.figureta = 0                        
00323                                    
00324                                    if eventhandle.keyUp('s'): 
00325                                           show_stats ^= 1
00326                                    
00327                                    if eventhandle.keyUp('m'):
00328                                           replaceModes = {
00329                                                         0: 1,
00330                                                         1: 0,
00331                                                         2: 1,
00332                                                  }
00333                                           mode = replaceModes[ mode ]
00334 
00335                                    if eventhandle.keyUp('e') and not Global.LOCKED_MODE :
00336                                           self.show_end_screen( startsound = True )
00337                                           mostrada_victoria = True
00338                                    
00339                                    if eventhandle.keyUp('l'): 
00340                                           Global.LOCKED_MODE = (not Global.LOCKED_MODE)
00341 
00342                                    if eventhandle.keyUp('k', 'F3', 'F5') and mode == 0:
00343                                           selcat = SelCat( self.game )
00344                                           selcat.juguem( mode )
00345 
00346                      if nou_grup == 1:
00347                             self.so_sub.play()
00348                             nou_grup = 0
00349                             self.game.teams[element_seleccionat].actiu ^= 1
00350                             if self.game.teams[element_seleccionat].actiu and self.game.teams[element_seleccionat].nom == "": escriu ^= 1
00351 
00352                      if atzar != 0 and teamsActius( self.game.teams ) >= 2:
00353                             element_seleccionat = seguentEquipActiu( self.game.teams, element_seleccionat )
00354                             atzar -= 1
00355                             self.so_sub2.play()
00356                      else:
00357                             atzar = 0
00358                      
00359                      if self.score_desplaca_el_fons != "False":
00360                             # Animem el fons
00361                             self.ypos += 1
00362                             self.ypos %= Global.screen_y
00363                      
00364                      xpinta = 0
00365                      
00366                      if self.score_ones_al_fons:
00367                             self.mou_fons += 8
00368                             
00369 
00370                      # Pintem el fons animat
00371                      for num in range(0, 768):
00372                             
00373                             if self.score_ones_al_fons:
00374                                    xpinta = cos((float(self.mou_fons +num)) / 100.0) * 20
00375                      
00376                             self.game.screen.blit( self.fons, (xpinta, num), (0, (self.ypos + num) % 768, 1024, 1) )
00377                      
00378                      self.game.screen.blit( self.mascara_de_fons, (0, 0) )
00379                      # pintem les puntuacions
00380 
00381                      for num in range(0, Global.game.max_teams):
00382                             ycaixa = self.score_caixes[num][1]
00383                             xcaixa = self.score_caixes[num][0]
00384 
00385                             if element_seleccionat == num and self.score_element_sobre != "True":
00386                                    for compta in range( 0, self.seleccio_score.get_height() ):
00387                                           desp = 0 if not mode else ( cos( frate.segons() * 10.0 + (float(compta)/10.0) ) * 2.0 )
00388                                           self.game.screen.blit( self.seleccio_score, (xcaixa + self.score_element_sel_offsetx + desp, ycaixa + self.score_element_sel_offsety + compta), (0,compta, self.seleccio_score.get_width(),1) )
00389 
00390                             
00391                             if self.game.teams[num].actiu:
00392                                    
00393                                    self.game.screen.blit( self.element_score, (xcaixa, ycaixa ) )
00394                                    
00395                                    if self.score_figureta_visible == 'True':
00396                                           self.game.screen.blit( self.figureta[self.game.teams[num].figureta], (xcaixa + self.score_figureta_offsetx, ycaixa + self.score_figureta_offsety ) )
00397 
00398                                    if self.game.teams[num].sfc_nom:
00399                                           self.game.screen.blit( self.game.teams[num].sfc_nom, (xcaixa + self.score_teams_offsetx , ycaixa + self.score_teams_offsety ) )
00400                                    ampletext = self.game.teams[num].sfc_nom.get_width() if self.game.teams[num].sfc_nom else 0
00401                                    if escriu and num == element_seleccionat:
00402                                           if (int(time.time() * 4) % 2) == 0: 
00403                                                  self.game.screen.blit( self.sfc_cursor, (xcaixa + self.score_teams_offsetx + ampletext, ycaixa + self.score_teams_offsety )) 
00404                                                  
00405                                    color = (128,0,0) if (maxPunts(self.game.teams) > self.game.teams[num].punts ) else (0,128,0)
00406                                    pinta = self.game.skin.render_text( str(self.game.teams[num].punts).zfill(2), color, 150, 1)
00407                                    if self.score_resultat_visible == 'True':
00408                                           self.game.screen.blit( pinta, (xcaixa + 200, ycaixa - 15) )
00409 
00410                                    if show_stats and self.final_stats:
00411                                           for cat in range(0,6):
00412                                                  self.game.screen.blit( self.barra_pos( self.game.teams[num].preguntes_tot[cat], self.game.teams[num].preguntes_ok[cat],  colorsCategories()[cat], 50, 14 ), (xcaixa + 140, ycaixa + 21 + cat * 16) )
00413                             
00414                                    
00415                                    if self.show_corrects:
00416                                           for compta in range(0, self.total_corrects):
00417                                                  if self.game.teams[num].punts > compta:
00418                                                         if self.correct_done_image != None:
00419                                                                self.game.screen.blit( self.correct_done_image, (xcaixa + self.corrects_coord[compta][0], ycaixa + self.corrects_coord[compta][1] ))
00420                                                  else:
00421                                                         if self.correct_notdone_image != None:
00422                                                                self.game.screen.blit( self.correct_notdone_image, (xcaixa + self.corrects_coord[compta][0], ycaixa + self.corrects_coord[compta][1] ))
00423                                           
00424                             if element_seleccionat == num and self.score_element_sobre == "True":
00425                                    for compta in range( 0, self.seleccio_score.get_height() ):
00426                                           desp = 0 if not escriu else ( cos( frate.segons() * 10.0 + (float(compta)/10.0) ) * 2.0 )
00427                                           self.game.screen.blit( self.seleccio_score, (xcaixa + self.score_element_sel_offsetx + desp, ycaixa + self.score_element_sel_offsety + compta), (0,compta, self.seleccio_score.get_width(),1) )
00428                      
00429                      
00430                      if Global.LOCKED_MODE: 
00431                             self.game.screen.blit( self.sfc_llum, (0, 0) )
00432                      
00433                      if mostra_ajuda: self.game.screen.blit( self.help_overlay, (0,0))
00434                      if mostra_credits: self.game.screen.blit( self.game.sfc_credits, (0,0))
00435                      
00436                      self.help_on_screen.draw( self.game.screen, (350, 740), HOS_SCORE_MODEW if escriu else mode)
00437                      
00438                      frate.next( self.game.screen )
00439                      
00440                      # Exchange self.game.screen buffers
00441                      pygame.display.flip()
00442 
00443               return 0