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

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

    raise UserWarning, "could not load or play soundfiles"

if pressedkeys[pygame.K_SPACE]:

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.

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

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


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.