From 262abf254476188f89af3cb22ca1c46a3d40599d Mon Sep 17 00:00:00 2001 From: Miguel Angel Astor Romero Date: Wed, 16 Jan 2013 08:05:28 -0430 Subject: [PATCH] Added the player object. --- constants.py | 3 +++ game.py | 6 ++++-- ingame.py | 6 ++++-- player.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ score.py | 4 ++-- 5 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 player.py diff --git a/constants.py b/constants.py index 21d4716..962de2a 100644 --- a/constants.py +++ b/constants.py @@ -4,3 +4,6 @@ # Debug constant. Set to False to turn off debugging messages. DEBUG = False + +# Number of players in the game. Should always be greater than or equal to one. +NUM_PLAYERS = 1 diff --git a/game.py b/game.py index 9709575..94e5d94 100644 --- a/game.py +++ b/game.py @@ -1,5 +1,6 @@ -# Miguel Angel Astor Romero. Created on 7-1-2013. # -################################################### +########################################### +# Created on 1-7-2013. Miguel Angel Astor # +########################################### import pygame try: @@ -7,6 +8,7 @@ try: except ImportError: android = None +import player from state import VALID_STATES from intro import IntroState from menu import MenuState diff --git a/ingame.py b/ingame.py index 2db7eba..1fa0d77 100644 --- a/ingame.py +++ b/ingame.py @@ -1,5 +1,6 @@ -# Miguel Angel Astor Romero. Created on 7-1-2013. # -################################################### +########################################### +# Created on 1-7-2013. Miguel Angel Astor # +########################################### import pygame try: @@ -7,6 +8,7 @@ try: except ImportError: android = None +import player from state import BaseState, VALID_STATES class InGameState(BaseState): diff --git a/player.py b/player.py new file mode 100644 index 0000000..a41886e --- /dev/null +++ b/player.py @@ -0,0 +1,60 @@ +############################################ +# Created on 1-16-2013. Miguel Angel Astor # +############################################ +import constants + +class Player: + def __init__(self, id): + self.id = id + self.score = 0 + self.alive = True + self.clamp_to_zero = True + + def get_id(): + return self.id + + def is_alive(self): + return self.alive + + def set_alive(self, alive): + self.alive = alive + + def revive(self): + self.alive = True + + def kill(self): + self.alive = False + + def clamps_to_zero(self): + return self.clamp_to_zero + + def set_clamp_to_zero(self, clamp): + self.clamp_to_zero = clamp + + def get_score(self): + return self.score + + def inc_score(self, amount): + self.score += amount + + def inc_score_by_one(self): + self.score += 1 + + def dec_score(self, amount): + if self.clamp_to_zero: + self.score = max(self.score - amount, 0) + else: + self.score -= amount + + def dec_score_by_one(self): + self.dec_score(1) + + def set_score(self, value): + self.score = value + + def reset_score(self): + self.score = 0 + +PLAYERS = {} +for i in range(constants.NUM_PLAYERS): + players[i + 1] = Player(i + 1) diff --git a/score.py b/score.py index cbba41a..a0f7f0d 100644 --- a/score.py +++ b/score.py @@ -9,6 +9,7 @@ try: except ImportError: android = None +import player import database from constants import DEBUG from imloader import cached_image_loader @@ -118,8 +119,7 @@ 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. - # 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) + 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.scores.commit() # Don't forget to reset the initials list.