Thread: Python pygames

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    9
    Rep Power
    0

    Python pygames


    Hi I'm new to this and want to learn.I want to download a image and a sound file and put it in a pygame.When I code it the program gives me a error message stating that it can't find the sound file.How do you import it into the game.Thanks
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,706
    Rep Power
    1959
    Please post the error message you received (copy/paste).

    Please post the code you have tried, which give this message. Use code tags to keep the code format.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    9
    Rep Power
    0
    Originally Posted by MrFujin
    Please post the error message you received (copy/paste).

    Please post the code you have tried, which give this message. Use code tags to keep the code format.
    This is part of the code.

    # Fish Game

    # By antiloquax

    import pygame, random
    from pygame.locals import *
    pygame.init()
    clock = pygame.time.Clock()

    screen = pygame.display.set_mode([600,400])
    pygame.display.set_caption("Fish Game")

    music = pygame.mixer.Sound("tune.wav")
    music.play(-1)
    toy = pygame.mixer.Sound("toy.wav")
    burp = pygame.mixer.Sound("burp.wav")

    Here is where I get an error

    IDLE_tmp_0czq8u
    python 3.3.3(v3.3.3:c3896275c0f6

    >>>
    >>>
    Traceback (most recent call last):
    File "C:\Python33\Fish Game.py" , line 13, in <module>
    music = pygame.mixer.Sound("tune.wav")
    pygame.error: Unable to open file 'tune.wav'
    >>>
  6. #4
  7. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    618
    Rep Power
    65
    The sound file "tune.wav" has to be in the working folder, or you have to give the full path.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    9
    Rep Power
    0
    Originally Posted by Dietrich
    The sound file "tune.wav" has to be in the working folder, or you have to give the full path.
    Thanks for all your help,but like I said I'm new to this.

    How to you put the sound wav file in the working folder or give it a full path.When I downloaded it,it was put in it's own folder and that folder is in python's 3.3 folder along with my program.

    How does python open the wav's folder.Thanks.
  10. #6
  11. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    618
    Rep Power
    65
    This code sample might help ...
    Code:
    import os
    import pygame
    
    def load_sound(sound_filename, directory):
        """
        load the sound file from the given directory
        return the sound object
        """
        fullname = os.path.join(directory, sound_filename)
        sound = pygame.mixer.Sound(fullname)
        return sound
            
    pygame.init()
    
    screen = pygame.display.set_mode([600, 400])
    pygame.display.set_caption("Play a Wave File")
    
    # pick a wave (.wav) sound file you have in the given directory
    directory = "C:/Windows/Media"
    chimes = load_sound("chimes.wav", directory)
    
    chimes.play()
    
    # event loop and exit conditions
    # use escape key or display window x click
    while True:
        for event in pygame.event.get():
            if (event.type == pygame.QUIT or 
                event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
                pygame.quit()
                raise SystemExit
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    9
    Rep Power
    0
    Thanks Dietrich

    I finally got it working.Like you said before the wav file has to be in the working directory.I finally figured out how to do it.Thanks again.

    Bob

IMN logo majestic logo threadwatch logo seochat tools logo