Added loader object. Game now loads on startup.

This commit is contained in:
Sakse Dalum 2012-08-10 18:54:48 +02:00
parent 91a228af72
commit d6ec520e21
5 changed files with 192 additions and 159 deletions

View File

@ -32,6 +32,7 @@ import jukebox
import level
import main_menu
import game_menu
import loader
class Game(object):
"""Create an object to handle the game."""
@ -56,6 +57,8 @@ class Game(object):
def load(self):
graphics_dir = os.path.join(self.directory, "resources", "graphics")
self.loader = loader.Loader(graphics_dir)
self.loader.load()
self.level = None
self.jukebox = jukebox.Jukebox(

View File

@ -35,9 +35,7 @@ class Level(object):
self.tiles = []
self.objects = []
self.imgs = {}
self.load()
self.imgs = game.loader.imgs
self.reverse_imgs = (
dict([(val, key) for (key, val)

View File

@ -338,79 +338,6 @@ class Level1(level.Level):
if blocking:
self.objects.append(block.InvisBlock(self, x, y))
def load(self):
"""Load all resources used in the level."""
l = ['ground1', 'ground2']
for o in l:
self.imgs[o] = pygame.image.load(os.path.join(
self.graphics_dir, 'tiles', '%s.png' % o))
for o in range(1, 7):
self.imgs['indoor%d' % o] = pygame.image.load(os.path.join(
self.graphics_dir, 'tiles', 'indoor', 'ground%02d.png' % o))
l = ['block1', 'block1_lifted']
for o in l:
self.imgs[o] = pygame.image.load(os.path.join(
self.graphics_dir, 'blocks', '%s.png' % o))
l = ['hole', 'well']
for o in l:
self.imgs[o] = pygame.image.load(os.path.join(
self.graphics_dir, '%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.graphics_dir, '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')),]
):
self.imgs[anim] = []
# Find all image files for the given animation
anim_files = []
for root, dirs, files in os.walk(os.path.join(
self.graphics_dir, 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)
def complete_task(self, task):
if task == 0:
return

186
robotgame/loader.py Normal file
View File

@ -0,0 +1,186 @@
# 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']
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')),]
):
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']:
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)

View File

@ -47,89 +47,8 @@ class Player(worldobject.WorldObject):
self.ignore_list.append(boulder.Boulder)
self.load()
def load(self):
self.imgs = {}
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[anim] = []
# Find all image files for the given animation
anim_files = []
for root, dirs, files in os.walk(os.path.join(
self.level.graphics_dir, 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']:
img = pygame.transform.flip(img, 1, 0)
if anim in ['lever_right_left', 'lever_left_right',
'lever_down_up', 'lever_up_down']:
self.imgs[anim].insert(0, img)
else:
self.imgs[anim].append(img)
self.img = self.imgs[self.anim_root + '_' + self.anim][int(self.frame)]
self.imgs = dict([(k[6:], v) for (k, v) in self.level.imgs.iteritems()
if k.startswith('robot_')])
def direction_as_string(self):
if self.direction == (1, 0):