From dc5acdf4c65d56c4a0e09251e94b46e75d68da66 Mon Sep 17 00:00:00 2001 From: Sakse Dalum Date: Tue, 7 Aug 2012 19:59:52 +0200 Subject: [PATCH] Added player and level and world objects. --- robotgame/game.py | 3 ++- robotgame/game_menu.py | 18 +++++++------- robotgame/level.py | 7 ++++-- robotgame/level1.py | 38 +++++++++++++++++++++++++++++ robotgame/main_menu.py | 4 ++-- robotgame/player.py | 47 ++++++++++++++++++++++++++++++++++++ robotgame/worldobject.py | 52 ++++++++++++++++++++++++++++++++++++++++ 7 files changed, 155 insertions(+), 14 deletions(-) create mode 100644 robotgame/level1.py create mode 100644 robotgame/player.py create mode 100644 robotgame/worldobject.py diff --git a/robotgame/game.py b/robotgame/game.py index a19f3fb..a17f470 100644 --- a/robotgame/game.py +++ b/robotgame/game.py @@ -67,7 +67,7 @@ class Game(object): t = pygame.time.get_ticks() dt = 0 while self.running: - dt = t - pygame.time.get_ticks() + dt = float(pygame.time.get_ticks() - t) / 1000 t = pygame.time.get_ticks() self.update(t, dt) self.draw() @@ -76,6 +76,7 @@ class Game(object): def goto_level(self, level): self.level = level + self.objs.append(self.level) def update(self, t, dt): diff --git a/robotgame/game_menu.py b/robotgame/game_menu.py index 3a7445e..3df5a2c 100644 --- a/robotgame/game_menu.py +++ b/robotgame/game_menu.py @@ -31,14 +31,14 @@ class GameMenu(object): def __init__(self, game, img_dir, active=False, selection=0): self.__dict__.update(locals()) - self.menu_order = ['restart_level', 'quit'] + self.menu = ['restart_level', 'quit'] self.load() def load(self): screen_size = self.game.window.get_size() - for item in self.menu_order: + for item in self.menu: setattr(self, '%s_imgs' % item, [ pygame.image.load(os.path.join(self.img_dir, '%s-%s.png' % (item, end))) @@ -59,26 +59,26 @@ class GameMenu(object): if event.key == pygame.K_ESCAPE: self.active = not self.active if self.active: - if event.key == pygame.K_SPACE: - if self.menu_order[self.selection] == 'restart_level': + if event.key in [pygame.K_SPACE, pygame.K_RETURN]: + if self.menu[self.selection] == 'restart_level': pass - if self.menu_order[self.selection] == 'quit': + if self.menu[self.selection] == 'quit': self.game.stop() if event.key == pygame.K_UP: self.selection = max(self.selection - 1, 0) if event.key == pygame.K_DOWN: self.selection = min(self.selection + 1, - len(self.menu_order) - 1) + len(self.menu) - 1) def draw(self, window): if self.active: screen_size = self.game.window.get_size() - for i in range(len(self.menu_order)): + for i in range(len(self.menu)): s = i == self.selection - img = getattr(self, '%s_imgs' % self.menu_order[i])[s] + img = getattr(self, '%s_imgs' % self.menu[i])[s] window.blit(img, (int((screen_size[0] - img.get_size()[0]) / 2), int(screen_size[1] / 2) - (int(screen_size[1]*0.13) - * (len(self.menu_order) / 2 - i)))) + * (len(self.menu) / 2 - i)))) diff --git a/robotgame/level.py b/robotgame/level.py index c8c1be0..130bece 100644 --- a/robotgame/level.py +++ b/robotgame/level.py @@ -25,5 +25,8 @@ A generic level. """ class Level(object): - def __init__(self, level_name): - self.__dict__.update(locals()) + def update(self, e, t, dt): + pass + + def draw(self, window): + pass diff --git a/robotgame/level1.py b/robotgame/level1.py new file mode 100644 index 0000000..ff7e87a --- /dev/null +++ b/robotgame/level1.py @@ -0,0 +1,38 @@ +# 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 . +# +# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' +# +# level1.py +# -------------------- +# date created : Tue Aug 7 2012 +# copyright : (C) 2012 Sakse Dalum +# maintained by : Sakse Dalum + +""" +The first level. +""" + +import level +import player + +class Level1(level.Level): + def __init__(self): + self.player = player.Player(200, 200) + + def update(self, e, t, dt): + self.player.update(e, t, dt) + + def draw(self, window): + self.player.draw(window) diff --git a/robotgame/main_menu.py b/robotgame/main_menu.py index 125c991..d5fca8c 100644 --- a/robotgame/main_menu.py +++ b/robotgame/main_menu.py @@ -27,7 +27,7 @@ The main menu. import os import pygame -import level +import level1 import game_menu class MainMenu(object): @@ -59,7 +59,7 @@ class MainMenu(object): for event in e: if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: - self.game.goto_level(level.Level("landing")) + self.game.goto_level(level1.Level1()) self.game.objs.remove(self) self.game.objs.append(game_menu.GameMenu(self.game, self.img_dir)) diff --git a/robotgame/player.py b/robotgame/player.py new file mode 100644 index 0000000..b193ab7 --- /dev/null +++ b/robotgame/player.py @@ -0,0 +1,47 @@ +# 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 . +# +# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' +# +# player.py +# -------------------- +# date created : Tue Aug 7 2012 +# copyright : (C) 2012 Sakse Dalum +# maintained by : Sakse Dalum + +""" +A generic level. +""" + +import pygame + +import worldobject + +class Player(worldobject.WorldObject): + def update(self, e, t, dt): + worldobject.WorldObject.update(self, e, t, dt) + + for event in e: + if event.type == pygame.KEYDOWN: + if event.key == pygame.K_UP: + self.move(0, -1) + if event.key == pygame.K_DOWN: + self.move(0, 1) + if event.key == pygame.K_RIGHT: + self.move(1, 0) + if event.key == pygame.K_LEFT: + self.move(-1, 0) + + def draw(self, window): + pygame.draw.circle(window, (255, 255, 255), (self.x, self.y), 20) diff --git a/robotgame/worldobject.py b/robotgame/worldobject.py new file mode 100644 index 0000000..f30f408 --- /dev/null +++ b/robotgame/worldobject.py @@ -0,0 +1,52 @@ +# 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 . +# +# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' +# +# worldobject.py +# -------------------- +# date created : Tue Aug 7 2012 +# copyright : (C) 2012 Sakse Dalum +# maintained by : Sakse Dalum + +""" +A generic world object. +""" + +class WorldObject(object): + def __init__(self, x, y, speed=5, tile_x=64, tile_y=48): + self.__dict__.update(locals()) + + self.move_x, self.move_y = self.x, self.y + + def move(self, move_x, move_y): + if self.move_x == self.x and self.move_y == self.y: + self.move_x += move_x * self.tile_x + self.move_y += move_y * self.tile_y + + def update(self, e, t, dt): + if self.x > self.move_x: + self.x -= min(self.speed * dt * self.tile_x, + abs(self.x - self.move_x)) + if self.x < self.move_x: + self.x += min(self.speed * dt * self.tile_x, + abs(self.x - self.move_x)) + if self.y > self.move_y: + self.y -= min(self.speed * dt * self.tile_y, + abs(self.y - self.move_y)) + if self.y < self.move_y: + self.y += min(self.speed * dt * self.tile_y, + abs(self.y - self.move_y)) + + self.x, self.y = int(self.x), int(self.y)