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