更新窗口标题并同时使用 pygame 播放声音



我一直在尝试在pygame窗口中弹出一个带有颜色和音频的单词一段时间,但是我一直收到此错误导入pygame.font失败,我无法弄清楚。 (我有OSX)。

我认为下一个最好的办法是在播放音频并显示颜色时在窗口标题(标题)中包含单词。在这一点上,我唯一的问题是它在提示音之后更新标题,而不是在期间。 我该如何更改此设置?

我宁愿搬到Tkinter,但我认为你不能让计算机产生频率的声音。

#!/usr/bin/python
#Dependencies
import pygame,colorsys,time
import numpy as np
from pygame.locals import *
#Primordial
duration = 2.5
window_size = (320,320)#(320, 568)#(1366, 720)
bits = 16
sample_rate = 44100
n_samples = int(round(duration*sample_rate))
qnty = 3
#basicfont = pygame.font.SysFont(None, 48)
#Setup
pygame.mixer.pre_init(44100, -bits, 2)
pygame.init()
#setup our np array to handle 16 bit ints, which is what we set our mixer to expect with "bits" up above
buf = np.zeros((n_samples, 2), dtype = np.int16)
max_sample = 2**(bits - 1) - 1
D_value_freq = {
                "Gamma1" : 300,
                "Gamma2" : 350,    
                "Alpha" :  400, 
                "Beta1" : 450,
                "Beta2" : 500,
                "Cappa" : 550,
                "Delta1" : 600,
                "Delta2" : 650,
                "Epsilon1" : 700,
                "Epsilon2" : 750,
                "Feta" : 800
                }
D_value_color = {
                "Gamma1" : (45,0,45),
                "Gamma2" : (70,0,43),
                "Alpha": (100,39,43),
                 "Beta1" : (100,93,22),
                "Beta2" : (60,100,56),
                "Cappa" : (16,100,84),
                "Delta1" : (12,100,91),
                "Delta2" : (34,49,100),
                "Epsilon1" : (2,32,100),
                "Epsilon2" : (27,44,92),
                "Feta" : (34,65,62)
                }       
for i in range(qnty):
    #Choose note
    value = np.random.choice(D_value_color.keys(),size=1)[0]
    color, frequency = D_value_color[value], D_value_freq[value]
    frequency_l = frequency_r = frequency/2.0
    #Color screen
    screen = pygame.display.set_mode(window_size, pygame.HWSURFACE | pygame.DOUBLEBUF)
    pygame.display.set_caption(value)
    screen.fill(color)
    pygame.display.update()
    #Sounds
    for s in range(n_samples):
        t = float(s)/sample_rate    # time in seconds
        #grab the x-coordinate of the sine wave at a given time, while constraining the sample to what our mixer is set to with "bits"
        buf[s][0] = int(round(max_sample*np.sin(2*np.pi*frequency_l*t)))        # left
        buf[s][1] = int(round(max_sample*0.5*np.sin(2*np.pi*frequency_r*t)))    # right

    sound = pygame.sndarray.make_sound(buf)
    #play once, then loop forever
    sound.play(loops = 0)
    time.sleep(duration)

也许它不能回答你的问题,但这段代码在 Linux Mint 上对我有用。它会在声音之前更改字幕 - 但它仅在OSX上才有问题。

import pygame
import numpy as np
#
# pygame (simple) template by furas
#
# === constants == (UPPER_CASE names)
# Primordial
DURATION = 2.5
WINDOWS_SIZE = (320,320) #(320, 568)#(1366, 720)
BITS = 16
SAMPLE_RATE = 44100
N_SAMPLES = int(round(DURATION*SAMPLE_RATE))
QNTY = 3
D_VALUE_FREQ = {
    "Gamma1" : 300,
    "Gamma2" : 350,    
    "Alpha" :  400, 
    "Beta1" : 450,
    "Beta2" : 500,
    "Cappa" : 550,
    "Delta1" : 600,
    "Delta2" : 650,
    "Epsilon1" : 700,
    "Epsilon2" : 750,
    "Feta" : 800
}
D_VALUE_COLOR = {
    "Gamma1" : (45,0,45),
    "Gamma2" : (70,0,43),
    "Alpha": (100,39,43),
    "Beta1" : (100,93,22),
    "Beta2" : (60,100,56),
    "Cappa" : (16,100,84),
    "Delta1" : (12,100,91),
    "Delta2" : (34,49,100),
    "Epsilon1" : (2,32,100),
    "Epsilon2" : (27,44,92),
    "Feta" : (34,65,62)
}       
# === classes === (CamelCase names)
  # empty
# === functions === (lower_case names)
  # empty
# === main === (lower_case names)
# --- vars ---
# setup our np array to handle 16 bit ints, which is what we set our mixer to expect with "bits" up above
buf = np.zeros((N_SAMPLES, 2), dtype = np.int16)
max_sample = 2**(BITS - 1) - 1
# --- init ---
pygame.mixer.pre_init(44100, -BITS, 2)
pygame.init()
screen = pygame.display.set_mode(WINDOWS_SIZE, pygame.HWSURFACE | pygame.DOUBLEBUF)
# --- other ---
#basicfont = pygame.font.SysFont(None, 48) # use after `pygame.init`
# --- mainloop ---
running = True
for i in range(QNTY):
    # clear events queue
    # TODO: check events during time.wait to close window faster
    for event in pygame.event.get():
        if event.type == pygame.QUIT: # quit by clicking "X" window button 
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE: # quit on ESCAPE key
                running = False
    if not running:
        break
    # Choose note
    value = np.random.choice(D_VALUE_COLOR.keys(), size=1)[0] # WRONG!
    color = D_VALUE_COLOR[value] # one command by line - it is more readable
    frequency = D_VALUE_FREQ[value]
    frequency_l = frequency_r = frequency/2.0
    print(i, value)
    # change caption
    pygame.display.set_caption(value)
    # change color
    screen.fill(color)
    pygame.display.update()
    # sounds
    for s in range(N_SAMPLES):
        t = float(s)/SAMPLE_RATE    # time in seconds
        #grab the x-coordinate of the sine wave at a given time, while constraining the sample to what our mixer is set to with "bits"
        buf[s][0] = int(round(max_sample*np.sin(2*np.pi*frequency_l*t)))        # left
        buf[s][1] = int(round(max_sample*0.5*np.sin(2*np.pi*frequency_r*t)))    # right
    sound = pygame.sndarray.make_sound(buf)
    #play once, then loop forever
    sound.play(loops=0)
    pygame.time.wait(int(DURATION*1000))
# --- the end ---
pygame.quit()

最新更新