Fixed the game background image construction on Android devices.

This commit is contained in:
2013-01-17 00:32:20 -04:30
parent b5e8c04754
commit e28cd752e7
2 changed files with 21 additions and 13 deletions

View File

@@ -6,16 +6,23 @@ import pygame
import imloader import imloader
from constants import DEBUG from constants import DEBUG
class TiledBackground(): class TiledBackground():
def __init__(self, rep_x, rep_y, texture_filename, position_x = 0, position_y = 0): 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) self.position = (position_x, position_y)
texture = imloader.cached_image_loader.get_image_to_screen_percent(texture_filename) texture = imloader.cached_image_loader.get_image_to_screen_percent(texture_filename)
img_w = texture.get_width() img_w = texture.get_width()
img_h = texture.get_height() img_h = texture.get_height()
self.w = img_w * rep_x rep_x = (self.w // img_w) + 1
self.h = img_h * rep_y rep_y = (self.h // img_h) + 1
if width == -1:
rep_x = 1
self.w = img_w
if height == -1:
rep_y = 1
self.h = img_h
if DEBUG: if DEBUG:
print '(' + str(rep_x) + ', ' + str(rep_y) + ')' print '(' + str(rep_x) + ', ' + str(rep_y) + ')'

View File

@@ -27,25 +27,26 @@ class InGameState(BaseState):
self.rectangle = pygame.Rect(screen_center[0] - 50, screen_center[1] - 50, 100, 100) self.rectangle = pygame.Rect(screen_center[0] - 50, screen_center[1] - 50, 100, 100)
# Create a surface for the background. # Create a surface for the background.
bg_w = int((1280.0 * float(pygame.display.Info().current_w)) / 1024.0) bg_w = 1280
bg_h = int((1024.0 * float(pygame.display.Info().current_h)) / 768.0) bg_h = 1024
self.background = pygame.Surface((bg_w, bg_h)) self.background = pygame.Surface((bg_w, bg_h))
# Create the floor. # Create the floor.
floor = background.TiledBackground(20, 16, 'gfx/piso.png') floor = background.TiledBackground(1280, 1024, 'gfx/piso.png')
# Create the walls for the top and the bottom (the same for both). # Create the walls for the top and the bottom (the same for both).
walls_top = background.TiledBackground(15, 1, 'gfx/Pared.png') bg_h = int((80.0 * float(pygame.display.Info().current_h)) / 768.0)
bg_y = int(((1024.0 - 80.0) * float(pygame.display.Info().current_h)) / 768.0) walls_top = background.TiledBackground(1280, bg_h, 'gfx/Pared.png')
bg_y = 1024 - int((80.0 * float(pygame.display.Info().current_h)) / 768.0)
# Create the left walls. # Create the left walls.
walls_left = background.TiledBackground(1, 12, 'gfx/Pared2.png') walls_left = background.TiledBackground(-1, 1024, 'gfx/Pared2.png')
_y = int((80.0 * float(pygame.display.Info().current_h)) / 768.0) _y = int((80.0 * float(pygame.display.Info().current_h)) / 768.0)
walls_left.set_position((0, _y)) walls_left.set_position((0, _y))
# Create the right walls. # Create the right walls.
walls_right = background.TiledBackground(1, 12, 'gfx/Pared3.png') walls_right = background.TiledBackground(-1, 1024, 'gfx/Pared3.png')
_x = int(((1280.0 - 40.0) * float(pygame.display.Info().current_h)) / 768.0) _x = 1280 - int((40.0 * float(pygame.display.Info().current_w)) / 1024.0)
walls_right.set_position((_x, _y)) walls_right.set_position((_x, _y))
# Build the background image. # Build the background image.
@@ -88,5 +89,5 @@ class InGameState(BaseState):
def render(self, canvas): def render(self, canvas):
canvas.fill(self.background_color) canvas.fill(self.background_color)
canvas.blit(self.background, (0, -1024 / 2)) canvas.blit(self.background, (0, 0))
pygame.draw.rect(canvas, (255, 0, 255), self.rectangle) pygame.draw.rect(canvas, (255, 0, 255), self.rectangle)