[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] What is wrong with this



On Sat, Jul 14, 2007 at 02:31:20PM -0400, Jason Coggins wrote:
> I am trying to create a very simple program that makes a list of the
> music files in a folder, sorts the files into alphabetical order and
> then plays each file in the folder.  The program below plays the first
> file in the folder and then freezes without playing any of the
> remaining songs.

I'm glad you found the problem, but I cannot refuse the opportunity to
offer some generic Python style suggestions.

> --------------------------------------------------------------------------------
> 
> import pygame, os
> 
> def findFiles(location):
>     files = []
>     for item in os.listdir(location):
>         files.append(item)

This is a bit pointless.  You're taking a list of files and converting
it to another list, one item at a time.

    files = os.listdir(location)

would work perfectly well.

>     files.sort()
>     return files

(And if you don't care about older Python versions, you could do

  def findFiles(location):
      return sorted(os.listdir(location)

)

> def playMusic(music)
>     clock=pygame.time.Clock()

I cringe when I see an assignment statement squished together without
any spaces around the '='.

>     pygame.mixer.music.load(music)
>     pygame.mixer.music.play(0, 0.0)
>     while pygame.mixer.music.get_busy():
>         clock.tick(300)
>     return

Empty returns at the end of a function are a bit pointless.

> def main():
>     pygame.init()
>     pygame.mixer.init()
>     destinationPath = "/home/owner/Desktop/playList"
>     os.chdir(destinationPath)
>     playList = []

This assignment is pointless.  You're reassigning a different value to
playList on the very next line.

>     playList = findFiles(destinationPath)
>     for item in playList:
>         playMusic(item)
> 
> --------------------------------------------------------------------------------------------

Cheers!
Marius Gedminas
-- 
C, n:
        A programming language that is sort of like Pascal except more like
        assembly except that it isn't very much like either one, or anything
        else.  It is either the best language available to the art today, or
        it isn't.
                -- Ray Simard

Attachment: signature.asc
Description: Digital signature