Added a jukebox and music.

This commit is contained in:
Sakse Dalum
2012-08-07 14:00:49 +02:00
parent f353d27d8c
commit 775e4a72a8
4 changed files with 85 additions and 19 deletions

View File

@@ -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()