Add music playlist ability
authorTJ <git@iam.tj>
Sun, 10 Nov 2013 01:43:57 +0000 (01:43 +0000)
committerTJ <git@iam.tj>
Sun, 10 Nov 2013 01:43:57 +0000 (01:43 +0000)
pygame-scroller.py

index f862e87..a773ac8 100755 (executable)
@@ -55,9 +55,22 @@ class Channel:
   else:
    pygame.mixer.music.load(sound)
    pygame.mixer.music.play(loops)
+ def playlist(self, path):
+  self.music_path = path
+  self.playlist = [ f for f in os.listdir(self.music_path) if os.path.isfile(os.path.join(self.music_path, f)) ]
+  random.shuffle(self.playlist)
+  if DEBUG: print ("playlist=", self.playlist)
+  self.playlist_index = -1 # flag for 'start from the beginning of list'
+  self.playlist_next()
+  pygame.mixer.music.set_endevent(pygame.USEREVENT)
+ def playlist_next(self):
+  self.playlist_index = self.playlist_index + 1 if self.playlist_index < len(self.playlist)-1 else 0
+  if DEBUG: print("Channel.playlist_next() = [%d] %s" %(self.playlist_index, self.playlist[self.playlist_index] ))
+  pygame.mixer.music.load(os.path.join(self.music_path, self.playlist[self.playlist_index]))
+  pygame.mixer.music.play()
 
 # container for all game control and configuration items
-class Config:
+class Game:
  config = dict()
  soundtrack = Channel(channel_id=-1)
  sound_effects = Channel(channel_id=0)
@@ -86,7 +99,7 @@ class SeamlessBackground:
  def draw(self, screen, x_pos, step):
   """ x_pos == -1 requests the full screen be painted, not just the damaged left/right margins
   """
-  if DEBUG: print("SeamlessBackground.draw(screen, %d, %d)" % (x_pos, step))
+  #if DEBUG: print("SeamlessBackground.draw(screen, %d, %d)" % (x_pos, step))
   if self.image != None:
    resolution = screen.get_size()
    # adjust x_pos to be x coord of damaged rectangle (left or right side of screen)
@@ -106,17 +119,17 @@ class SeamlessBackground:
    if remainder < abs(step):
     # need more pixels because we're just wrapped around the background image
     patch.blit(self.image, (4,0), pygame.Rect(0, 0, remainder, resolution[1]))
-    if DEBUG: print("Wrap-around, remainder=%d" % remainder)
+    #if DEBUG: print("Wrap-around, remainder=%d" % remainder)
 
    scr_x_dest = resolution[0] - step if step > 0 else 0
-   if DEBUG: print("x_pos=%d, scr_x_dest=%d, bk_x_start=%d, bk_rect=%s" % (x_pos, scr_x_dest, bk_x_start, bk_rect))
+   #if DEBUG: print("x_pos=%d, scr_x_dest=%d, bk_x_start=%d, bk_rect=%s" % (x_pos, scr_x_dest, bk_x_start, bk_rect))
    screen.blit(patch, (scr_x_dest, 0))
   else:
    print("No image to draw")
 
 
 def main():
- myGame = Config(1500, 1000)
+ myGame = Game(900, 600)
  flags = pygame.OPENGL & pygame.DOUBLEBUF
  depth = 0
 
@@ -146,7 +159,7 @@ def main():
  y = resolution[1] / 2
 
  if myGame.config['sound'] == 1:
-  myGame.soundtrack.play(os.path.join("resources","Music","Rolemusic_-_Savage_Steel_Fun_Club.mp3"), loops=-1)
+  myGame.soundtrack.playlist(os.path.join(os.getcwd(), "resources", "Music"))
 
  auto_scroll = False
  movement = 0
@@ -189,12 +202,16 @@ def main():
 
   # process events
   for event in pygame.event.get():
-   if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_q):
+   if event.type == pygame.QUIT or (event.type == pygame.KEYUP and event.key == pygame.K_q):
     myGame.play = False
-   if event.type == pygame.KEYDOWN:
+   if event.type == pygame.KEYUP:
     k = event.key
     if k == pygame.K_p:
      time.sleep(10)
+   if event.type == pygame.USEREVENT:
+    if DEBUG: print("pygame.USEREVENT received; calling playlist_next()")
+    myGame.soundtrack.playlist_next()
+
   if not auto_scroll:
     movement = 0
   keys_pressed = pygame.key.get_pressed()