Created and tested the CachedImageLoader class.

This commit is contained in:
2013-01-12 10:55:35 -04:30
parent 913700796b
commit aa874f266f
4 changed files with 46 additions and 10 deletions

34
imloader.py Normal file
View File

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

View File

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

View File

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

View File

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