Skip to content

Commit

Permalink
If going to a new area with same music, keep playing
Browse files Browse the repository at this point in the history
  • Loading branch information
georgeklees committed Feb 17, 2015
1 parent cce7e1b commit 0885440
Show file tree
Hide file tree
Showing 25 changed files with 23 additions and 9 deletions.
Binary file added engine/__pycache__/graphics.cpython-34.pyc
Binary file not shown.
Binary file added engine/__pycache__/level.cpython-34.pyc
Binary file not shown.
Binary file added engine/__pycache__/music.cpython-34.pyc
Binary file not shown.
Binary file added engine/__pycache__/objects.cpython-34.pyc
Binary file not shown.
Binary file added engine/__pycache__/save.cpython-34.pyc
Binary file not shown.
Binary file added engine/__pycache__/title.cpython-34.pyc
Binary file not shown.
Binary file added engine/__pycache__/worlds.cpython-34.pyc
Binary file not shown.
Binary file added engine/enemies/__pycache__/__init__.cpython-34.pyc
Binary file not shown.
Binary file added engine/heroes/__pycache__/__init__.cpython-34.pyc
Binary file not shown.
Binary file added engine/heroes/__pycache__/mario.cpython-34.pyc
Binary file not shown.
Binary file added engine/heroes/__pycache__/objects.cpython-34.pyc
Binary file not shown.
Binary file added engine/heroes/__pycache__/sprites.cpython-34.pyc
Binary file not shown.
Binary file added engine/items/__pycache__/__init__.cpython-34.pyc
Binary file not shown.
Binary file added engine/items/__pycache__/hp.cpython-34.pyc
Binary file not shown.
Binary file added engine/items/__pycache__/objects.cpython-34.pyc
Binary file not shown.
Binary file added engine/items/__pycache__/powerups.cpython-34.pyc
Binary file not shown.
Binary file added engine/items/__pycache__/sprites.cpython-34.pyc
Binary file not shown.
20 changes: 17 additions & 3 deletions engine/level.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ def str2list(string):
string = string.replace('[','')
string = string.replace(']','')

print(string)

return string.split(',')

class Background(pyglet.sprite.Sprite):
Expand All @@ -42,6 +40,7 @@ def __init__(self, filename, mode):
# Create an object list and graphics batch for the area
self.objects = []
self.batch = pyglet.graphics.Batch()
self.music = None

self.background = pyglet.graphics.OrderedGroup(0)
self.foreground = pyglet.graphics.OrderedGroup(1)
Expand All @@ -51,6 +50,7 @@ def __init__(self, filename, mode):
elif mode == 1:
pass
def read(self, filename):
"""Read area data from a data file into an Area object"""
# Open the area file
fin = open(filename, 'r')

Expand All @@ -71,7 +71,8 @@ def read(self, filename):
if name == "Background":
obj = Background(img=pyglet.image.load(properties), x=x, y=y, batch=self.batch, group=self.background)
if name == "MusicPlayer":
obj = music.MusicPlayer(name=properties)
self.music = music.MusicPlayer(name=properties)
continue

# Heroes
if name == "Player":
Expand Down Expand Up @@ -103,6 +104,7 @@ def read(self, filename):

self.objects.append(obj)
def play(self):
"""Switch to this area"""
global current_area

old_area = current_area
Expand All @@ -123,7 +125,17 @@ def play(self):

# Switch to the new graphics
graphics.set_current_batch(self.batch)

# If the old music is different, stop it and begin ours from 0
if old_area:
if self.music.sample_name != old_area.music.sample_name:
old_area.music.pause()
self.music.seek(0.0)
self.music.play()
else:
self.music.play()
def end(self):
"""End gameplay of this area"""
window = graphics.get_current_window()

for obj in self.objects:
Expand All @@ -142,6 +154,7 @@ def __init__(self, filename, mode):
elif mode == 1:
pass
def read(self, filename):
"""Read each area into memory"""
# Open the area file
fin = open(filename, 'r')

Expand All @@ -151,6 +164,7 @@ def read(self, filename):
area = Area(os.path.dirname(filename) + '/' + line.strip('\n'), 0)
self.areas.append(area)
def play(self):
"""Switch to the first area of this level"""
global current_level

current_level = self
Expand Down
11 changes: 5 additions & 6 deletions engine/music.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import pyglet

# Music names
# Music sample names
music = {'title':'../music/ending_credits.mp3'}

class MusicPlayer(pyglet.media.Player):
def __init__(self, name):
# Initialize the Player class
super().__init__()

# Load the music file
# Load the sample and queue it on our player
source = pyglet.media.load(music[name])
self.queue(source)
def on_load(self):
self.play()
def on_unload(self):
self.pause()

# Set our sample name
self.sample_name = name


Binary file added engine/npc/__pycache__/__init__.cpython-34.pyc
Binary file not shown.
Binary file added engine/ui/__pycache__/__init__.cpython-34.pyc
Binary file not shown.
Binary file added engine/ui/__pycache__/button.cpython-34.pyc
Binary file not shown.
Binary file added engine/ui/__pycache__/text.cpython-34.pyc
Binary file not shown.
Binary file added engine/villains/__pycache__/__init__.cpython-34.pyc
Binary file not shown.
1 change: 1 addition & 0 deletions levels/special/title_instructions.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Background 0 0 ../sprites/backgrounds/title/title.png
MusicPlayer 0 0 title
Text 240 240 [Hello world,12,black]

0 comments on commit 0885440

Please sign in to comment.