From e742f9c19d610c2aa79b6a0f4ee1a7986cd5a22c Mon Sep 17 00:00:00 2001 From: Miguel Angel Astor Romero Date: Wed, 9 Jan 2013 22:34:15 -0430 Subject: [PATCH] Moved background color to BaseState. --- ingame.py | 45 ++++++++++++++++++++++++--------------------- menu.py | 45 ++++++++++++++++++++++++--------------------- notvalidstate.py | 32 +++++++++++++++++--------------- score.py | 45 ++++++++++++++++++++++++--------------------- state.py | 5 ++++- 5 files changed, 93 insertions(+), 79 deletions(-) diff --git a/ingame.py b/ingame.py index b7cd494..2db7eba 100644 --- a/ingame.py +++ b/ingame.py @@ -11,32 +11,35 @@ from state import BaseState, VALID_STATES class InGameState(BaseState): def __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.next_transition = VALID_STATES['STAY'] + 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.next_transition = VALID_STATES['STAY'] def input(self): - for event in pygame.event.get(): - if android: - if android.check_pause(): - android.wait_for_resume() + for event in pygame.event.get(): + if android: + if android.check_pause(): + android.wait_for_resume() - if event.type == pygame.KEYDOWN: - if event.key == pygame.K_ESCAPE: - self.next_transition = VALID_STATES['QUIT'] - if event.type == pygame.QUIT: + if event.type == pygame.KEYDOWN: + if event.key == pygame.K_ESCAPE: self.next_transition = VALID_STATES['QUIT'] + if event.type == pygame.QUIT: + self.next_transition = VALID_STATES['QUIT'] def update(self): - if self.next_transition != VALID_STATES['QUIT']: - if self.count < 120: - self.count += 1 - self.next_transition = VALID_STATES['STAY'] - else: - self.count = 0 - self.next_transition = VALID_STATES['SCORE'] - return self.next_transition + if self.next_transition != VALID_STATES['QUIT']: + if self.count < 120: + self.count += 1 + self.next_transition = VALID_STATES['STAY'] + else: + self.count = 0 + self.next_transition = VALID_STATES['SCORE'] + return self.next_transition def render(self, canvas): - pygame.draw.rect(canvas, (255, 0, 255), self.rectangle) + canvas.fill(self.background_color) + pygame.draw.rect(canvas, (255, 0, 255), self.rectangle) diff --git a/menu.py b/menu.py index be8dc58..f48dcfb 100644 --- a/menu.py +++ b/menu.py @@ -11,32 +11,35 @@ from state import BaseState, VALID_STATES class MenuState(BaseState): def __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.next_transition = VALID_STATES['STAY'] + 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.next_transition = VALID_STATES['STAY'] def input(self): - for event in pygame.event.get(): - if android: - if android.check_pause(): - android.wait_for_resume() + for event in pygame.event.get(): + if android: + if android.check_pause(): + android.wait_for_resume() - if event.type == pygame.KEYDOWN: - if event.key == pygame.K_ESCAPE: - self.next_transition = VALID_STATES['QUIT'] - if event.type == pygame.QUIT: + if event.type == pygame.KEYDOWN: + if event.key == pygame.K_ESCAPE: self.next_transition = VALID_STATES['QUIT'] + if event.type == pygame.QUIT: + self.next_transition = VALID_STATES['QUIT'] def update(self): - if self.next_transition != VALID_STATES['QUIT']: - if self.count < 120: - self.count += 1 - self.next_transition = VALID_STATES['STAY'] - else: - self.count = 0 - self.next_transition = VALID_STATES['IN_GAME'] - return self.next_transition + if self.next_transition != VALID_STATES['QUIT']: + if self.count < 120: + self.count += 1 + self.next_transition = VALID_STATES['STAY'] + else: + self.count = 0 + self.next_transition = VALID_STATES['IN_GAME'] + return self.next_transition def render(self, canvas): - pygame.draw.rect(canvas, (0, 255, 255), self.rectangle) + canvas.fill(self.background_color) + pygame.draw.rect(canvas, (0, 255, 255), self.rectangle) diff --git a/notvalidstate.py b/notvalidstate.py index c6e73c5..ea9ef74 100644 --- a/notvalidstate.py +++ b/notvalidstate.py @@ -11,25 +11,27 @@ from state import BaseState, VALID_STATES class NotValidState(BaseState): def __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.next_transition = VALID_STATES['STAY'] + 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.next_transition = VALID_STATES['STAY'] def input(self): - for event in pygame.event.get(): - if android: - if android.check_pause(): - android.wait_for_resume() + for event in pygame.event.get(): + if android: + if android.check_pause(): + android.wait_for_resume() - if event.type == pygame.KEYDOWN: - if event.key == pygame.K_ESCAPE: - self.next_transition = VALID_STATES['QUIT'] - if event.type == pygame.QUIT: - self.next_transition = VALID_STATES['QUIT'] + if event.type == pygame.KEYDOWN: + self.next_transition = VALID_STATES['QUIT'] + if event.type == pygame.QUIT: + self.next_transition = VALID_STATES['QUIT'] def update(self): - return self.next_transition + return self.next_transition def render(self, canvas): - pygame.draw.rect(canvas, (0, 0, 0), self.rectangle) + canvas.fill(self.background_color) + pygame.draw.rect(canvas, (0, 0, 0), self.rectangle) diff --git a/score.py b/score.py index be0076c..5651d7b 100644 --- a/score.py +++ b/score.py @@ -11,32 +11,35 @@ from state import BaseState, VALID_STATES class ScoreState(BaseState): def __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.next_transition = VALID_STATES['STAY'] + 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.next_transition = VALID_STATES['STAY'] def input(self): - for event in pygame.event.get(): - if android: - if android.check_pause(): - android.wait_for_resume() + for event in pygame.event.get(): + if android: + if android.check_pause(): + android.wait_for_resume() - if event.type == pygame.KEYDOWN: - if event.key == pygame.K_ESCAPE: - self.next_transition = VALID_STATES['QUIT'] - if event.type == pygame.QUIT: + if event.type == pygame.KEYDOWN: + if event.key == pygame.K_ESCAPE: self.next_transition = VALID_STATES['QUIT'] + if event.type == pygame.QUIT: + self.next_transition = VALID_STATES['QUIT'] def update(self): - if self.next_transition != VALID_STATES['QUIT']: - if self.count < 120: - self.count += 1 - self.next_transition = VALID_STATES['STAY'] - else: - self.count = 0 - self.next_transition = VALID_STATES['MENU'] - return self.next_transition + if self.next_transition != VALID_STATES['QUIT']: + if self.count < 120: + self.count += 1 + self.next_transition = VALID_STATES['STAY'] + else: + self.count = 0 + self.next_transition = VALID_STATES['MENU'] + return self.next_transition def render(self, canvas): - pygame.draw.rect(canvas, (255, 255, 0), self.rectangle) + canvas.fill(self.background_color) + pygame.draw.rect(canvas, (255, 255, 0), self.rectangle) diff --git a/state.py b/state.py index 645b781..fae44d9 100644 --- a/state.py +++ b/state.py @@ -7,6 +7,9 @@ VALID_STATES = { 'INTRO':0, 'MENU':1, 'IN_GAME':2, 'SCORE':3, 'STAY':4, 'QUIT':8 # Parent class for game states. class BaseState: + def __init__(self): + self.background_color = (139, 210, 228) + def input(self): """ Empty. Should handle PyGame input. """ pass @@ -17,7 +20,7 @@ class BaseState: def render(self, canvas): """ Empty. Should render this state on the canvas. """ - pass + canvas.fill(self.background_color) def get_screen_center(self): return (pygame.display.Info().current_w / 2,