Added a jukebox and music.
This commit is contained in:
parent
f353d27d8c
commit
775e4a72a8
Binary file not shown.
|
@ -72,4 +72,3 @@ if __name__ == '__main__':
|
||||||
|
|
||||||
game = robotgame.game.Game(window)
|
game = robotgame.game.Game(window)
|
||||||
game.start()
|
game.start()
|
||||||
help(robotgame.game.Game)
|
|
||||||
|
|
|
@ -24,18 +24,26 @@
|
||||||
The game. Handles everything.
|
The game. Handles everything.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
import pygame
|
import pygame
|
||||||
|
|
||||||
|
import jukebox
|
||||||
|
|
||||||
class Game(object):
|
class Game(object):
|
||||||
"""Create an object to handle the game."""
|
"""Create an object to handle the game."""
|
||||||
def __init__(self, window, running=False, speed=30):
|
def __init__(self, window, running=False, speed=30):
|
||||||
self.__dict__.update(locals())
|
self.__dict__.update(locals())
|
||||||
|
|
||||||
self.active_objs = []
|
self.objs = []
|
||||||
self.passive_objs = []
|
|
||||||
|
|
||||||
self.clock = pygame.time.Clock()
|
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):
|
def start(self):
|
||||||
self.running = True
|
self.running = True
|
||||||
self.run()
|
self.run()
|
||||||
|
@ -43,32 +51,44 @@ class Game(object):
|
||||||
def stop(self):
|
def stop(self):
|
||||||
self.running = False
|
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):
|
def run(self):
|
||||||
|
t = pygame.time.get_ticks()
|
||||||
|
dt = 0
|
||||||
while self.running:
|
while self.running:
|
||||||
self.update()
|
dt = t - pygame.time.get_ticks()
|
||||||
|
t = pygame.time.get_ticks()
|
||||||
|
self.update(t, dt)
|
||||||
self.draw()
|
self.draw()
|
||||||
self.clock.tick(self.speed)
|
self.clock.tick(self.speed)
|
||||||
|
|
||||||
def update(self):
|
def update(self, t, dt):
|
||||||
# Get all events since last update call (this prevents event
|
"""
|
||||||
# "bottlenecking"/lock-ups)
|
Update all game objects.
|
||||||
|
"""
|
||||||
|
# Retrieve and flush all events since last update call (this prevents
|
||||||
|
# event "bottlenecking"/lock-ups)
|
||||||
events = pygame.event.get()
|
events = pygame.event.get()
|
||||||
for event in events:
|
for event in events:
|
||||||
# Stop the game when closing the window
|
# Stop the game when closing the window
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
self.stop()
|
self.stop()
|
||||||
|
|
||||||
t = pygame.time.get_ticks()
|
# Keep the music playing!
|
||||||
for obj in self.active_objs:
|
if not pygame.mixer.music.get_busy():
|
||||||
obj.update(t)
|
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):
|
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()
|
||||||
|
|
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
#
|
||||||
|
# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
|
||||||
|
#
|
||||||
|
# jukebox.py
|
||||||
|
# --------------------
|
||||||
|
# date created : Tue Aug 7 2012
|
||||||
|
# copyright : (C) 2012 Sakse Dalum
|
||||||
|
# maintained by : Sakse Dalum <don_s@hongabar.org>
|
||||||
|
|
||||||
|
"""
|
||||||
|
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()
|
Loading…
Reference in New Issue