diff --git a/TODO b/TODO index 95187a7..c2d4877 100644 --- a/TODO +++ b/TODO @@ -2,3 +2,4 @@ Stuff left: 1) Add sound and music. 2) Implement the defeat condition with the enemies. 3) Center the letters in ScoreState and the story labels in MenuState. + 4) Add time to player after scoring. diff --git a/db/scores.db b/db/scores.db index adf54c8..50c30d2 100644 Binary files a/db/scores.db and b/db/scores.db differ diff --git a/ingame.py b/ingame.py index 87a6124..ce2377e 100644 --- a/ingame.py +++ b/ingame.py @@ -16,6 +16,7 @@ import player import background import imloader import actor +import database from state import BaseState, VALID_STATES class InGameState(BaseState): @@ -435,6 +436,7 @@ class InGameState(BaseState): npc.make_invisible() self.create_explosion(npc.get_position()) player.PLAYERS[1].inc_score_by_one() + self.time_left += 1 # If the npc exploded this turn, remove it. if not npc.is_visible(): @@ -470,8 +472,10 @@ class InGameState(BaseState): self.npcs.clear() player.PLAYERS[1].revive() - if not self.cancel: - # Register scores only if the game ended cleanly. + + database.cursor.execute('SELECT * FROM score ORDER BY score ASC') + row = database.cursor.fetchone() + if player.PLAYERS[1].get_score() > row[2]: self.next_transition = VALID_STATES['SCORE'] else: self.next_transition = VALID_STATES['MENU'] @@ -508,25 +512,25 @@ class InGameState(BaseState): # Blit everything to the bacground. # Sort npcs by Y coordinate and draw. # The idea is to draw npcs near the bottom edge of the screen last. - #npc_list = list(self.npcs) - #if player.PLAYERS[1].is_alive(): - # npc_list.append(self.player) - #sorted(npc_list, key = lambda npc: npc.get_position()[1]) - #for npc in npc_list: - # npc.draw(self.game_area) - for npc in self.npcs: - npc.draw(self.game_area) - + npc_list = list(self.npcs) if player.PLAYERS[1].is_alive(): - self.player.draw(self.game_area) + npc_list.append(self.player) + npc_list = sorted(npc_list, key = lambda npc: npc.get_position()[1]) + for npc in npc_list: + npc.draw(self.game_area) + #for npc in self.npcs: + # npc.draw(self.game_area) + + #if player.PLAYERS[1].is_alive(): + # self.player.draw(self.game_area) # Same idea here. - #expl_list = list(self.explosions) - #sorted(expl_list, key = lambda explosion: explosion.get_position()[1]) - #for explosion in expl_list: - # explosion.draw(self.game_area) - for explosion in self.explosions: + expl_list = list(self.explosions) + expl_list = sorted(expl_list, key = lambda explosion: explosion.get_position()[1]) + for explosion in expl_list: explosion.draw(self.game_area) + #for explosion in self.explosions: + # explosion.draw(self.game_area) self.text_box.fill((128, 128, 128)) self.text_box.blit(self.score_text, (5, 5)) diff --git a/menu.py b/menu.py index 413d994..83e88b3 100644 --- a/menu.py +++ b/menu.py @@ -9,6 +9,7 @@ except ImportError: android = None import database +import player from constants import DEBUG from imloader import cached_image_loader from actor import BaseActor @@ -146,17 +147,22 @@ class MenuState(BaseState): def reload_scores(self): # Reload the scores from the database. - for row in database.cursor.execute('SELECT * FROM score ORDER BY _id'): - if row[0] == 1: + database.cursor.execute('SELECT * FROM score ORDER BY _id') + rows = database.cursor.fetchall() + rows = sorted(rows, key = lambda row: row[2], reverse = True) + i = 1 + for row in rows: + if i == 1: self.score_1 = self.font.render("1) " + row[1] + " . . . . . . . . . " + str(max(row[2], 0)), True, (0, 0, 0)) - elif row[0] == 2: + elif i == 2: self.score_2 = self.font.render("2) " + row[1] + " . . . . . . . . . " + str(max(row[2], 0)), True, (0, 0, 0)) - elif row[0] == 3: + elif i == 3: self.score_3 = self.font.render("3) " + row[1] + " . . . . . . . . . " + str(max(row[2], 0)), True, (0, 0, 0)) - elif row[0] == 4: + elif i == 4: self.score_4 = self.font.render("4) " + row[1] + " . . . . . . . . . " + str(max(row[2], 0)), True, (0, 0, 0)) else: self.score_5 = self.font.render("5) " + row[1] + " . . . . . . . . . " + str(max(row[2], 0)), True, (0, 0, 0)) + i += 1 def input(self): for event in pygame.event.get(): @@ -182,6 +188,8 @@ class MenuState(BaseState): if android is None: pygame.mouse.set_visible(True) + player.PLAYERS[1].reset_score() + if self.next_transition != VALID_STATES['QUIT']: if self.next_transition != VALID_STATES['STAY']: # Set next_transition to STAY if the game gets to this state from ScoreState. diff --git a/score.py b/score.py index 5baffb0..b1e41fb 100644 --- a/score.py +++ b/score.py @@ -119,10 +119,13 @@ class ScoreState(BaseState): 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. - score = (str(self.player_init[0] + self.player_init[1] + self.player_init[2]), player.PLAYERS[1].get_score()) - 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.cursor.execute('SELECT * FROM score ORDER BY score ASC') + row = database.cursor.fetchone() + score = (str(self.player_init[0] + self.player_init[1] + self.player_init[2]), + player.PLAYERS[1].get_score(), + row[0]) + database.cursor.execute('UPDATE score SET player_name = ?, score = ? WHERE _id = ?', score) database.scores.commit() - player.PLAYERS[1].reset_score() # Don't forget to reset the initials list. self.player_init = [] self.letter_index = 0