Merge branch 'master' of hongabar.org:robotgame

This commit is contained in:
Sakse Dalum 2012-08-09 16:00:31 +02:00
commit fc5efcc69a
4 changed files with 217 additions and 6 deletions

56
robotgame/fadeout.py Normal file
View File

@ -0,0 +1,56 @@
# 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/>.
#
# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
#
# fadeout.py
# --------------------
# date created : Thu Aug 9 2012
# copyright : (C) 2012 Niels G. W. Serup
# maintained by : Niels G. W. Serup <ns@metanohi.name>
"""
Fade out and in.
"""
import os
import pygame
class Fadeout(object):
def __init__(self, game, function):
self.__dict__.update(locals())
self.game.objs.append(self)
self.img = pygame.Surface(self.game.window.get_size())
self._start_time = pygame.time.get_ticks()
self._middle_time = self._start_time + 500
self._end_time = self._middle_time + 500
self.img.set_alpha(0)
self.img.fill((0, 0, 0))
self._has_run = False
def update(self, e, t, dt):
if t < self._middle_time:
self.img.set_alpha(255 * (t - self._start_time) / 500)
else:
if not self._has_run:
self.function()
self._has_run = True
if t < self._end_time:
self.img.set_alpha(255 * (500 - (t - self._middle_time)) / 500)
else:
self.game.objs.remove(self)
self.update = lambda *xs: None
def draw(self, window):
window.blit(self.img, (0, 0))

View File

@ -109,7 +109,7 @@ class Game(object):
self.jukebox.play()
# Update all objects
for obj in self.objs:
for obj in self.objs[:]:
if hasattr(obj, 'update'):
obj.update(e, t, dt)
@ -121,7 +121,7 @@ class Game(object):
"""
self.window.fill((0, 0, 0))
for obj in self.objs:
for obj in self.objs[:]:
if hasattr(obj, 'draw'):
obj.draw(self.window)
self.menu.draw(self.window)

View File

@ -9,14 +9,16 @@ import tile
import block
import boulder
import lever
import level_bonus
import fadeout
class Level2(level.Level):
def __init__(self, game, graphics_dir, paused=False):
level.Level.__init__(self, game, graphics_dir, size=(64*20, 48*20),
level.Level.__init__(self, game, graphics_dir, size=(64*5, 48*5),
paused=paused)
self.dimensions = 20, 20
self.dimensions = 5, 5
for i in range(self.dimensions[0]):
for j in range(self.dimensions[1]):
@ -25,9 +27,15 @@ class Level2(level.Level):
self.draw_background()
blocks = [block.Block(self, 64*4, 48, self.imgs['block1'])]
bonus = level_bonus.Level(self.game, self.graphics_dir)
self.objects.append(
lever.Lever(
self, 64 * 2, 48 * 3,
[lambda setting:
fadeout.Fadeout(self.game, lambda: bonus.enter(self))],
toggling=False,
anim='lever_updown'))
self.objects.extend(blocks)
def load(self):
"""Load all resources used in the level."""
@ -42,6 +50,32 @@ class Level2(level.Level):
self.imgs[block] = pygame.image.load(os.path.join(
self.graphics_dir, 'blocks', '%s.png' % block))
# Load animations
for anim, directory in (
[('lever_updown', os.path.join('lever', 'down-up')),
]
):
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 restart(self):
for obj in self.objects:
obj.reset_pos()

121
robotgame/level_bonus.py Normal file
View File

@ -0,0 +1,121 @@
# 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/>.
#
# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
#
# level_bonus.py
# --------------------
# date created : Thu Aug 9 2012
# copyright : (C) 2012 Niels G. W. Serup
# maintained by : Niels G. W. Serup <ns@metanohi.name>
"""
Fun bonus level.
"""
import os
import pygame
import random
import re
import itertools
import level
import player
import tile
import block
import boulder
import lever
import fadeout
class Level(level.Level):
def __init__(self, game, graphics_dir, paused=False):
level.Level.__init__(self, game, graphics_dir, size=(64*20, 48*20),
paused=paused)
self.dimensions = 20, 20
for i in range(self.dimensions[0]):
for j in range(self.dimensions[1]):
self.tiles.append(
tile.Tile(self, i*64, j*48, self.imgs['ground1']))
self.draw_background()
emptys = list(itertools.product(range(2, 20), range(2, 20)))
for _ in range(150):
x, y = random.choice(emptys)
emptys.remove((x, y))
self.objects.append(block.Block(self, 64 * x, 48 * y,
self.imgs['block1'], movable=True))
self.objects.append(
lever.Lever(
self, 64, 48,
[lambda setting: fadeout.Fadeout(self.game, self.exit)],
toggling=False,
anim='lever_updown'))
def load(self):
"""Load all resources used in the level."""
tile_list = ['ground1', 'ground2']
for tile in tile_list:
self.imgs[tile] = pygame.image.load(os.path.join(
self.graphics_dir, 'tiles', '%s.png' % tile))
block_list = ['block1']
for block in block_list:
self.imgs[block] = pygame.image.load(os.path.join(
self.graphics_dir, 'blocks', '%s.png' % block))
# Load animations
for anim, directory in (
[('lever_updown', os.path.join('lever', 'down-up')),
]
):
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 enter(self, root_level):
self.__dict__.update(locals())
self.game.objs.remove(root_level)
self.game.objs.insert(0, self)
self.game.level = self
def exit(self):
self.game.objs.remove(self)
self.game.objs.insert(0, self.root_level)
def restart(self):
for obj in self.objects:
obj.reset_pos()