a-robots-conundrum/robotgame/game.py

138 lines
3.9 KiB
Python

# 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 os
import pygame
import robotgame
import jukebox
import level
import main_menu
import game_menu
import loader
class Game(object):
"""Create an object to handle the game."""
def __init__(self, window, directory, disable_music,
running=False, speed=30, initial_goto_level=None,
level_num=0):
self.__dict__.update(locals())
self.objs = []
self.clock = pygame.time.Clock()
self.ticks = self.prev_ticks = pygame.time.get_ticks()
self.load()
def start(self):
self.running = True
self.run()
def stop(self):
self.running = False
def load(self):
graphics_dir = os.path.join(self.directory, "resources", "graphics")
self.loader = loader.Loader(graphics_dir)
self.loader.load()
self.level = None
self.jukebox = jukebox.Jukebox(
os.path.join(self.directory, "resources", "music"),
["basshit.ogg"])
self.jukebox.stop()
if not self.disable_music:
self.jukebox.play()
if not self.initial_goto_level:
self.menu = main_menu.MainMenu(self, graphics_dir)
else:
self.goto_level(self.initial_goto_level)
def run(self):
t = pygame.time.get_ticks()
dt = 0
while self.running:
dt = float(pygame.time.get_ticks() - t) / 1000
t = pygame.time.get_ticks()
self.update(t, dt)
self.draw()
self.clock.tick(self.speed)
def goto_level(self, level):
self.level_num = level
graphics_dir = os.path.join(self.directory, "resources", "graphics")
self.menu = game_menu.GameMenu(self, graphics_dir)
exec 'from level%d import Level%d as level' % (level, level)
self.level = level(self, graphics_dir)
self.objs.append(self.level)
def restart_level(self):
self.goto_level(self.level_num)
def update(self, t, dt):
"""
Update all game objects.
"""
# Retrieve and flush all events since last update call (this prevents
# event "bottlenecking"/lock-ups)
e = pygame.event.get()
for event in e:
# Stop the game when closing the window
if event.type == pygame.QUIT:
self.stop()
# Keep the music playing!
if not pygame.mixer.music.get_busy() and not self.disable_music:
self.jukebox.play()
# Update all objects
for obj in self.objs[:]:
if hasattr(obj, 'update'):
obj.update(e, t, dt)
self.menu.update(e, t, dt)
def draw(self):
"""
Draw all game objects.
"""
self.window.fill((0, 0, 0))
for obj in self.objs[:]:
if hasattr(obj, 'draw'):
obj.draw(self.window)
self.menu.draw(self.window)
pygame.display.flip()