2 回答
TA贡献1943条经验 获得超7个赞
我是这样解决的:
我只是用字典将字符转换为平假名
Table = {
#A Column
'a':'あ',
'ka':'か',
'ga':'が',
'sa':'さ',
'za':'ざ',
'ta':'た',
'da':'だ',
'na':'な',
'ha':'は',
'ba':'ば',
'pa':'ぱ',
'ma':'ま',
'ya':'や',
'ra':'ら',
'wa':'わ',
#I Column
'i':'い',
'ki':'き',
'gi':'ぎ',
'shi':'し',
'ji':'じ',
'chi':'ち',
'ni':'に',
'hi':'ひ',
'bi':'び',
'pi':'ぴ',
'mi':'み',
'ri':'り',
#U Column
'u':'う',
'ku':'く',
'gu':'ぐ',
'su':'す',
'zu':'ず',
'tsu':'つ',
'nu':'ぬ',
'fu':'ふ',
'bu':'ぶ',
'pu':'ぷ',
'mu':'む',
'ru':'る',
'yu':'ゆ',
#E Column
'e':'え',
'ke':'け',
'ge':'げ',
'se':'せ',
'ze':'ぜ',
'te':'て',
'de':'で',
'ne':'ね',
'he':'へ',
'be':'べ',
'pe':'ぺ',
'me':'め',
're':'れ',
#O Column
'o':'お',
'ko':'こ',
'go':'ご',
'so':'そ',
'zo':'ぞ',
'to':'と',
'do':'ど',
'no':'の',
'ho':'ほ',
'bo':'ぼ',
'po':'ぽ',
'mo':'も',
'yo':'よ',
'ro':'ろ',
'wo':'を',
#N sound
'nn':'ん',
#Characters with 3 sounds that use the small Y characters
'kya':'きゃ',
'kyu':'きゅ',
'kyo':'きょ',
'sha':'しゃ',
'shu':'しゅ',
'sho':'しょ',
'cha':'ちゃ',
'chu':'ちゅ',
'cho':'ちょ',
'nya':'にゃ',
'nyu':'にゅ',
'nyo':'にょ',
'hya':'ひゃ',
'hyu':'ひゅ',
'hyo':'ひょ',
'mya':'みゃ',
'myu':'みゅ',
'myo':'みょ',
'rya':'りゃ',
'ryu':'りゅ',
'ryo':'りょ',
'gya':'ぎゃ',
'gyu':'ぎゅ',
'gyo':'ぎょ',
'ja':'じゃ',
'ju':'じゅ',
'jo':'じょ',
'bya':'びゃ',
'byu':'びゅ',
'byo':'びょ',
'pya':'ぴゃ',
'pyu':'ぴゅ',
'pyo':'ぴょ',
#Weird characters rarely used
'di':'ぢ',
'du':'づ',
#Small Tsu
'kk':'っ',
'ss':'っ',
'tt':'っ',
'hh':'っ',
'mm':'っ',
'yy':'っ',
'rr':'っ',
'ww':'っ',
'gg':'っ',
'zz':'っ',
'dd':'っ',
'bb':'っ',
'pp':'っ',
}
然后我使用了以下函数
def deleteText(self):
if len(self.text) > 0:
self.textoChico = self.textoChico[:-1]
else:
self.texto = self.texto[:-1]
def writeText(self):
if len(self.text) > 3:
self.text = ""
if self.text in Table :
self.texto += Table [self.text]
self.text = ''
TA贡献1876条经验 获得超7个赞
我认为这取决于你的输入法。在 Linux PyGame 上使用“Anthy”IBus 输入可以很好地输入平假名文本。
我怀疑您的键盘正在发送拉丁字符,并通过软件驱动程序将其转换为平假名(等),但 PyGame 正在较低级别处理键盘,因此接收原始按键而不是转换。
参考代码:
import pygame
# Window size
WINDOW_WIDTH = 600
WINDOW_HEIGHT = 100
WINDOW_SURFACE = pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.RESIZABLE
DARK_BLUE = ( 3, 5, 54 )
WHITE = ( 255, 255, 250 )
### initialisation
pygame.init()
pygame.mixer.init()
window = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ), WINDOW_SURFACE )
pygame.display.set_caption("Hiragana Test")
### For the simple text handling
#font = pygame.font.SysFont( None, 16 )
font = pygame.font.Font( 'umeboshi.ttf', 16 )
text = ''
text_surf = font.render( text, True, WHITE )
### Main Loop
clock = pygame.time.Clock()
done = False
while not done:
# Handle user-input
for event in pygame.event.get():
if ( event.type == pygame.QUIT ):
done = True
elif ( event.type == pygame.MOUSEBUTTONUP ):
# On mouse-click
pass
elif ( event.type == pygame.KEYDOWN ):
if ( event.key == pygame.K_BACKSPACE ):
text = text[:-1]
else:
text = text + event.unicode
#print( "TEXT: [%s]" % ( text ) )
text_surf = font.render( text, True, WHITE )
# Update the window, but not more than 60fps
window.fill( DARK_BLUE )
window.blit( text_surf, ( 10, WINDOW_HEIGHT//2 ) )
pygame.display.flip()
# Clamp FPS
clock.tick_busy_loop(60)
pygame.quit()
添加回答
举报