2
\$\begingroup\$

I'm trying to play music when a key is pressed in pygame, then print a message.

When I run this script:

import pygame
import time

pressedkeys = pygame.key.get_pressed()

pygame.init()                      #initialize pygame
pygame.mixer.pre_init(44100, -16, 2, 2048) # setup mixer to avoid sound lag

try:
    firstMusic = pygame.mixer.Sound("C:\(pathForMyMusicFile).wav")  #load sound
    secondMusic = pygame.mixer.Sound("C:\(pathForMy2ndMusicFile).wav")  #load sound

except:
    raise UserWarning, "could not load or play soundfiles"

if pressedkeys[pygame.K_SPACE]:
    firstMusic.play(-1)
    time.sleep(2)

I get this error:

pressedkeys = pygame.key.get_pressed()
pygame.error: video system not initialised
Script terminated.

What am I doing wrong?

I'm using Stani's Python Editor.

\$\endgroup\$
1
  • \$\begingroup\$ You should call pre_init before init. \$\endgroup\$ – congusbongus May 5 '14 at 5:54
1
\$\begingroup\$

Random assumption since I've never used Pygame:

You can't receive/check keyboard events before initializing Pygame first. So move down your pressedkeys = ... line to be after pygame.init()?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.