Python 如何在pygame中更改密钥的名称?
我有一个代码,R键和p键发出声音,并在按下键时将其保存在列表中,我的问题是每次按下键时如何更改键的名称 以下是代码的一部分:Python 如何在pygame中更改密钥的名称?,python,python-3.x,pygame,Python,Python 3.x,Pygame,我有一个代码,R键和p键发出声音,并在按下键时将其保存在列表中,我的问题是每次按下键时如何更改键的名称 以下是代码的一部分: from array import array import pygame from pygame.mixer import Sound, get_init, pre_init class Note(pygame.mixer.Sound): def __init__(self,key, frequency, volume=.1): s
from array import array
import pygame
from pygame.mixer import Sound, get_init, pre_init
class Note(pygame.mixer.Sound):
def __init__(self,key, frequency, volume=.1):
self.frequency = frequency
Sound.__init__(self, self.build_samples())
self.set_volume(volume)
self.key=key
def build_samples(self):
period = int(round(get_init()[0] / self.frequency))
samples = array("h", [0] * period)
amplitude = 2 ** (abs(get_init()[1]) - 1) - 1
for time in range(period):
if time < period / 2:
samples[time] = amplitude
else:
samples[time] = -amplitude
return samples
def __repr__(self):
return 'Note({})'.format(self.key)
pre_init(44100, -16, 1, 1024)
pygame.init()
screen = pygame.display.set_mode([640, 480], 0)
sounds = {}
keymap = {pygame.K_p: 880, pygame.K_r: 440}
key_pressed=[]
while True:
evt = pygame.event.wait()
if evt.type == pygame.QUIT:
break
elif evt.type == pygame.KEYDOWN:
if evt.key in keymap:
note = Note(keymap[evt.key])
note.play(-1)
sounds[evt.key] = note
key_pressed.append(note)
elif evt.type == pygame.KEYUP:
if evt.key in sounds:
sounds.pop(evt.key).stop()
print(key_pressed)
从数组导入数组
导入pygame
从pygame.mixer导入声音,获取_init、pre_init
课堂笔记(pygame.mixer.Sound):
定义初始化(self、key、frequency、volume=.1):
self.frequency=频率
声音.\uuuu初始化(self,self.build\u samples())
自我设置音量(音量)
self.key=key
def构建_示例(自):
period=int(舍入(get_init()[0]/self.frequency))
样本=数组(“h”,[0]*周期)
振幅=2**(abs(get_init()[1])-1)-1
对于范围内的时间(周期):
如果时间<周期/2:
采样[时间]=振幅
其他:
采样[时间]=-振幅
返回样品
定义报告(自我):
返回'Note({})'.format(self.key)
预初始化(44100,-16,1104)
pygame.init()
screen=pygame.display.set_模式([640480],0)
声音={}
keymap={pygame.K_p:880,pygame.K_r:440}
按键按下=[]
尽管如此:
evt=pygame.event.wait()
如果evt.type==pygame.QUIT:
打破
elif evt.type==pygame.KEYDOWN:
如果evt.KEYIN keymap:
注释=注释(keymap[evt.key])
注:播放(-1)
声音[evt.key]=音符
按下按键。追加(注意)
elif evt.type==pygame.KEYUP:
如果evt.key输入声音:
sounds.pop(evt.key).stop()
打印(按下按键)
如果要更改对象的表示形式,必须为它们提供一个返回字符串的
\uuuu repr\uuu
方法
import pygame
pygame.init()
screen = pygame.display.set_mode([640, 480], 0)
class Note:
def __init__(self, key):
self.key = key
def __repr__(self):
return 'Note({})'.format(self.key)
sounds = {}
keymap = {pygame.K_p: 880, pygame.K_r: 440}
key_pressed = []
while True:
evt = pygame.event.wait()
if evt.type == pygame.QUIT:
break
elif evt.type == pygame.KEYDOWN:
if evt.key in keymap:
note = Note(keymap[evt.key])
print(note)
sounds[evt.key] = note
key_pressed.append(note)
print(key_pressed)
pygame.quit()
你的意思是想看
注释(880)
或类似的东西而不是
?然后你需要给你的类一个\uuuu repr\uuuu
方法。或者只访问dict值,而不是print(keymap[按下键])
Hi skrx。我希望你今晚过得愉快。编辑带有所有代码及其修改的问题,并执行它,我得到以下错误:TypeError:uuu init_uuu()缺少1个必需的位置参数:“frequency”只需删除key
参数和self.key
属性,并改用frequency
。在我的示例中,我只使用了名称key
,因为我不知道您的类实际上是什么样子的。