1

Pygame sound effects do not play for a few seconds if the corresponding buttons (to play the sound effect) is pressed for four consecutive times. I'm currently trying to experiment with different keys of the piano.

I've tried removing the pygame clock object. I've tried making the Sound.play() into a function, as found in the code.

import pygame
pygame.init()

###LordKeys###

A5 = pygame.mixer.Sound('PianoKeys/A5.wav')
A6 = pygame.mixer.Sound('PianoKeys/A6.wav')


def A56():
    A5.play()
    A6.play()

###############################################
run = True
win = pygame.display.set_mode((700,700))
pygame.display.set_caption("Piano Gen")
while run:
    win.fill((255,255,255))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:
                A56()

    pygame.display.update()

Expected Results: the sound effect to play when the key is pressed, regardless of frequency.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Tay
  • 27
  • 2

2 Answers2

0

You have to use the Sound function to play the sound. The sound variable you have created does not have a play() function.

Replace

A5.play()
A6.play()

with

pygame.mixer.Sound.play(A5)
pygame.mixer.Sound.play(A6)
Ham Sandwich
  • 11
  • 1
  • 1
  • unfortunately, it doesn't seem to work despite changing the corresponding parts. Pressing 'a' five times still plays it for four times. thank you though – Tay Jul 27 '19 at 06:21
  • `A5` and `A6` are instances of [pygame.mixer.Sound](https://www.pygame.org/docs/ref/mixer.html#pygame.mixer.Sound), an instance objecet of this class provides the method [`.play()`](https://www.pygame.org/docs/ref/mixer.html#pygame.mixer.Sound.play). So your'answer is obviously wrong. Did you try it? Read the question *"[...] sound effects do not play [...] if the corresponding button [...] is pressed for four consecutive times"*. – Rabbid76 Jul 27 '19 at 07:05
0

So I decided to play the sounds on a channel and that solved the problem!

def A56():
    channel1.play(A5)
    channel1.play(A6)
Tay
  • 27
  • 2