Back to index

freevial  1.2
selcat.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002  
00003 #
00004 # Freevial
00005 # Game categories selector
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 import random
00029 import pygame
00030 from math import *
00031 from pygame.locals import *
00032 
00033 from common.freevialglob import *
00034 from common.events import EventHandle
00035 from questions import shuffle_databases, get_databases
00036 from roda import Roda
00037 
00038 def FindList( llista, element ):
00039 
00040        for compta in range( 0, len(llista) ):
00041               if llista[compta] == element:
00042                      return compta
00043 
00044        return -1
00045 
00046 class SelCat:
00047 
00048        def __init__( self, game ):
00049               
00050               self.game = game
00051               game.skin.set_domain( 'selcat' )
00052               
00053               self.mascara_de_fons =  game.skin.LoadImage('fons_score')
00054               self.fons = game.skin.LoadImage('score_fons')
00055 
00056               self.sel_quadres = game.skin.LoadImage('sel_quadres')
00057               self.sel_reflexos = game.skin.LoadImage('sel_reflexos')
00058 
00059               self.sel_fletxap = game.skin.LoadImage('sel_fletxap')
00060               self.sel_fletxab = game.skin.LoadImage('sel_fletxab')
00061 
00062               self.sel_quadre = game.skin.LoadImage('sel_quadre')
00063               self.sel_quadreok = game.skin.LoadImage('sel_quadreok')
00064 
00065               self.so_sub = game.skin.LoadSound( 'so_sub', 'so_sub_vol' )
00066               self.so_sub2 = game.skin.LoadSound( 'so_sub2', 'so_sub_vol' )
00067 
00068               self.selcat_color_text = game.skin.configGetRGB( 'color_text' )
00069               
00070               self.selcat_color_text_nosel = game.skin.configGetRGB( 'color_text_nosel' )
00071 
00072               self.help_overlay = createHelpScreen( 'score' )
00073 
00074               self.help_on_screen = helpOnScreen( HOS_SCORE_MODE0 )
00075 
00076               self.cp = get_databases()
00077               self.sfc_preguntes = range(0, len(self.cp))
00078 
00079               self.reinicia_cats()
00080 
00081               self.sfc_nombres = range(0,6)
00082               for compta in range(0, 6):
00083                      self.sfc_nombres[compta] = render_text( str(compta+1), colorsCategories()[compta], 35, 1, '', 50 )
00084 
00085        def refa_cats( self ):
00086               for cat in range(0, 6):
00087                      self.cp[cat].num = cat+1
00088        
00089        def reinicia_cats( self ):
00090               self.categories_seleccionades = [0,1,2,3,4,5];
00091        
00092               for compta in range(0, len(self.cp)):
00093                      color = self.selcat_color_text_nosel
00094                      if compta < 6:
00095                             color = colorsCategories()[compta]
00096                      self.sfc_preguntes[compta] = render_text( self.cp[compta].name, color, 27, 1, '', 220 )
00097 
00098        def CanviaElements( self, aposar, atreure):
00099 
00100               self.so_sub2.play() 
00101 
00102               if aposar == atreure:
00103                      return
00104 
00105               queda = self.cp[aposar]
00106               self.cp[aposar] = self.cp[atreure]
00107               self.cp[atreure] = queda
00108 
00109               self.darrera_info = -1
00110               self.reinicia_cats( )
00111 
00112        def PosaPrimer( self, seleccio ):
00113 
00114               self.so_sub2.play() 
00115               if seleccio == 0:
00116                      return
00117 
00118               actual = self.cp[seleccio]
00119               self.cp.remove( actual )
00120               self.cp.insert( 0, actual )
00121               self.darrera_info = -1
00122               self.reinicia_cats( )
00123 
00124        def juguem( self, estat ):
00125               # estat 0 = edició, 1/2 = veure categories
00126               
00127               self.game.skin.set_domain( 'selcat' )
00128               frate = frameRate( Global.fps_limit )
00129               
00130               self.game.screen.fill( (0,0,0,0) )
00131               
00132               ypos = mou_fons = mostra_ajuda = mostra_credits = 0
00133 
00134               self.darrera_info = -1
00135 
00136               darrer_element_a_la_vista = 0
00137 
00138               primer_element_a_la_vista = 0
00139 
00140               self.help_on_screen.activitat()
00141                      
00142               seleccio = 0
00143 
00144               nelements = len(self.cp) if estat == 0 else 6
00145 
00146               while 1:
00147                             
00148                      # Event iterator
00149                      for event in pygame.event.get():
00150 
00151                             eventhandle = EventHandle(event)
00152                             if eventhandle.handled: continue
00153                             
00154                             if eventhandle.keyUp('q', 'ESCAPE', 'KP_ENTER', 'F3', 'F5'):
00155                                    if estat == 0: self.refa_cats();
00156                                    return
00157                             
00158                             if eventhandle.keyUp('DOWN'):
00159                                    seleccio += 1
00160                                    seleccio %=  nelements
00161                                    self.so_sub.play() 
00162                             
00163                             if eventhandle.keyUp('UP'):
00164                                    seleccio -= 1
00165                                    seleccio %=  nelements 
00166                                    self.so_sub.play() 
00167                             
00168                             if estat == 0:
00169 
00170                                    if eventhandle.keyUp('r'):
00171                                           shuffle_databases()
00172                                           self.reinicia_cats( )
00173                                           self.so_sub2.play()
00174                                           self.darrera_info = -1
00175 
00176                                    for num in range(0, 6):
00177                                           if eventhandle.keyUp(str(num + 1), 'KP' + str(num + 1)):       
00178                                                  self.CanviaElements( seleccio, num )
00179                                                  seleccio = num
00180 
00181                                    if eventhandle.keyUp('RETURN'):                                                                   
00182                                           self.PosaPrimer( seleccio )
00183 
00184                             else:
00185                                    if eventhandle.keyUp('RETURN'):                  
00186                                           if estat == 0: self.refa_cats();                                             
00187                                           return
00188               
00189                      # Animem el fons
00190                      ypos += 1
00191                      ypos %= Global.screen_y
00192 
00193                      # Pintem el fons animat
00194                      mou_fons += 8
00195                      for num in range(0, 768):
00196                             self.game.screen.blit( self.fons, (cos((float(mou_fons +num)) / 100.0) * 20, num), (0, (ypos + num) % 768, 1024, 1) )
00197 
00198                      self.game.screen.blit( self.mascara_de_fons, (0, 0) )
00199                      self.game.screen.blit( self.sel_quadres, (0, 0) )
00200 
00201 
00202                      nelements = len(self.cp) if estat == 0 else 6
00203 
00204                      posact= 220
00205                      for compta in range(primer_element_a_la_vista, nelements):     
00206                             if posact + self.sfc_preguntes[compta].get_height() > (768 -80)       :
00207                                    break
00208                      
00209                             if compta == seleccio:
00210                                    self.game.screen.fill( (64,64,64), (100, posact, 300, self.sfc_preguntes[compta].get_height() +3 ) )
00211 
00212                             if compta < 6 :
00213                                    self.game.screen.blit( self.sfc_nombres[compta], (120, posact-3) )
00214 
00215                             darrer_element_a_la_vista = compta
00216                             self.game.screen.blit( self.sfc_preguntes[compta], ( 160,posact ))    
00217 
00218                             posact += self.sfc_preguntes[compta].get_height() + 20
00219 
00220                      if primer_element_a_la_vista > 0: 
00221                             self.game.screen.blit( self.sel_fletxap, ( 386,216 + 10 + cos(time.time() * 10) * 10))
00222 
00223                      if darrer_element_a_la_vista < nelements - 1:
00224                             self.game.screen.blit( self.sel_fletxab, ( 386, 651 - 10 - cos(time.time()*10) * 10 ))
00225 
00226                      if darrer_element_a_la_vista < seleccio:  primer_element_a_la_vista += 1
00227 
00228                      if primer_element_a_la_vista > seleccio:  primer_element_a_la_vista -= 1
00229 
00230                                           
00231                      self.game.screen.blit( self.sfc_preguntes[seleccio], ( 475, 220 ))    
00232 
00233                      if seleccio != self.darrera_info:  
00234                             self.sfc_text_info0 = render_text( self.cp[seleccio].authors, self.selcat_color_text, 14, 1, '', 220 )
00235                             self.sfc_text_info1 = render_text( self.cp[seleccio].description, self.selcat_color_text, 16, 1, '', 350 )
00236                             self.sfc_text_info2 = render_text( self.cp[seleccio].players, self.selcat_color_text, 16, 1, '', 350 )
00237                             self.sfc_text_info3 = render_text( u"N. Pregutes: " + str(len(self.cp[seleccio])), self.selcat_color_text, 16, 1, '', 350 )
00238                             self.sfc_text_info4 = render_text( u"Idioma: " + self.cp[compta].language, self.selcat_color_text, 16, 1, '', 100 )
00239                             self.sfc_text_info5 = render_text( u"Data creació: " + time.strftime('%d/%m/%Y', time.gmtime(self.cp[compta].time[0])), self.selcat_color_text, 16, 1, '', 350 )
00240                             self.sfc_text_info6 = render_text( u"Data darrera modificació: " + time.strftime('%d/%m/%Y', time.gmtime(self.cp[seleccio].time[1])), self.selcat_color_text, 16, 1, '', 350 )
00241 
00242                             self.sfc_cat = loadImage( self.cp[seleccio].image )
00243                             if seleccio < 6:
00244                                    sfcmask = loadImage( 'filtre_c' + str(seleccio+1) + '.png' )
00245                                    self.sfc_cat.blit( sfcmask, (0,0))
00246                             self.sfc_cat = pygame.transform.scale(self.sfc_cat, (184, 138) )
00247 
00248                             self.darrera_info = seleccio
00249                      
00250                      self.game.screen.blit( self.sfc_text_info0, ( 475, 305 ))      
00251                      self.game.screen.blit( self.sfc_text_info1, ( 490, 380 ))      
00252                      self.game.screen.blit( self.sfc_text_info2, ( 490, 495 ))      
00253                      self.game.screen.blit( self.sfc_text_info3, ( 490, 606 ))      
00254                      self.game.screen.blit( self.sfc_text_info4, ( 765, 606 ))      
00255                      self.game.screen.blit( self.sfc_text_info5, ( 490, 635 ))      
00256                      self.game.screen.blit( self.sfc_text_info6, ( 490, 655 ))      
00257        
00258                      self.game.screen.blit( self.sfc_cat, ( 697, 221 ))      
00259                      
00260                      if mostra_ajuda: self.game.screen.blit( self.help_overlay, (0,0))
00261                      if mostra_credits: self.game.screen.blit( self.game.sfc_credits, (0,0))
00262                      
00263                      self.game.screen.blit( self.sel_reflexos, (0, 0) )
00264 
00265                      frate.next( self.game.screen )
00266                      
00267                      # Exchange self.game.screen buffers
00268                      pygame.display.flip()
00269 
00270               return 0