user2874977
user2874977

Reputation: 79

PYGAME: how to activate event by pressing a key

So I'm having trouble with activating sound, when pressed a key.

what i have so far is

if event.type == pygame.key(K_a):
    self.sound.play()

I get the error code called global name 'K_a' is not defined.

If anyone can help me correct this code? Thanks!

Upvotes: 4

Views: 495

Answers (1)

Weiner Nir
Weiner Nir

Reputation: 1475

You should use pygame.K_a. This way: if event.type == pygame.key(pygame.K_a):. If it doesn't works for you, then you've probably didn't import pygame as needed. Import pygame this way: from pygame import *. You may have an error with pygame.key, Because you shouldn't use it this way. The best practice is to use the following line: if event.type == pygame.K_a:

Upvotes: 3

Related Questions