Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
    
You should call pre_init before init. –  congusbongus May 5 '14 at 5:54

1 Answer 1

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()?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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