0

UPDATE: I used a different sound function in pygame to try to troubleshoot. This was my new program.

#pygame  mixer testing
import pygame

pygame.init()
sound = pygame.mixer.music.load("jan_amit.wav")

This was the error.

Traceback (most recent call last):
  File "C:\Users\Will\Desktop\Programming\Python\Programs\shmup\snd\pygame  mixer testing.py", line 5, in <module>
    pygame.mixer.music.load("jan_amit.wav")
pygame.error: Unknown WAVE data format

How do change a wav data format?

(OLD POST)---I'm a beginner, and I'm trying to learn how to use pygame. However, when I try to load sounds into my game with pygame.mixer.Sound() I get this error:

C:\Users\Will\Desktop\Programming\Python\Programs\shmup\snd\bullet_shoot.wav
Traceback (most recent call last):
  File "C:\Users\Will\Desktop\Programming\Python\Programs\shmup\shmup.py", line 127, in <module>
    shoot_sound = pygame.mixer.Sound(path.join(snd_dir,"bullet_shoot.wav"))
pygame.error: Unable to open file 'C:\\Users\\Will\\Desktop\\Programming\\Python\\Programs\\shmup\\snd\\bullet_shoot.wav'
[Finished in 6.5s]

It appears that somehow this pygame function is inserting an additional backslash next to every other backslash. So to troubleshoot, I printed out to the string I was feeding into the function. Sure enough, single backslashes.

C:\Users\Will\Desktop\Programming\Python\Programs\shmup\snd\bullet_shoot.wav

Here is the complete code, because I'm unsure of what's going on.

#shmup
#music by Jan Amit
import pygame
import random
from os import path

img_dir = path.join(path.dirname(__file__),'img')
snd_dir = path.join(path.dirname(__file__),'snd')

WIDTH = 480
HEIGHT = 480
FPS = 60

WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
YELLOW = (255,255,0)

pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("Shoot'em Up!")
clock = pygame.time.Clock()

font_name = pygame.font.match_font("arial")
def draw_text(surf,text,size,x,y):
    font = pygame.font.Font(font_name, size)
    text_surface = font.render(text, True, WHITE)
    text_rect = text_surface.get_rect()
    text_rect.midtop = (x, y)
    surf.blit(text_surface, text_rect)

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.transform.scale(player_img,(50,38))
        self.image.set_colorkey(BLACK)
        self.rect = self.image.get_rect()
        self.radius = 19
        self.rect.centerx = WIDTH / 2
        self.rect.bottom = HEIGHT - 10
        self.speedx = 0

    def update(self):
        speed = 8
        self.speedx = 0
        keystate = pygame.key.get_pressed()
        if not keystate[pygame.K_LEFT] or not keystate[pygame.K_RIGHT]:
            if keystate[pygame.K_LEFT]:
                self.speedx = -speed
            if keystate[pygame.K_RIGHT]:
                self.speedx = speed
        self.rect.x += self.speedx
        if self.rect.right > WIDTH:
            self.rect.right = WIDTH
        if self.rect.left < 0:   
            self.rect.left = 0

    def shoot(self):
        bullet = Bullet(self.rect.centerx, self.rect.top)
        all_sprites.add(bullet)
        bullets.add(bullet)
        shoot_sound.play()

