2013-01-15 13:53:40 -04:30
###########################################
# Created on 1-7-2013. Miguel Angel Astor #
###########################################
2013-01-15 22:13:26 -04:30
import string
2013-01-07 20:22:23 -04:30
2013-01-15 22:13:26 -04:30
import pygame
2013-01-07 20:22:23 -04:30
try :
import android
except ImportError :
android = None
2013-01-16 08:05:28 -04:30
import player
2013-01-15 22:13:26 -04:30
import database
from constants import DEBUG
from imloader import cached_image_loader
from actor import BaseActor
2013-01-07 20:22:23 -04:30
from state import BaseState , VALID_STATES
class ScoreState ( BaseState ) :
def __init__ ( self ) :
2013-01-09 22:34:15 -04:30
BaseState . __init__ ( self )
2013-01-15 22:13:26 -04:30
self . background_color = ( 125 , 158 , 192 )
2013-01-09 22:34:15 -04:30
self . next_transition = VALID_STATES [ ' STAY ' ]
2013-01-15 22:13:26 -04:30
self . cursor_x = 0
self . cursor_y = 0
self . letter_index = 0 # Tells how many letters the user has clicked.
self . player_init = [ ] # Holds the player initials.
image = cached_image_loader . get_image_to_screen_percent ( ' gfx/iniciales.png ' )
self . banner = BaseActor ( 0 , image , " Banner " , False , True , False )
self . banner . set_position ( [ pygame . display . Info ( ) . current_w / / 2 , ( image . get_height ( ) / / 2 ) + 20 ] )
image2 = cached_image_loader . get_image_to_screen_percent ( ' gfx/Fuente/_.png ' )
self . underscore_c = BaseActor ( 1 , image2 , " Underscore center " , False , True , False )
self . underscore_c . set_position ( [ pygame . display . Info ( ) . current_w / / 2 ,
2013-01-16 13:45:12 -04:30
self . banner . get_position ( ) [ 1 ] + image . get_height ( ) + 25 ] )
2013-01-15 22:13:26 -04:30
self . underscore_l = BaseActor ( 2 , image2 , " Underscore left " , False , True , False )
self . underscore_l . set_position ( [ self . underscore_c . get_position ( ) [ 0 ] - image2 . get_width ( ) ,
self . underscore_c . get_position ( ) [ 1 ] ] )
self . underscore_r = BaseActor ( 3 , image2 , " Underscore right " , False , True , False )
self . underscore_r . set_position ( [ self . underscore_c . get_position ( ) [ 0 ] + image2 . get_width ( ) ,
self . underscore_c . get_position ( ) [ 1 ] ] )
image = cached_image_loader . get_image_to_screen_percent ( ' gfx/del.png ' )
self . del_button = BaseActor ( 4 , image , " Delete button " , False , True , False )
self . del_button . set_position ( [ self . underscore_c . get_position ( ) [ 0 ] + ( 2 * image2 . get_width ( ) ) ,
self . underscore_c . get_position ( ) [ 1 ] ] )
image = cached_image_loader . get_image_to_screen_percent ( ' gfx/listo.png ' )
self . done_button = BaseActor ( 5 , image , " Done button " , False , False , False )
self . done_button . set_position ( [ self . underscore_c . get_position ( ) [ 0 ] + ( 3 * image2 . get_width ( ) ) ,
self . underscore_c . get_position ( ) [ 1 ] ] )
self . letters = { }
letter_list = [ ' q ' , ' w ' , ' e ' , ' r ' , ' t ' , ' y ' , ' u ' , ' i ' , ' o ' , ' p ' , ' a ' , ' s ' , ' d ' , ' f ' , ' g ' , ' h ' ,
' j ' , ' k ' , ' l ' , ' z ' , ' x ' , ' c ' , ' v ' , ' b ' , ' n ' , ' m ' ]
q_x_position = int ( ( float ( pygame . display . Info ( ) . current_w ) * 88.0 ) / 1024.0 )
q_y_position = int ( ( float ( pygame . display . Info ( ) . current_h ) * 438.0 ) / 768.0 )
letter_sep = int ( ( float ( pygame . display . Info ( ) . current_w ) * 10.0 ) / 1024.0 )
for l in letter_list :
image = cached_image_loader . get_image_to_screen_percent ( ' gfx/Fuente/ ' + l + ' .png ' )
letter_actor = BaseActor ( 89 , image , string . upper ( l ) , False , True , False )
if l == ' a ' :
q_x_position = int ( ( float ( pygame . display . Info ( ) . current_w ) * 154.0 ) / 1024.0 )
q_y_position = int ( ( float ( pygame . display . Info ( ) . current_h ) * 543.0 ) / 768.0 )
elif l == ' z ' :
q_x_position = int ( ( float ( pygame . display . Info ( ) . current_w ) * 199.0 ) / 1024.0 )
q_y_position = int ( ( float ( pygame . display . Info ( ) . current_h ) * 649.0 ) / 768.0 )
letter_actor . set_position ( [ q_x_position , q_y_position ] )
self . letters [ l ] = letter_actor
q_x_position + = image . get_width ( ) + letter_sep
2013-01-16 13:45:12 -04:30
self . letter_y = int ( ( float ( pygame . display . Info ( ) . current_h ) * 265.0 ) / 768.0 )
2013-01-15 22:13:26 -04:30
2013-01-07 20:22:23 -04:30
def input ( self ) :
2013-01-09 22:34:15 -04:30
for event in pygame . event . get ( ) :
2013-01-15 22:13:26 -04:30
if android is not None :
2013-01-09 22:34:15 -04:30
if android . check_pause ( ) :
android . wait_for_resume ( )
if event . type == pygame . KEYDOWN :
if event . key == pygame . K_ESCAPE :
2013-01-07 20:22:23 -04:30
self . next_transition = VALID_STATES [ ' QUIT ' ]
2013-01-09 22:34:15 -04:30
if event . type == pygame . QUIT :
self . next_transition = VALID_STATES [ ' QUIT ' ]
2013-01-07 20:22:23 -04:30
2013-01-15 22:13:26 -04:30
# Catch the position of a mouse click (or tap).
if event . type == pygame . MOUSEBUTTONDOWN :
( self . cursor_x , self . cursor_y ) = event . pos
2013-01-07 20:22:23 -04:30
def update ( self ) :
2013-01-09 22:34:15 -04:30
if self . next_transition != VALID_STATES [ ' QUIT ' ] :
2013-01-15 22:13:26 -04:30
if self . next_transition != VALID_STATES [ ' STAY ' ] :
# Set next_transition to STAY if the game gets to this state from GameState a second or third time, etc.
2013-01-09 22:34:15 -04:30
self . next_transition = VALID_STATES [ ' STAY ' ]
2013-01-15 22:13:26 -04:30
if self . letter_index < 3 :
# If not all initials are set, check taps on every letter.
for key in self . letters . keys ( ) :
if self . letters [ key ] . test_collision_with_point ( ( self . cursor_x , self . cursor_y ) ) :
self . player_init . append ( self . letters [ key ] . get_name ( ) )
self . letter_index + = 1
if self . letter_index > 0 and self . del_button . test_collision_with_point ( ( self . cursor_x , self . cursor_y ) ) :
# If the player clicked on the delete button and there are initials set,
# remove the last one.
self . player_init . pop ( )
self . letter_index - = 1
if self . letter_index == 3 :
# If all initials have been set, make the done button visible.
self . done_button . make_visible ( )
2013-01-09 22:34:15 -04:30
else :
2013-01-15 22:13:26 -04:30
self . done_button . make_invisible ( )
if self . done_button . is_visible ( ) and self . done_button . test_collision_with_point ( ( self . cursor_x , self . cursor_y ) ) :
# If the user clicked on the done button, insert the score in the database and go to the main menu.
2013-01-16 08:05:28 -04:30
score = ( str ( self . player_init [ 0 ] + self . player_init [ 1 ] + self . player_init [ 2 ] ) , player . PLAYERS [ 1 ] . get_score ( ) )
2013-01-15 22:13:26 -04:30
database . cursor . execute ( ' UPDATE score SET player_name = ?, score = ? WHERE _id IN (SELECT _id FROM score WHERE score IN (SELECT MIN(score) FROM score)) ' , score )
database . scores . commit ( )
# Don't forget to reset the initials list.
self . player_init = [ ]
self . letter_index = 0
2013-01-09 22:34:15 -04:30
self . next_transition = VALID_STATES [ ' MENU ' ]
2013-01-15 22:13:26 -04:30
# Reset the mouse pointer.
self . cursor_x = 0
self . cursor_y = 0
2013-01-09 22:34:15 -04:30
return self . next_transition
2013-01-07 20:22:23 -04:30
def render ( self , canvas ) :
2013-01-09 22:34:15 -04:30
canvas . fill ( self . background_color )
2013-01-15 22:13:26 -04:30
self . banner . draw ( canvas )
if self . letter_index < 1 :
self . underscore_l . draw ( canvas )
if self . letter_index < 2 :
self . underscore_c . draw ( canvas )
if self . letter_index < 3 :
self . underscore_r . draw ( canvas )
self . del_button . draw ( canvas )
if self . done_button . is_visible ( ) :
self . done_button . draw ( canvas )
for key in self . letters . keys ( ) :
self . letters [ key ] . draw ( canvas )
for i in range ( self . letter_index ) :
initial = self . letters [ string . lower ( self . player_init [ i ] ) ] . image
position = None
if i == 0 :
2013-01-16 13:45:12 -04:30
position = ( self . underscore_l . rect . left , self . letter_y - ( initial . get_height ( ) / / 2 ) )
2013-01-15 22:13:26 -04:30
elif i == 1 :
2013-01-16 13:45:12 -04:30
position = ( self . underscore_c . rect . left , self . letter_y - ( initial . get_height ( ) / / 2 ) )
2013-01-15 22:13:26 -04:30
else :
2013-01-16 13:45:12 -04:30
position = ( self . underscore_r . rect . left , self . letter_y - ( initial . get_height ( ) / / 2 ) )
2013-01-15 22:13:26 -04:30
canvas . blit ( initial , position )