Added the robot with animations to the game. Also made the game pause when using the menu.
This commit is contained in:
parent
28a19d5493
commit
ea181b40c0
|
@ -1,2 +1,2 @@
|
||||||
*~
|
*~
|
||||||
*.py[co]
|
*.py[co]
|
|
@ -50,6 +50,9 @@ import argparse
|
||||||
import robotgame
|
import robotgame
|
||||||
|
|
||||||
if __name__ == '__main__':
|
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
|
# Parse command-line arguments
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('-r',
|
parser.add_argument('-r',
|
||||||
|
@ -78,12 +81,12 @@ if __name__ == '__main__':
|
||||||
print("Display width: %d, display height: %d, fullscreen: %s"
|
print("Display width: %d, display height: %d, fullscreen: %s"
|
||||||
% (resolution[0], resolution[1], fullscreen))
|
% (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_icon(pygame.image.load(icon))
|
||||||
pygame.display.set_caption("ROBOTGAME")
|
pygame.display.set_caption("ROBOTGAME")
|
||||||
|
|
||||||
window = pygame.display.set_mode(resolution,
|
window = pygame.display.set_mode(resolution,
|
||||||
pygame.FULLSCREEN if fullscreen else 0)
|
pygame.FULLSCREEN if fullscreen else 0)
|
||||||
|
|
||||||
game = robotgame.game.Game(window, disable_music)
|
game = robotgame.game.Game(window, directory, disable_music)
|
||||||
game.start()
|
game.start()
|
||||||
|
|
|
@ -42,3 +42,4 @@ class Block(worldobject.WorldObject):
|
||||||
def draw(self, window):
|
def draw(self, window):
|
||||||
window.blit(self.img, (self.x - 32,
|
window.blit(self.img, (self.x - 32,
|
||||||
self.y - self.img.get_size()[1]))
|
self.y - self.img.get_size()[1]))
|
||||||
|
|
||||||
|
|
|
@ -34,7 +34,8 @@ import game_menu
|
||||||
|
|
||||||
class Game(object):
|
class Game(object):
|
||||||
"""Create an object to handle the game."""
|
"""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.__dict__.update(locals())
|
||||||
|
|
||||||
self.objs = []
|
self.objs = []
|
||||||
|
@ -53,12 +54,12 @@ class Game(object):
|
||||||
self.running = False
|
self.running = False
|
||||||
|
|
||||||
def load(self):
|
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.objs.append(main_menu.MainMenu(self, graphics_dir))
|
||||||
self.level = None
|
self.level = None
|
||||||
self.jukebox = jukebox.Jukebox(
|
self.jukebox = jukebox.Jukebox(
|
||||||
os.path.abspath(os.path.join("resources", "music")),
|
os.path.join(self.directory, "resources", "music"),
|
||||||
["basshit.ogg"])
|
["basshit.ogg"])
|
||||||
self.jukebox.stop()
|
self.jukebox.stop()
|
||||||
if not self.disable_music:
|
if not self.disable_music:
|
||||||
|
|
|
@ -53,15 +53,19 @@ class GameMenu(object):
|
||||||
int(screen_size[1]*factors[1])))
|
int(screen_size[1]*factors[1])))
|
||||||
for img in getattr(self, '%s_imgs' % item)])
|
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):
|
def update(self, e, t, dt):
|
||||||
for event in e:
|
for event in e:
|
||||||
if event.type == pygame.KEYDOWN:
|
if event.type == pygame.KEYDOWN:
|
||||||
if event.key == pygame.K_ESCAPE:
|
if event.key == pygame.K_ESCAPE:
|
||||||
self.active = not self.active
|
self.toggle_menu()
|
||||||
if self.active:
|
if self.active:
|
||||||
if event.key in [pygame.K_SPACE, pygame.K_RETURN]:
|
if event.key in [pygame.K_SPACE, pygame.K_RETURN]:
|
||||||
if self.menu[self.selection] == 'restart_level':
|
if self.menu[self.selection] == 'restart_level':
|
||||||
pass
|
self.game.level.restart()
|
||||||
|
self.toggle_menu()
|
||||||
if self.menu[self.selection] == 'quit':
|
if self.menu[self.selection] == 'quit':
|
||||||
self.game.stop()
|
self.game.stop()
|
||||||
if event.key == pygame.K_UP:
|
if event.key == pygame.K_UP:
|
||||||
|
|
|
@ -25,8 +25,20 @@ A generic level.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
class Level(object):
|
class Level(object):
|
||||||
def update(self, e, t, dt):
|
def __init__(self, graphics_dir, paused=False):
|
||||||
|
self.__dict__.update(locals())
|
||||||
|
|
||||||
|
def restart(self):
|
||||||
pass
|
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):
|
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)
|
||||||
|
|
|
@ -34,12 +34,12 @@ import tile
|
||||||
import block
|
import block
|
||||||
|
|
||||||
class Level1(level.Level):
|
class Level1(level.Level):
|
||||||
def __init__(self, graphics_dir):
|
def __init__(self, graphics_dir, paused=False):
|
||||||
self.__dict__.update(locals())
|
level.Level.__init__(self, graphics_dir, paused)
|
||||||
|
|
||||||
self.player = player.Player(self, 5*64, 5*48)
|
self.player = player.Player(self, 5*64, 5*48)
|
||||||
|
|
||||||
self.dimensions = 20, 20
|
self.dimensions = 10, 10
|
||||||
|
|
||||||
self.tiles = []
|
self.tiles = []
|
||||||
self.objects = []
|
self.objects = []
|
||||||
|
@ -54,9 +54,9 @@ class Level1(level.Level):
|
||||||
tile.Tile(self, i*64, j*48, self.imgs['ground1']))
|
tile.Tile(self, i*64, j*48, self.imgs['ground1']))
|
||||||
|
|
||||||
self.objects.append(self.player)
|
self.objects.append(self.player)
|
||||||
for i in range(100):
|
for i in range(10):
|
||||||
self.objects.append(block.Block(self, random.randint(0, 20)*64,
|
self.objects.append(block.Block(self, random.randint(0, 10)*64,
|
||||||
random.randint(0, 20)*48,
|
random.randint(0, 10)*48,
|
||||||
self.imgs['block1'],
|
self.imgs['block1'],
|
||||||
movable=True))
|
movable=True))
|
||||||
|
|
||||||
|
@ -73,13 +73,7 @@ class Level1(level.Level):
|
||||||
self.imgs[block] = pygame.image.load(os.path.join(
|
self.imgs[block] = pygame.image.load(os.path.join(
|
||||||
self.graphics_dir, 'blocks', '%s.png' % block))
|
self.graphics_dir, 'blocks', '%s.png' % block))
|
||||||
|
|
||||||
def update(self, e, t, dt):
|
def restart(self):
|
||||||
for obj in self.objects:
|
self.objects.remove(self.player)
|
||||||
obj.update(e, t, dt)
|
self.player = player.Player(self, 5*64, 5*48)
|
||||||
|
self.objects.append(self.player)
|
||||||
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)
|
|
||||||
|
|
|
@ -25,6 +25,8 @@ A generic level.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import pygame
|
import pygame
|
||||||
|
import re
|
||||||
|
import os
|
||||||
|
|
||||||
import worldobject
|
import worldobject
|
||||||
|
|
||||||
|
@ -34,6 +36,43 @@ class Player(worldobject.WorldObject):
|
||||||
worldobject.WorldObject.__init__(self, level, x, y,
|
worldobject.WorldObject.__init__(self, level, x, y,
|
||||||
z=z, movable=movable)
|
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):
|
def touch(self, touch_x, touch_y):
|
||||||
for obj in self.level.objects:
|
for obj in self.level.objects:
|
||||||
if (obj.x == self.x + touch_x * self.tile_x
|
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 event.key == pygame.K_UP:
|
||||||
if not self.holding:
|
if not self.holding:
|
||||||
self.direction = (0, -1)
|
self.direction = (0, -1)
|
||||||
|
self.anim = 'idle_up'
|
||||||
self.move(0, -1)
|
self.move(0, -1)
|
||||||
if event.key == pygame.K_DOWN:
|
if event.key == pygame.K_DOWN:
|
||||||
if not self.holding:
|
if not self.holding:
|
||||||
self.direction = (0, 1)
|
self.direction = (0, 1)
|
||||||
|
self.anim = 'idle_down'
|
||||||
self.move(0, 1)
|
self.move(0, 1)
|
||||||
if event.key == pygame.K_RIGHT:
|
if event.key == pygame.K_RIGHT:
|
||||||
if not self.holding:
|
if not self.holding:
|
||||||
self.direction = (1, 0)
|
self.direction = (1, 0)
|
||||||
|
self.anim = 'idle_right'
|
||||||
self.move(1, 0)
|
self.move(1, 0)
|
||||||
if event.key == pygame.K_LEFT:
|
if event.key == pygame.K_LEFT:
|
||||||
if not self.holding:
|
if not self.holding:
|
||||||
self.direction = (-1, 0)
|
self.direction = (-1, 0)
|
||||||
|
self.anim = 'idle_left'
|
||||||
self.move(-1, 0)
|
self.move(-1, 0)
|
||||||
|
|
||||||
if event.key == pygame.K_SPACE:
|
if event.key == pygame.K_SPACE:
|
||||||
self.touch(*self.direction)
|
self.touch(*self.direction)
|
||||||
|
|
||||||
|
# Update the animation
|
||||||
|
self.frame = ((self.frame + self.anim_speed * dt) %
|
||||||
|
len(self.imgs[self.anim]))
|
||||||
|
|
||||||
def draw(self, window):
|
def draw(self, window):
|
||||||
pygame.draw.circle(window, (255, 0, 255),
|
self.img = self.imgs[self.anim][int(self.frame)]
|
||||||
(self.x + self.tile_x / 2,
|
window.blit(self.img, (self.x - 32,
|
||||||
self.y - self.tile_y / 2), 20)
|
self.y - self.img.get_size()[1] + 32))
|
||||||
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)
|
|
||||||
|
|
Loading…
Reference in New Issue