user13668033
user13668033

Reputation:

Pygame mixer playing sounds when I don't want it to

I'm trying to make this program that uses the keyboard, or a gh guitar with joytokey. I'm trying to set the "fret" variable when you push 1,2,3,4, or 5. And play the correct sound when you press k or l. But when I press 1,2,3,4, or 5, it plays the sound. Heres the important bit of the code:

def playFret(fret):
    if fret == 1:
        pygame.mixer.Sound.play(eguitargreen)
    if fret == 2:
        pygame.mixer.Sound.play(eguitarred)
    if fret == 3:
        pygame.mixer.Sound.play(eguitaryellow)
    if fret == 4:
        pygame.mixer.Sound.play(eguitarblue)
    if fret == 5:
        pygame.mixer.Sound.play(eguitarorange)


while jamtime:
    
    for event in pygame.event.get():
        
        if event.type == pygame.QUIT: 
              jamtime = False 
        elif event.type == pygame.KEYDOWN:
    
        
            if event.key == pygame.K_x:
                jamtime = False
            if event.key == pygame.K_1:
                fret = 1
            if event.key == pygame.K_2:
                fret = 2
            if event.key == pygame.K_3:
                fret = 3
            if event.key == pygame.K_4:
                fret = 4
            if event.key == pygame.K_5:
                fret = 5
                
            if event.key == pygame.K_r or pygame.K_l:
                playFret(fret)

Upvotes: 0

Views: 41

Answers (1)

BWallDev
BWallDev

Reputation: 375

Try changing your k and l key check to:

if event.key == pygame.K_r or event.key == pygame.K_l:
    playFret(fret)

The way your if statement works, isn't how you expect it to work. For example, what do you think the following code will print:

num = 7

if num == 4 or 5:
  print("yup")
else:
  print("nope")

Output:

yup

Upvotes: 1

Related Questions