Added game.py etc.

This commit is contained in:
Sakse Dalum 2012-08-07 13:21:01 +02:00
parent ff0d23263f
commit 7170b4a3d2
4 changed files with 130 additions and 3 deletions

4
.gitignore vendored
View File

@ -1 +1,3 @@
*~
*~
*.pyc
*.pyo

53
robotgame.py Normal file → Executable file
View File

@ -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 <http://www.gnu.org/licenses/>.
# ROBOTGAME. If not, see <http://www.gnu.org/licenses/>.
#
# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
#
@ -21,6 +21,55 @@
# date created : Tue Aug 7 2012
# copyright : (C) 2012 Sakse Dalum
# maintained by : Sakse Dalum <don_s@hongabar.org>
# 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)

View File

@ -1 +1,3 @@
# init file
import game

74
robotgame/game.py Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
#
# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
#
# game.py
# --------------------
# date created : Tue Aug 7 2012
# copyright : (C) 2012 Sakse Dalum
# maintained by : Sakse Dalum <don_s@hongabar.org>
"""
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