From a928348e070d54c706e4d61efb7b66e15e32bb3a Mon Sep 17 00:00:00 2001 From: Miguel Angel Astor Romero Date: Wed, 16 Jan 2013 22:03:30 -0430 Subject: [PATCH] Implemented and tested the TiledBackground class. --- background.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ ingame.py | 27 ++++++++++++++++++++------- 2 files changed, 69 insertions(+), 7 deletions(-) create mode 100644 background.py diff --git a/background.py b/background.py new file mode 100644 index 0000000..d4edc37 --- /dev/null +++ b/background.py @@ -0,0 +1,49 @@ +############################################ +# Created on 1-16-2013. Miguel Angel Astor # +############################################ +import pygame + +import imloader +from constants import DEBUG +class TiledBackground(): + def __init__(self, width, height, texture_filename, position_x = 0, position_y = 0): + self.w = width + self.h = height + self.position = (position_x, position_y) + + texture = imloader.cached_image_loader.get_image_to_screen_percent(texture_filename) + img_w = texture.get_width() + img_h = texture.get_height() + + rep_x = (self.w // img_w) + 1 + rep_y = (self.h // img_h) + 1 + if DEBUG: + print '(' + str(rep_x) + ', ' + str(rep_y) + ')' + + self.image = pygame.Surface((self.w, self.h)) + render_x = 0 + render_y = 0 + for i in range(rep_x): + render_x = i * img_w + for j in range(rep_y): + render_y = j * img_h + self.image.blit(texture, (render_x, render_y)) + render_y = 0 + + def get_width(self): + return self.w + + def get_height(self): + return self.h + + def get_position(self): + return self.position + + def set_position(self, position): + self.position = position + + def set_position_xy(self, position_x, position_y): + self.position = (position_x, position_y) + + def draw(self, canvas): + canvas.blit(self.image, self.position) diff --git a/ingame.py b/ingame.py index 1fa0d77..6f1fd36 100644 --- a/ingame.py +++ b/ingame.py @@ -9,16 +9,22 @@ except ImportError: android = None import player +import background from state import BaseState, VALID_STATES class InGameState(BaseState): def __init__(self): BaseState.__init__(self) - self.count = 0 + self.next_transition = VALID_STATES['STAY'] + self.cursor_x = 0 + self.cursor_y = 0 + self.user_click = False + 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'] + + self.background = background.TiledBackground(1280, 1024, 'gfx/piso.png') def input(self): for event in pygame.event.get(): @@ -29,19 +35,26 @@ class InGameState(BaseState): if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: self.next_transition = VALID_STATES['QUIT'] + self.user_click = True 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 + self.user_click = True + def update(self): if self.next_transition != VALID_STATES['QUIT']: - if self.count < 120: - self.count += 1 + if self.next_transition != VALID_STATES['STAY']: self.next_transition = VALID_STATES['STAY'] - else: - self.count = 0 + + if self.user_click: self.next_transition = VALID_STATES['SCORE'] + self.user_click = False + return self.next_transition def render(self, canvas): - canvas.fill(self.background_color) + self.background.draw(canvas) pygame.draw.rect(canvas, (255, 0, 255), self.rectangle)