[英]How do you continuously play a note in Pygame?
Python 2.7 Python 2.7
Im trying to create a piano-like thing in pygame where when a key is pressed a note plays. 我试图在pygame中创建一个类似钢琴的东西,当按下一个键时,一个音符播放。 My problem is that I can not figure out how to get the note to play forever while the key is held down. 我的问题是,当按键被按下时,我无法弄清楚如何让音符永远播放。 I tried to make a while loop but I couldnt figure out how to leave it. 我试着做一个while循环,但我无法弄清楚如何离开它。
import pygame
pygame.mixer.pre_init(44100, -16, 2, 2048)
pygame.init()
size = width, height = 800, 400
screen = pygame.display.set_mode((size))
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if event.type == pygame.KEYDOWN:
if event.type == pygame.KEYUP:
break
elif event.key == pygame.K_i:
pygame.mixer.music.load('file.wav') #A
pygame.mixer.music.play()
elif event.key == pygame.K_o:
pygame.mixer.Sound('file.wav').play() #B
elif event.key == pygame.K_e:
pygame.mixer.Sound('file.wav').play() #C
elif event.key == pygame.K_r:
pygame.mixer.Sound('file.wav').play() #D
elif event.key == pygame.K_t:
pygame.mixer.Sound('file.wav').play() #E
elif event.key == pygame.K_y:
pygame.mixer.Sound('file.wav').play() #F
elif event.key == pygame.K_u:
pygame.mixer.Sound('file.wav').play() #G
while running:
keys = pygame.key.get_pressed() #checking pressed keys
if keys[pygame.K_UP]:
pygame.mixer.Sound('file.wav').stop()
if keys[pygame.K_DOWN]:
pygame.mixer.Sound('file.wav').play()
While a key is pressed it will continue to play that key but when it isn't pressed anymore it will stop. 当按下一个键时,它将继续播放该键,但当它不再按下时它将停止。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.