class Mob(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image_orig = meteor_img
        self.image_orig.set_colorkey(BLACK)
        self.image = self.image_orig.copy()
        self.rect = self.image.get_rect()
        self.radius = int(self.rect.width * .85 / 2)
        self.rect.x = random.randrange(WIDTH - self.rect.width)
        self.rect.y = random.randrange(-100,-40)
        self.speedy = random.randrange(1,8)
        self.speedx = random.randrange(-3,3)
        self.rot = 0
        self.rot_speed = random.randrange(-8,8)
        self.last_update = pygame.time.get_ticks()

    def update(self):
        self.rotate()
        self.rect.y += self.speedy
        self.rect.x += self.speedx
        if self.rect.top > HEIGHT + 10 or self.rect.left < -20 or self.rect.right > WIDTH + 20:
            self.rect.x = random.randrange(WIDTH - self.rect.width)
            self.rect.y = random.randrange(-100,-40)
            self.speedy = random.randrange(1,8)
            self.speedx = random.randrange(-3,3)

    def rotate(self):
        now = pygame.time.get_ticks()
        if now - self.last_update > 50:
            self.last_update = now
            self.rot = (self.rot + self.rot_speed) % 360
            new_image = pygame.transform.rotate(self.image_orig,self.rot)
            old_center = self.rect.center
            self.image = new_image
            self.rect = self.image.get_rect()
            self.rect.center = old_center

class Bullet(pygame.sprite.Sprite):
    def __init__(self,x,y):
        pygame.sprite.Sprite.__init__(self)
        self.image = bullet_img 
        self.image.set_colorkey(BLACK)
        self.rect = self.image.get_rect()
        self.rect.bottom = y
        self.rect.centerx = x
        self.speedy = -10

    def update(self):
        self.rect.y += self.speedy
        if self.rect.bottom < 10:
            self.kill()

#load all game graphics
background = pygame.image.load(path.join(img_dir,'space_field.png')).convert()
background_rect = background.get_rect()
player_img = pygame.image.load(path.join(img_dir,"playerShip1_orange.png")).convert()
meteor_img = pygame.image.load(path.join(img_dir,"meteorBrown_med1.png")).convert()
bullet_img = pygame.image.load(path.join(img_dir,"laserBlue05.png")).convert()
#load game sounds
print(path.join(snd_dir, 'bullet_shoot.wav'))
shoot_sound = pygame.mixer.Sound(path.join(snd_dir,"bullet_shoot.wav"))

all_sprites = pygame.sprite.Group()
mobs = pygame.sprite.Group()
bullets = pygame.sprite.Group()
player = Player()
all_sprites.add(player)
for i in range(8):
    m = Mob()
    all_sprites.add(m)
    mobs.add(m)
score = 1

#game loop
GAME_RUNNING = True
while GAME_RUNNING == True:
    clock.tick(FPS)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            GAME_RUNNING = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                player.shoot()

    all_sprites.update()

    hits = pygame.sprite.groupcollide(mobs,bullets,True,True)
    for hit in hits:
        score += 1
        m = Mob()
        all_sprites.add(m)
        mobs.add(m)

    hits = pygame.sprite.spritecollide(player, mobs, False, pygame.sprite.collide_circle)
    if hits:
        GAME_RUNNING = False

    #draw and render
    screen.fill(BLACK)
    screen.blit(background, background_rect)
    all_sprites.draw(screen)
    draw_text(screen,str(score), 18, WIDTH/2, 15)
    pygame.display.flip()

pygame.quit()
Will
  • 21
  • 6
  • TL;TR. Could you please simplify your example? – Laurent LAPORTE Jan 12 '17 at 16:04
  • did you try to open file in some player using full path from PyGame `C:\Users\Will\Desktop\Programming\Python\Programs\shmup\snd\bullet_shoot.wav` ? – furas Jan 12 '17 at 16:04
  • yes, I can open the sound using that directory – Will Jan 12 '17 at 16:07
  • The `\\'s` in the exception string is python showing how the string would need to be entered. You need to escape a `\ ` with a `\ `. – Stephen Rauch Jan 12 '17 at 16:07
  • BTW: to make code better organized you could put all classes and functions before `pygame.init()` – furas Jan 12 '17 at 16:07
  • @StephenRauch Ok, so I don't need to worry about the double backslahes in the error. That's just the way python prints them out? I'm not sure what you mean by "escape" – Will Jan 12 '17 at 16:12
  • PyGame doc: `"The Sound can be loaded from an OGG audio file or from an uncompressed WAV."` - maybe your `wav` is incorrect for `PyGame`. – furas Jan 12 '17 at 16:12
  • BTW: to check slashes you can try to open without full path - you have to only put file in the same folder as script. – furas Jan 12 '17 at 16:14
  • @furas Well I know it's a wav file. But how to determine if it's compressed or uncompressed? Also, I moved bullet_shoot.wav to my script folder, and changed the code to shoot_sound = pygame.mixer.Sound("bullet_shoot.wav") – Will Jan 12 '17 at 16:21
  • And got pygame.error: Unable to open file 'bullet_shoot.wav' – Will Jan 12 '17 at 16:21
  • SO: [compressed and uncompressed .wav files](http://stackoverflow.com/questions/560011/compressed-and-uncompressed-wav-files). I think that any music player or converter should display information about your wav file. – furas Jan 12 '17 at 16:48
  • try giving absolute path to the 'bullet_shoot.wav'. e.g `import pygame` `pygame.init()` `pygame.mixer.init()` `soundx =pygame.mixer.Sound("C:\Users\Will\Desktop\Programming\Python\Programs\shmup\snd\bullet_shoot.wav")` `soundx.play()` in a seperate program and see if it works. – Anil_M Jan 13 '17 at 04:59

0 Answers0