2012-08-07 13:21:01 +02:00
|
|
|
# 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.
|
|
|
|
"""
|
|
|
|
|
2012-08-07 14:00:49 +02:00
|
|
|
import os
|
2012-08-07 13:21:01 +02:00
|
|
|
import pygame
|
|
|
|
|
2012-08-08 19:42:57 +02:00
|
|
|
import robotgame
|
2012-08-07 14:00:49 +02:00
|
|
|
import jukebox
|
2012-08-07 15:40:42 +02:00
|
|
|
import level
|
|
|
|
import main_menu
|
2012-08-07 17:26:49 +02:00
|
|
|
import game_menu
|
2012-08-10 18:54:48 +02:00
|
|
|
import loader
|
2012-08-07 14:00:49 +02:00
|
|
|
|
2012-08-07 13:21:01 +02:00
|
|
|
class Game(object):
|
|
|
|
"""Create an object to handle the game."""
|
2012-08-08 13:26:55 +02:00
|
|
|
def __init__(self, window, directory, disable_music,
|
2012-08-12 19:47:55 +02:00
|
|
|
running=False, speed=30, initial_goto_level=None,
|
|
|
|
level_num=0):
|
2012-08-07 13:21:01 +02:00
|
|
|
self.__dict__.update(locals())
|
|
|
|
|
2012-08-07 14:00:49 +02:00
|
|
|
self.objs = []
|
2012-08-07 13:21:01 +02:00
|
|
|
|
|
|
|
self.clock = pygame.time.Clock()
|
|
|
|
|
2012-08-07 14:00:49 +02:00
|
|
|
self.ticks = self.prev_ticks = pygame.time.get_ticks()
|
|
|
|
|
2012-08-07 15:46:40 +02:00
|
|
|
self.load()
|
|
|
|
|
2012-08-07 13:21:01 +02:00
|
|
|
def start(self):
|
|
|
|
self.running = True
|
|
|
|
self.run()
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self.running = False
|
|
|
|
|
2012-08-07 15:46:40 +02:00
|
|
|
def load(self):
|
2012-08-08 13:26:55 +02:00
|
|
|
graphics_dir = os.path.join(self.directory, "resources", "graphics")
|
2012-08-10 18:54:48 +02:00
|
|
|
self.loader = loader.Loader(graphics_dir)
|
|
|
|
self.loader.load()
|
2012-08-07 17:26:49 +02:00
|
|
|
|
2012-08-07 15:46:40 +02:00
|
|
|
self.level = None
|
|
|
|
self.jukebox = jukebox.Jukebox(
|
2012-08-08 13:26:55 +02:00
|
|
|
os.path.join(self.directory, "resources", "music"),
|
2012-08-07 15:46:40 +02:00
|
|
|
["basshit.ogg"])
|
|
|
|
self.jukebox.stop()
|
2012-08-07 21:23:10 +02:00
|
|
|
if not self.disable_music:
|
|
|
|
self.jukebox.play()
|
2012-08-07 15:46:40 +02:00
|
|
|
|
2012-08-08 19:42:57 +02:00
|
|
|
if not self.initial_goto_level:
|
2012-08-08 23:00:39 +02:00
|
|
|
self.menu = main_menu.MainMenu(self, graphics_dir)
|
2012-08-08 19:42:57 +02:00
|
|
|
else:
|
2012-08-08 23:00:39 +02:00
|
|
|
self.goto_level(self.initial_goto_level)
|
2012-08-08 19:42:57 +02:00
|
|
|
|
2012-08-07 13:21:01 +02:00
|
|
|
def run(self):
|
2012-08-07 14:00:49 +02:00
|
|
|
t = pygame.time.get_ticks()
|
|
|
|
dt = 0
|
2012-08-07 13:21:01 +02:00
|
|
|
while self.running:
|
2012-08-07 19:59:52 +02:00
|
|
|
dt = float(pygame.time.get_ticks() - t) / 1000
|
2012-08-07 14:00:49 +02:00
|
|
|
t = pygame.time.get_ticks()
|
|
|
|
self.update(t, dt)
|
2012-08-07 13:21:01 +02:00
|
|
|
self.draw()
|
|
|
|
self.clock.tick(self.speed)
|
|
|
|
|
2012-08-07 15:40:42 +02:00
|
|
|
|
|
|
|
def goto_level(self, level):
|
2012-08-12 19:47:55 +02:00
|
|
|
self.level_num = level
|
2012-08-08 23:00:39 +02:00
|
|
|
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)
|
2012-08-07 19:59:52 +02:00
|
|
|
self.objs.append(self.level)
|
2012-08-07 15:40:42 +02:00
|
|
|
|
2012-08-12 19:47:55 +02:00
|
|
|
def restart_level(self):
|
|
|
|
self.goto_level(self.level_num)
|
|
|
|
|
2012-08-07 15:40:42 +02:00
|
|
|
|
2012-08-07 14:00:49 +02:00
|
|
|
def update(self, t, dt):
|
|
|
|
"""
|
|
|
|
Update all game objects.
|
|
|
|
"""
|
|
|
|
# Retrieve and flush all events since last update call (this prevents
|
|
|
|
# event "bottlenecking"/lock-ups)
|
2012-08-07 15:40:42 +02:00
|
|
|
e = pygame.event.get()
|
|
|
|
for event in e:
|
2012-08-07 13:21:01 +02:00
|
|
|
# Stop the game when closing the window
|
|
|
|
if event.type == pygame.QUIT:
|
|
|
|
self.stop()
|
|
|
|
|
2012-08-07 14:00:49 +02:00
|
|
|
# Keep the music playing!
|
2012-08-07 21:23:10 +02:00
|
|
|
if not pygame.mixer.music.get_busy() and not self.disable_music:
|
2012-08-07 14:00:49 +02:00
|
|
|
self.jukebox.play()
|
|
|
|
|
|
|
|
# Update all objects
|
2012-08-09 15:51:59 +02:00
|
|
|
for obj in self.objs[:]:
|
2012-08-07 14:00:49 +02:00
|
|
|
if hasattr(obj, 'update'):
|
2012-08-07 15:40:42 +02:00
|
|
|
obj.update(e, t, dt)
|
2012-08-07 13:21:01 +02:00
|
|
|
|
2012-08-08 23:00:39 +02:00
|
|
|
self.menu.update(e, t, dt)
|
|
|
|
|
2012-08-07 13:21:01 +02:00
|
|
|
def draw(self):
|
2012-08-07 14:00:49 +02:00
|
|
|
"""
|
2012-08-07 15:40:42 +02:00
|
|
|
Draw all game objects.
|
2012-08-07 14:00:49 +02:00
|
|
|
"""
|
2012-08-08 15:03:39 +02:00
|
|
|
self.window.fill((0, 0, 0))
|
2012-08-08 23:00:39 +02:00
|
|
|
|
2012-08-09 15:51:59 +02:00
|
|
|
for obj in self.objs[:]:
|
2012-08-07 14:00:49 +02:00
|
|
|
if hasattr(obj, 'draw'):
|
|
|
|
obj.draw(self.window)
|
2012-08-08 23:00:39 +02:00
|
|
|
self.menu.draw(self.window)
|
|
|
|
|
2012-08-07 14:00:49 +02:00
|
|
|
pygame.display.flip()
|