From 1b879f423b382ce63662e6218a8ff5859f2306a6 Mon Sep 17 00:00:00 2001 From: Miguel Angel Astor Romero Date: Tue, 15 Jan 2013 22:13:26 -0430 Subject: [PATCH] Implemented ScoreState. Assorted fixes to BaseActor and MenuState. --- actor.py | 15 ++++++ menu.py | 14 ++++-- score.py | 137 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 152 insertions(+), 14 deletions(-) diff --git a/actor.py b/actor.py index 7be5ca7..44fb074 100644 --- a/actor.py +++ b/actor.py @@ -50,6 +50,18 @@ class BaseActor(pygame.sprite.Sprite): def is_visible(self): return self.visible + def set_visible(self, visible): + self.visible = visible + + def toggle_visible(self): + self.visible = not self.visible + + def make_visible(self): + self.visible = True + + def make_invisible(self): + self.visible = False + def is_solid(self): return self.solid @@ -103,6 +115,9 @@ class BaseActor(pygame.sprite.Sprite): def set_image_point_xy(self, point_x, point_y): self.image_points.append((point_x, point_y)) + def test_collision_with_point(self, point): + return self.rect.collidepoint(point[0], point[1]) + def draw(self, canvas): if self.image is not None: canvas.blit(self.image, self.rect) diff --git a/menu.py b/menu.py index b24e2cf..413d994 100644 --- a/menu.py +++ b/menu.py @@ -28,6 +28,8 @@ class MenuState(BaseState): self.cursor_x = 0 self.cursor_y = 0 + + self.background_color = (125, 158, 192) # Load main menu buttons. image = cached_image_loader.get_image_to_screen_percent('gfx/logo.png') @@ -144,7 +146,7 @@ class MenuState(BaseState): def reload_scores(self): # Reload the scores from the database. - for row in database.scores.execute('SELECT * FROM score ORDER BY _id'): + for row in database.cursor.execute('SELECT * FROM score ORDER BY _id'): if row[0] == 1: self.score_1 = self.font.render("1) " + row[1] + " . . . . . . . . . " + str(max(row[2], 0)), True, (0, 0, 0)) elif row[0] == 2: @@ -158,7 +160,7 @@ class MenuState(BaseState): def input(self): for event in pygame.event.get(): - if android: + if android is not None: if android.check_pause(): android.wait_for_resume() @@ -184,6 +186,12 @@ class MenuState(BaseState): if self.next_transition != VALID_STATES['STAY']: # Set next_transition to STAY if the game gets to this state from ScoreState. self.next_transition = VALID_STATES['STAY'] + # Reset the scores label to force a database reload. + self.score_1 = None + self.score_2 = None + self.score_3 = None + self.score_4 = None + self.score_5 = None if self.current_menu == MENUS['MAIN']: # Check for mouse (tap) collisions with the main menu buttons. @@ -225,7 +233,7 @@ class MenuState(BaseState): # Keep tracking time. self.then = now - # Recenter the mouse pointer. + # Reset the mouse pointer. self.cursor_x = 0 self.cursor_y = 0 diff --git a/score.py b/score.py index da6e8e6..cbba41a 100644 --- a/score.py +++ b/score.py @@ -1,29 +1,83 @@ ########################################### # Created on 1-7-2013. Miguel Angel Astor # ########################################### -# Update score database: -# UPDATE score SET player_name = ?, score = ? WHERE _id IN (SELECT _id FROM score WHERE score IN (SELECT MIN(score) FROM score)) -import pygame +import string +import pygame try: import android except ImportError: android = None +import database +from constants import DEBUG +from imloader import cached_image_loader +from actor import BaseActor from state import BaseState, VALID_STATES class ScoreState(BaseState): def __init__(self): BaseState.__init__(self) - self.count = 0 - screen_center = self.get_screen_center() - self.rectangle = pygame.Rect(screen_center[0] - 50, screen_center[1] - 50, 100, 100) + self.background_color = (125, 158, 192) self.next_transition = VALID_STATES['STAY'] + 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, + self.banner.get_position()[1] + image.get_height()]) + 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 + + self.letter_y = int((float(pygame.display.Info().current_h) * 240.0 ) / 768.0) + def input(self): for event in pygame.event.get(): - if android: + if android is not None: if android.check_pause(): android.wait_for_resume() @@ -33,16 +87,77 @@ class ScoreState(BaseState): if event.type == pygame.QUIT: self.next_transition = VALID_STATES['QUIT'] + # Catch the position of a mouse click (or tap). + if event.type == pygame.MOUSEBUTTONDOWN: + (self.cursor_x, self.cursor_y) = event.pos + def update(self): if self.next_transition != VALID_STATES['QUIT']: - if self.count < 120: - self.count += 1 + 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. self.next_transition = VALID_STATES['STAY'] + + 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() else: - self.count = 0 + 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. + # TODO: Replace the 0 on the following line with player.get_score() + score = (str(self.player_init[0] + self.player_init[1] + self.player_init[2]), 0) + 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 self.next_transition = VALID_STATES['MENU'] + + # Reset the mouse pointer. + self.cursor_x = 0 + self.cursor_y = 0 + return self.next_transition def render(self, canvas): canvas.fill(self.background_color) - pygame.draw.rect(canvas, (255, 255, 0), self.rectangle) + + 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: + position = (self.underscore_l.rect.left, self.letter_y - initial.get_height()) + elif i == 1: + position = (self.underscore_c.rect.left, self.letter_y - initial.get_height()) + else: + position = (self.underscore_r.rect.left, self.letter_y - initial.get_height()) + canvas.blit(initial, position)