diff --git a/resources/music/basshit.ogg b/resources/music/basshit.ogg new file mode 100644 index 0000000..3693246 Binary files /dev/null and b/resources/music/basshit.ogg differ diff --git a/robotgame.py b/robotgame.py index f76fd62..254e72b 100755 --- a/robotgame.py +++ b/robotgame.py @@ -72,4 +72,3 @@ if __name__ == '__main__': game = robotgame.game.Game(window) game.start() - help(robotgame.game.Game) diff --git a/robotgame/game.py b/robotgame/game.py index 3da4720..20d5179 100644 --- a/robotgame/game.py +++ b/robotgame/game.py @@ -24,18 +24,26 @@ The game. Handles everything. """ +import os import pygame +import jukebox + 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.objs = [] self.clock = pygame.time.Clock() + self.jukebox = jukebox.Jukebox(os.path.abspath(os.path.join("resources", + "music")), + ["basshit.ogg"]) + + self.ticks = self.prev_ticks = pygame.time.get_ticks() + def start(self): self.running = True self.run() @@ -43,32 +51,44 @@ class Game(object): 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): + t = pygame.time.get_ticks() + dt = 0 while self.running: - self.update() + dt = t - pygame.time.get_ticks() + t = pygame.time.get_ticks() + self.update(t, dt) self.draw() self.clock.tick(self.speed) - def update(self): - # Get all events since last update call (this prevents event - # "bottlenecking"/lock-ups) + def update(self, t, dt): + """ + Update all game objects. + """ + # Retrieve and flush 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) + # Keep the music playing! + if not pygame.mixer.music.get_busy(): + self.jukebox.play() + + # Update all objects + for obj in self.objs: + if hasattr(obj, 'update'): + obj.update(t, dt) + self.prev_ticks = pygame.time.get_ticks() def draw(self): - pass + """ + Update all game objects. + """ + self.window.fill((0, 0, 0)) + for obj in self.objs: + if hasattr(obj, 'draw'): + obj.draw(self.window) + pygame.display.flip() diff --git a/robotgame/jukebox.py b/robotgame/jukebox.py new file mode 100644 index 0000000..b64f37a --- /dev/null +++ b/robotgame/jukebox.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 . +# +# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' +# +# jukebox.py +# -------------------- +# date created : Tue Aug 7 2012 +# copyright : (C) 2012 Sakse Dalum +# maintained by : Sakse Dalum + +""" +The jukebox. Handles playback of background music. +""" + +import os +import pygame +import random + +class Jukebox(object): + """Create an object to handle music playback.""" + def __init__(self, music_dir, music_files, volume=100, playing=True): + self.__dict__.update(locals()) + + self.iterator = 0 + + def play(self): + pygame.mixer.music.load( + os.path.join(self.music_dir, + self.music_files[self.iterator + % len(self.music_files)])) + pygame.mixer.music.play() + self.iterator += 1 + + def stop(self): + pygame.mixer.music.stop()