diff --git a/.gitignore b/.gitignore index 182889a..d0b85ca 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ *~ -*.py[co] +*.py[co] \ No newline at end of file diff --git a/robotgame.py b/robotgame.py index 3319a37..4957809 100755 --- a/robotgame.py +++ b/robotgame.py @@ -50,6 +50,9 @@ import argparse import robotgame if __name__ == '__main__': + # Get the absolute path for the directory of the script. + directory = os.path.dirname(os.path.realpath(__file__)) + # Parse command-line arguments parser = argparse.ArgumentParser() parser.add_argument('-r', @@ -78,12 +81,12 @@ if __name__ == '__main__': print("Display width: %d, display height: %d, fullscreen: %s" % (resolution[0], resolution[1], fullscreen)) - icon = os.path.abspath(os.path.join("resources", "graphics", "icon.png")) + icon = os.path.join(directory, "resources", "graphics", "icon.png") pygame.display.set_icon(pygame.image.load(icon)) pygame.display.set_caption("ROBOTGAME") window = pygame.display.set_mode(resolution, pygame.FULLSCREEN if fullscreen else 0) - game = robotgame.game.Game(window, disable_music) + game = robotgame.game.Game(window, directory, disable_music) game.start() diff --git a/robotgame/block.py b/robotgame/block.py index 3565d8f..7144dc3 100644 --- a/robotgame/block.py +++ b/robotgame/block.py @@ -42,3 +42,4 @@ class Block(worldobject.WorldObject): def draw(self, window): window.blit(self.img, (self.x - 32, self.y - self.img.get_size()[1])) + diff --git a/robotgame/game.py b/robotgame/game.py index 803a3fd..f9b85bd 100644 --- a/robotgame/game.py +++ b/robotgame/game.py @@ -34,7 +34,8 @@ import game_menu class Game(object): """Create an object to handle the game.""" - def __init__(self, window, disable_music, running=False, speed=30): + def __init__(self, window, directory, disable_music, + running=False, speed=30): self.__dict__.update(locals()) self.objs = [] @@ -53,12 +54,12 @@ class Game(object): self.running = False def load(self): - graphics_dir = os.path.abspath(os.path.join("resources", "graphics")) + graphics_dir = os.path.join(self.directory, "resources", "graphics") self.objs.append(main_menu.MainMenu(self, graphics_dir)) self.level = None self.jukebox = jukebox.Jukebox( - os.path.abspath(os.path.join("resources", "music")), + os.path.join(self.directory, "resources", "music"), ["basshit.ogg"]) self.jukebox.stop() if not self.disable_music: diff --git a/robotgame/game_menu.py b/robotgame/game_menu.py index 3df5a2c..a270dc4 100644 --- a/robotgame/game_menu.py +++ b/robotgame/game_menu.py @@ -53,15 +53,19 @@ class GameMenu(object): int(screen_size[1]*factors[1]))) for img in getattr(self, '%s_imgs' % item)]) + def toggle_menu(self): + self.game.level.paused = self.active = not self.active + def update(self, e, t, dt): for event in e: if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: - self.active = not self.active + self.toggle_menu() if self.active: if event.key in [pygame.K_SPACE, pygame.K_RETURN]: if self.menu[self.selection] == 'restart_level': - pass + self.game.level.restart() + self.toggle_menu() if self.menu[self.selection] == 'quit': self.game.stop() if event.key == pygame.K_UP: diff --git a/robotgame/level.py b/robotgame/level.py index 130bece..6476231 100644 --- a/robotgame/level.py +++ b/robotgame/level.py @@ -25,8 +25,20 @@ A generic level. """ class Level(object): - def update(self, e, t, dt): + def __init__(self, graphics_dir, paused=False): + self.__dict__.update(locals()) + + def restart(self): pass + def update(self, e, t, dt): + if not self.paused: + for obj in self.objects: + obj.update(e, t, dt) + def draw(self, window): - pass + for tile in self.tiles: + tile.draw(window) + + for obj in sorted(self.objects, key=lambda obj: (obj.y + obj.z)): + obj.draw(window) diff --git a/robotgame/level1.py b/robotgame/level1.py index dc87452..c8ca107 100644 --- a/robotgame/level1.py +++ b/robotgame/level1.py @@ -34,12 +34,12 @@ import tile import block class Level1(level.Level): - def __init__(self, graphics_dir): - self.__dict__.update(locals()) + def __init__(self, graphics_dir, paused=False): + level.Level.__init__(self, graphics_dir, paused) self.player = player.Player(self, 5*64, 5*48) - self.dimensions = 20, 20 + self.dimensions = 10, 10 self.tiles = [] self.objects = [] @@ -54,9 +54,9 @@ class Level1(level.Level): tile.Tile(self, i*64, j*48, self.imgs['ground1'])) self.objects.append(self.player) - for i in range(100): - self.objects.append(block.Block(self, random.randint(0, 20)*64, - random.randint(0, 20)*48, + for i in range(10): + self.objects.append(block.Block(self, random.randint(0, 10)*64, + random.randint(0, 10)*48, self.imgs['block1'], movable=True)) @@ -73,13 +73,7 @@ class Level1(level.Level): self.imgs[block] = pygame.image.load(os.path.join( self.graphics_dir, 'blocks', '%s.png' % block)) - def update(self, e, t, dt): - for obj in self.objects: - obj.update(e, t, dt) - - def draw(self, window): - for tile in self.tiles: - tile.draw(window) - - for obj in sorted(self.objects, key=lambda obj: (obj.y + obj.z)): - obj.draw(window) + def restart(self): + self.objects.remove(self.player) + self.player = player.Player(self, 5*64, 5*48) + self.objects.append(self.player) diff --git a/robotgame/player.py b/robotgame/player.py index 6c172f9..9ac9b50 100644 --- a/robotgame/player.py +++ b/robotgame/player.py @@ -25,6 +25,8 @@ A generic level. """ import pygame +import re +import os import worldobject @@ -34,6 +36,43 @@ class Player(worldobject.WorldObject): worldobject.WorldObject.__init__(self, level, x, y, z=z, movable=movable) + self.load() + + self.anim = 'idle_right' + self.frame = 0 + self.anim_speed = 30 + + 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'))] + ): + + 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 == 'idle_left': + img = pygame.transform.flip(img, 1, 0) + + self.imgs[anim].append(img) + + def touch(self, touch_x, touch_y): for obj in self.level.objects: if (obj.x == self.x + touch_x * self.tile_x @@ -49,27 +88,33 @@ class Player(worldobject.WorldObject): if event.key == pygame.K_UP: if not self.holding: self.direction = (0, -1) + self.anim = 'idle_up' self.move(0, -1) if event.key == pygame.K_DOWN: if not self.holding: self.direction = (0, 1) + self.anim = 'idle_down' self.move(0, 1) if event.key == pygame.K_RIGHT: if not self.holding: self.direction = (1, 0) + self.anim = 'idle_right' self.move(1, 0) if event.key == pygame.K_LEFT: if not self.holding: self.direction = (-1, 0) + self.anim = 'idle_left' self.move(-1, 0) if event.key == pygame.K_SPACE: self.touch(*self.direction) + # Update the animation + self.frame = ((self.frame + self.anim_speed * dt) % + len(self.imgs[self.anim])) + def draw(self, window): - pygame.draw.circle(window, (255, 0, 255), - (self.x + self.tile_x / 2, - self.y - self.tile_y / 2), 20) - pygame.draw.circle(window, (255, 255, 0), - (self.x + self.tile_x / 2 + self.direction[0]*10, - self.y - self.tile_y / 2 + self.direction[1]*10), 5) + self.img = self.imgs[self.anim][int(self.frame)] + window.blit(self.img, (self.x - 32, + self.y - self.img.get_size()[1] + 32)) + diff --git a/tests/rollingstone_tests.py b/tests/rollingstone_tests.py old mode 100644 new mode 100755