From aa874f266fa12a09bb20491f5f5a0259e9e77eed Mon Sep 17 00:00:00 2001 From: Miguel Angel Astor Romero Date: Sat, 12 Jan 2013 10:55:35 -0430 Subject: [PATCH] Created and tested the CachedImageLoader class. --- imloader.py | 34 ++++++++++++++++++++++++++++++++++ intro.py | 14 ++++++++------ main.py | 5 ++--- particle.py | 3 ++- 4 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 imloader.py diff --git a/imloader.py b/imloader.py new file mode 100644 index 0000000..d099d86 --- /dev/null +++ b/imloader.py @@ -0,0 +1,34 @@ +############################################ +# Created on 1-12-2013. Miguel Angel Astor # +############################################ +import pygame + +class CachedImageLoader: + def __init__(self): + self.image_cache = {} + + def load_image(self, path): + if path in self.image_cache: + return self.image_cache[path] + else: + image = pygame.image.load(path) + self.image_cache[path] = image + return image + + def get_image_to_screen_percent(self, path): + if path in self.image_cache: + return self.image_cache[path] + else: + image = self.load_image(path) + + screen_prop = (float(image.get_height()) / 768.0) + screen_fract = (float(pygame.display.Info().current_h) * screen_prop) / 768.0 + scale_factor = screen_fract / screen_prop + + size = (int(image.get_width() * scale_factor), int(image.get_height() * scale_factor)) + ret_image = pygame.transform.smoothscale(image, size) + + self.image_cache[path] = ret_image + return ret_image + +cached_image_loader = CachedImageLoader() diff --git a/intro.py b/intro.py index 4ea2824..4aa3c6d 100644 --- a/intro.py +++ b/intro.py @@ -14,6 +14,7 @@ from state import BaseState, VALID_STATES import actor import game import particle +import imloader from constants import DEBUG class IntroState(BaseState): @@ -25,19 +26,21 @@ class IntroState(BaseState): self.screen_vertical_half = pygame.display.Info().current_h / 2 - image = pygame.image.load('gfx/burbuja.png') + image = imloader.cached_image_loader.get_image_to_screen_percent('gfx/burbuja.png') + image2 = imloader.cached_image_loader.get_image_to_screen_percent('gfx/submarino1.png') + image3 = imloader.cached_image_loader.get_image_to_screen_percent('gfx/oneoop.png') + self.sine_movement = actor.BulletActor(0, image, "SineMovement", False, True, False) - self.sine_movement.set_position([-300, pygame.display.Info().current_h / 2]) + self.sine_movement.set_position([-(image2.get_width() / 2 + 10), pygame.display.Info().current_h / 2]) # The next line calculates the horizontal velocity of sine_movement. # We want it to cover the width of the screen plus the width of the submarine sprite # in 20 seconds. We divide by 60 to obtain the speed in pixels per frame. - x_velocity = (float(pygame.display.Info().current_w + 600) / 20.0) / 60.0 + x_velocity = (float(pygame.display.Info().current_w + image2.get_width()) / 20.0) / 60.0 self.sine_movement.set_velocity([0.5, 0]) self.sine_movement.move() - image2 = pygame.image.load('gfx/submarino1.png') self.submarine = actor.BaseActor(1, image2, "Submarine", True, True, False) - self.submarine.set_image_point_xy(117, 284) + self.submarine.set_image_point_xy(int(float(image2.get_width()) * 0.195), int(float(image2.get_height()) * 0.835)) # Instert second animation frame of the subamrine. self.particle_system = particle.ParticleSystem(0, "Bubbles", 'gfx/burbuja.png', 1000, 1000, 1, -130.0) @@ -46,7 +49,6 @@ class IntroState(BaseState): self.particle_system.set_max_velocity(5.0) self.particle_system.start() - image3 = pygame.image.load('gfx/oneoop.png') self.oneoop_logo = actor.BaseActor(2, image3, "1-Oop logo", False, True, False) self.oneoop_logo.set_position([10 + (image3.get_width() / 2), pygame.display.Info().current_h - 10 - (image3.get_height() / 2)]) diff --git a/main.py b/main.py index 8b602b2..9f5d3da 100755 --- a/main.py +++ b/main.py @@ -28,10 +28,9 @@ def main(): pygame.display.Info().current_h) screen = pygame.display.set_mode(screen_size) else: - # If not on Android, default to a 800x600 pixels screen. - screen = pygame.display.set_mode((1024, 768), + screen = pygame.display.set_mode((800, 600), pygame.FULLSCREEN | pygame.HWSURFACE) - pygame.display.set_caption("Super HUGS Revolution 98") + pygame.display.set_caption("Super HUGS Revolution 98") pygame.mouse.set_visible(False) # Create the game object and start the main game loop. diff --git a/particle.py b/particle.py index 62032cd..afb008b 100644 --- a/particle.py +++ b/particle.py @@ -7,6 +7,7 @@ import random import pygame import math_utils +import imloader class Particle(pygame.sprite.Sprite): def __init__(self, lifespan, scale, texture, gravity = [0.0, 9.8], position = [0,0], initial_vel = [100.0, 100.0], friction = 1.0, frame_rate = 60.0): @@ -86,7 +87,7 @@ class ParticleSystem: self.angle = angle self.working = False self.particles = set() - self.texture = pygame.image.load(texture_filename) + self.texture = imloader.cached_image_loader.get_image_to_screen_percent(texture_filename) self.part_creation_accum = 0.0 self.then = pygame.time.get_ticks()