a-robots-conundrum/robotgame/loader.py

220 lines
8.8 KiB
Python
Raw Normal View History

# This file is part of ROBOTGAME
#
# ROBOTGAME is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# ROBOTGAME is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# ROBOTGAME. If not, see <http://www.gnu.org/licenses/>.
#
# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
#
# loader.py
# --------------------
# date created : Fri Aug 10 2012
# copyright : (C) 2012 Sakse Dalum
# maintained by : Sakse Dalum <don_s@hongabar.org>
"""
A loader object.
"""
import pygame
import os
import re
class Loader(object):
def __init__(self, game, directory):
self.__dict__.update(locals())
self.imgs = {}
def load(self):
"""Load all game resources."""
l = ['ground1', 'ground2']
for o in l:
self.imgs[o] = pygame.image.load(os.path.join(
self.directory, 'tiles', '%s.png' % o))
for o in range(1, 18):
for i in range(16, 19):
self.imgs['symbol%02d-%04d' % (o, i)] = (
pygame.image.load(os.path.join(
self.directory, 'symbols', 'blocks',
'block%02d-%04d.png' % (o, i))))
for o in range(1, 7):
self.imgs['indoor%d' % o] = pygame.image.load(os.path.join(
self.directory, 'tiles', 'indoor', 'ground%02d.png' % o))
l = ['block1', 'block1_lifted', 'block3', '../lasertarget',
'../lasersource_up', '../lasersource_down', '../lasersource_right',
'../lasertarget_glow']
for o in l:
self.imgs[os.path.basename(o)] = pygame.image.load(os.path.join(
self.directory, 'blocks', '%s.png' % o))
self.imgs['lasersource_left'] = pygame.transform.flip(
self.imgs['lasersource_right'], 1, 0)
l = ['hole', 'well', 'wall', 'wall_outside', 'intro-screen']
for o in l:
self.imgs[o] = pygame.image.load(os.path.join(
self.directory, '%s.png' % o))
# Special treatment
screen_size = self.game.window.get_size()
img_size = self.imgs['intro-screen'].get_size()
factors = (float(img_size[0]) / 1920, float(img_size[1]) / 1440)
self.imgs['intro-screen'] = pygame.transform.smoothscale(
self.imgs['intro-screen'],
(int(screen_size[0]*factors[0]),
int(screen_size[1]*factors[1])))
### Moat
l = ['moat_corner_north',
'moat_corner_south',
'moat_corner_north_flip',
'moat_corner_south_flip',
'moat_end_horizontal',
'moat_horizontal',
'moat_vertical']
for o in l:
self.imgs[o] = pygame.image.load(os.path.join(
self.directory, 'moat', '%s.png' % o))
# Special treatment
self.imgs['moat_end_horizontal_flip'] = pygame.transform.flip(
self.imgs['moat_end_horizontal'], 1, 0)
# Load animations
for anim, directory in (
[('boulder_up', os.path.join('boulder', 'up')),
('boulder_down', os.path.join('boulder', 'down')),
('boulder_right', os.path.join('boulder', 'right')),
('boulder_left', os.path.join('boulder', 'right')),
('boulder_falling', os.path.join('boulder_fall')),
('lever_updown', os.path.join('lever', 'down-up')),
('lever_leftright', os.path.join('lever', 'left-right')),
('arrow_up', os.path.join('matt', 'up')),
('arrow_right', os.path.join('matt', 'right')),
('arrow_down', os.path.join('matt', 'down')),
2012-08-10 22:31:15 +02:00
('arrow_left', os.path.join('matt', 'right')),
('stairs', 'stairs'),
2012-08-11 14:47:06 +02:00
('elevating_column', 'elevating_column'),
2012-08-12 19:05:54 +02:00
('mirror', 'mirror'),
('door', 'door')]
):
self.imgs[anim] = []
# Find all image files for the given animation
anim_files = []
for root, dirs, files in os.walk(os.path.join(
self.directory, directory)):
for f in files:
if re.match(r"^.*\.(png)$", '/'.join([root, f])):
anim_files.append('/'.join([root, f]))
# Sort and load the files
for f in sorted(anim_files):
img = pygame.image.load(f)
# Special treatment:
if anim in ('arrow_left',):
img = pygame.transform.flip(img, 1, 0)
self.imgs[anim].append(img)
####### PLAYER #######
for anim, directory in (
[('idle_up', os.path.join('robot_idle', 'up')),
('idle_down', os.path.join('robot_idle', 'down')),
('idle_right', os.path.join('robot_idle', 'right')),
('idle_left', os.path.join('robot_idle', 'right')),
('carry_up', os.path.join('robot_carry', 'up')),
('carry_down', os.path.join('robot_carry', 'down')),
('carry_right', os.path.join('robot_carry', 'right')),
('carry_left', os.path.join('robot_carry', 'right')),
# Lever
('lever_down_right', os.path.join('robot_lever', 'horizontal',
'down_left')),
('lever_down_left', os.path.join('robot_lever', 'horizontal',
'down_left')),
('lever_down_up', os.path.join('robot_lever', 'vertical',
'down_down')),
('lever_down_down', os.path.join('robot_lever', 'vertical',
'down_down')),
('lever_up_right', os.path.join('robot_lever', 'horizontal',
'up_right')),
('lever_up_left', os.path.join('robot_lever', 'horizontal',
'up_right')),
('lever_up_up', os.path.join('robot_lever', 'vertical',
'up_up')),
('lever_up_down', os.path.join('robot_lever', 'vertical',
'up_up')),
('lever_right_right', os.path.join('robot_lever', 'horizontal',
'right_right')),
('lever_right_left', os.path.join('robot_lever', 'horizontal',
'right_right')),
('lever_right_up', os.path.join('robot_lever', 'vertical',
'left_up')),
('lever_right_down', os.path.join('robot_lever', 'vertical',
'right_down')),
('lever_left_right', os.path.join('robot_lever', 'horizontal',
'right_right')),
('lever_left_left', os.path.join('robot_lever', 'horizontal',
'left_left')),
('lever_left_up', os.path.join('robot_lever', 'vertical',
'left_up')),
('lever_left_down', os.path.join('robot_lever', 'vertical',
'right_down'))
]
):
self.imgs['robot_' + anim] = []
# Find all image files for the given animation
anim_files = []
for root, dirs, files in os.walk(os.path.join(
self.directory, directory)):
for f in files:
if re.match(r"^.*\.(png)$", '/'.join([root, f])):
anim_files.append('/'.join([root, f]))
# Sort and load the files
for f in sorted(anim_files):
img = pygame.image.load(f)
# Special treatment:
if anim in ['idle_left', 'carry_left',
'lever_down_right', 'lever_left_down',
2012-08-10 22:31:15 +02:00
'lever_up_left', 'lever_right_up',
'lever_left_right']:
img = pygame.transform.flip(img, 1, 0)
if anim in ['lever_right_left', 'lever_left_right',
'lever_down_up', 'lever_up_down']:
self.imgs['robot_' + anim].insert(0, img)
else:
self.imgs['robot_' + anim].append(img)