From 7170b4a3d23c40568195ea9d8f709f08910efbc2 Mon Sep 17 00:00:00 2001 From: Sakse Dalum Date: Tue, 7 Aug 2012 13:21:01 +0200 Subject: [PATCH] Added game.py etc. --- .gitignore | 4 ++- robotgame.py | 53 +++++++++++++++++++++++++++++-- robotgame/__init__.py | 2 ++ robotgame/game.py | 74 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 130 insertions(+), 3 deletions(-) mode change 100644 => 100755 robotgame.py create mode 100644 robotgame/game.py diff --git a/.gitignore b/.gitignore index e4e5f6c..37a1f59 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -*~ \ No newline at end of file +*~ +*.pyc +*.pyo \ No newline at end of file diff --git a/robotgame.py b/robotgame.py old mode 100644 new mode 100755 index 9aa6a45..f76fd62 --- a/robotgame.py +++ b/robotgame.py @@ -12,7 +12,7 @@ # 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 -# Project Ant. If not, see . +# ROBOTGAME. If not, see . # # ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' # @@ -21,6 +21,55 @@ # date created : Tue Aug 7 2012 # copyright : (C) 2012 Sakse Dalum # maintained by : Sakse Dalum -# description : The main entrance point. + +""" +The main entrance point. +""" + +from __future__ import print_function + +import sys +import pygame + +# Initialise all Pygame modules +pygame.init() +# Check if critical Pygame modules were initialised properly before continuing. +module_names = ['display', 'mixer'] +for module_name in module_names: + module = getattr(pygame, module_name) + if not module.get_init(): + print("Failed to initialise %s module." % module_name) + sys.exit() + +import argparse import robotgame + +if __name__ == '__main__': + # Parse command-line arguments + parser = argparse.ArgumentParser() + parser.add_argument('-r', + '--resolution', + type=str, + default="800x600", + help="specifies the resolution of the game") + parser.add_argument('-f', + '--fullscreen', + action='store_const', + const=True, + default=False, + help="specifies the resolution of the game") + + args = vars(parser.parse_args()) + resolution = tuple([int(x) for x in args['resolution'].split('x')]) + fullscreen = bool(args['fullscreen']) + + print("Display width: %d, display height: %d, fullscreen: %s" + % (resolution[0], resolution[1], fullscreen)) + + window = pygame.display.set_mode(resolution, + pygame.FULLSCREEN if fullscreen else 0) + + game = robotgame.game.Game(window) + game.start() + help(robotgame.game.Game) diff --git a/robotgame/__init__.py b/robotgame/__init__.py index 5c21d1e..9c634e4 100644 --- a/robotgame/__init__.py +++ b/robotgame/__init__.py @@ -1 +1,3 @@ # init file + +import game diff --git a/robotgame/game.py b/robotgame/game.py new file mode 100644 index 0000000..3da4720 --- /dev/null +++ b/robotgame/game.py @@ -0,0 +1,74 @@ +# 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 . +# +# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' +# +# game.py +# -------------------- +# date created : Tue Aug 7 2012 +# copyright : (C) 2012 Sakse Dalum +# maintained by : Sakse Dalum + +""" +The game. Handles everything. +""" + +import pygame + +class Game(object): + """Create an object to handle the game.""" + def __init__(self, window, running=False, speed=30): + self.__dict__.update(locals()) + + self.active_objs = [] + self.passive_objs = [] + + self.clock = pygame.time.Clock() + + def start(self): + self.running = True + self.run() + + def stop(self): + self.running = False + + def activate_object(self, obj): + self.active_objs.remove(obj) + self.passive_objs.append(obj) + + def deactivate_object(self, obj): + self.passive_objs.remove(obj) + self.active_objs.append(obj) + + def run(self): + while self.running: + self.update() + self.draw() + self.clock.tick(self.speed) + + def update(self): + # Get all events since last update call (this prevents event + # "bottlenecking"/lock-ups) + events = pygame.event.get() + for event in events: + # Stop the game when closing the window + if event.type == pygame.QUIT: + self.stop() + + t = pygame.time.get_ticks() + for obj in self.active_objs: + obj.update(t) + + def draw(self): + pass