Pygame error: mixer system not initialized

2019-07-02 22:18发布

I've just started a little game project and im trying to make it play a sound everytime a bullet is fired but i keep getting the same error:

pygame.error: mixer system not initialized

I don't get what i do wrong, here is my code:

import pygame, sys
from pygame.locals import *

theClock = pygame.time.Clock()

sound = pygame.mixer.Sound("bullet.mp3")

....

if event.type == KEYDOWN:
            if event.key == K_SPACE and shot_count == 0:
                sound.play()
                shot_y = h-50
                shot_x = x
            elif event.type == K_SPACE and shot_count == 1:
                shot_y_2 = h-50
                shot_x_2 = x
            print(h, ' ', shot_y, shot_count)
        if event.type == KEYUP:
            if event.key == K_SPACE and shot_count == 0:
                resetShot = 0 
            elif event.type == K_SPACE and shot_count == 1:
                resetShot = 0

2条回答
【Aperson】
2楼-- · 2019-07-02 22:33

You need to pygame.init() before using mixer/sound objects.

According to documentation, you should use OGG or WAV sound files.

查看更多
迷人小祖宗
3楼-- · 2019-07-02 22:41

I was making a Tetris game before two weeks and I had the same problem! What I did is inserting this before playing the sound and it worked.

pygame.mixer.init(44100, -16,2,2048)

Try it yourself and see if it works! I hope that helped

查看更多
登录 后发表回答