Added the player object.

This commit is contained in:
2013-01-16 08:05:28 -04:30
parent 99d2b68712
commit 262abf2544
5 changed files with 73 additions and 6 deletions

View File

@@ -4,3 +4,6 @@
# Debug constant. Set to False to turn off debugging messages. # Debug constant. Set to False to turn off debugging messages.
DEBUG = False DEBUG = False
# Number of players in the game. Should always be greater than or equal to one.
NUM_PLAYERS = 1

View File

@@ -1,5 +1,6 @@
# Miguel Angel Astor Romero. Created on 7-1-2013. # ###########################################
################################################### # Created on 1-7-2013. Miguel Angel Astor #
###########################################
import pygame import pygame
try: try:
@@ -7,6 +8,7 @@ try:
except ImportError: except ImportError:
android = None android = None
import player
from state import VALID_STATES from state import VALID_STATES
from intro import IntroState from intro import IntroState
from menu import MenuState from menu import MenuState

View File

@@ -1,5 +1,6 @@
# Miguel Angel Astor Romero. Created on 7-1-2013. # ###########################################
################################################### # Created on 1-7-2013. Miguel Angel Astor #
###########################################
import pygame import pygame
try: try:
@@ -7,6 +8,7 @@ try:
except ImportError: except ImportError:
android = None android = None
import player
from state import BaseState, VALID_STATES from state import BaseState, VALID_STATES
class InGameState(BaseState): class InGameState(BaseState):

60
player.py Normal file
View File

@@ -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)

View File

@@ -9,6 +9,7 @@ try:
except ImportError: except ImportError:
android = None android = None
import player
import database import database
from constants import DEBUG from constants import DEBUG
from imloader import cached_image_loader 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 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. # 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]), player.PLAYERS[1].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.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() database.scores.commit()
# Don't forget to reset the initials list. # Don't forget to reset the initials list.