a-robots-conundrum/robotgame/loader.py

192 lines
7.7 KiB
Python

# 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, 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, 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']
for o in l:
self.imgs[o] = pygame.image.load(os.path.join(
self.directory, 'blocks', '%s.png' % o))
l = ['hole', 'well']
for o in l:
self.imgs[o] = pygame.image.load(os.path.join(
self.directory, '%s.png' % o))
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')),
('arrow_left', os.path.join('matt', 'right')),
('stairs', 'stairs'),
('mirror', 'mirror')]
):
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 == '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',
'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)