-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkana.pyw
executable file
·423 lines (357 loc) · 12.8 KB
/
kana.pyw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#!/usr/bin/python3
import enum
import random
import unicodedata
import pygame as pg
from pygame.font import SysFont
WIDTH = 640
HEIGHT = 480
FPS = 30
BGCOLOR = (192, 224, 255)
FGCOLOR = (0, 0, 0)
WRONGCOLOR = (255, 0, 0)
SCORECOLOR = (0, 64, 255)
FAILURESCOLOR = (255, 0, 0)
SUCCESSESCOLOR = (0, 192, 0)
HOVERCOLOR = (0, 255, 255)
CLICKCOLOR = (0, 192, 255)
NCHOICES = 5
JAP_FONT = ['MS Gothic', 'notosanscjkjp']
LATIN_FONT = ['arialnarrow', 'Arial']
TITLE_FONT = 'Verdana'
class Mode(enum.Enum):
KANA_TO_LATIN = 1
LATIN_TO_KANA = 2
class Charset(enum.Enum):
HIRAGANA = 1
KATAKANA = 2
#region data
def nospace(s):
return ''.join(s.split())
HIRAGANA_CHARS = nospace('''
あいうえお
かきくけこ
さしすせそ
たちつてと
なにぬねの
はひふへほ
まみむめも
や ゆ よ
らりるれろ
わ を
ん
''')
HIRAGANA_EXT_CHARS = nospace('''
がぎぐげご
ざじずぜぞ
だぢづでど
ばびぶべぼ
ぱぴぷぺぽ
''')
KATAKANA_CHARS = nospace('''
アイウエオ
カキクケコ
サシスセソ
タチツテト
ナニヌネノ
ハヒフヘホ
マミムメモ
ヤ ユ ヨ
ラリルレロ
ワ ヲ
ン
''')
KATAKANA_EXT_CHARS = nospace('''
ガギグゲゴ
ザジズゼゾ
ダヂヅデド
バビブベボ
パピプペポ
''')
#endregion
CHARSETS = {
Charset.HIRAGANA: HIRAGANA_CHARS,
Charset.KATAKANA: KATAKANA_CHARS,
}
IRREGULAR = {
'si': 'shi', 'ti': 'chi', 'tu': 'tsu', 'hu': 'fu',
'zi': 'ji', 'di': 'ji', 'du': 'zu'
}
def kana_name(c):
name = unicodedata.name(c).split()[-1].lower()
return IRREGULAR.get(name, name)
def make_rect(size=(0, 0), **kwargs):
r = pg.Rect((0, 0), size)
for k, v in kwargs.items():
setattr(r, k, v)
return r
def center_rect(center, size):
r = pg.Rect((0, 0), size)
r.center = center
return r
class Label:
def __init__(self, text, rect, font=None, text_color=(0, 0, 0),
bg_color=None, border_width=0, border_radius=0,
border_color=(0, 0, 0), hover_color=None, click_color=None,
visible=True, enabled=True, on_click=None):
self.text = text
self.font: pg.font.Font = font or SysFont(LATIN_FONT, 20)
if len(rect) == 2:
size = self.font.size(text)
self.rect = center_rect(rect, size)
else:
self.rect = pg.Rect(rect)
self.text_color = text_color
self.bg_color = bg_color
self.border_width = border_width
self.border_radius = border_radius
self.border_color = border_color
self.hover_color = hover_color
self.click_color = click_color
self.visible = visible
self.enabled = enabled
self.on_click = on_click
self.hovering = False
self.clicking = False
def mouse_down(self, x, y):
if self.enabled and self.rect.collidepoint(x, y):
self.clicking = True
def mouse_up(self, x, y):
if self.enabled and self.clicking and self.rect.collidepoint(x, y):
if self.on_click:
self.on_click(self)
self.clicking = False
def hover(self, x, y):
self.hovering = self.rect.collidepoint(x, y)
def draw(self, screen):
if not self.visible:
return
bg_color = (self.click_color if self.clicking and self.click_color
else self.hover_color if self.hovering and self.hover_color
else self.bg_color)
if bg_color:
pg.draw.rect(screen, bg_color, self.rect, 0,
self.border_radius)
text_img = self.font.render(self.text, True, self.text_color)
text_rect = text_img.get_rect(center=self.rect.center)
screen.blit(text_img, text_rect)
if self.border_width:
pg.draw.rect(screen, self.border_color, self.rect,
self.border_width, self.border_radius)
class Button(Label):
def __init__(self, text, rect, font=None, text_color=(0, 0, 0),
bg_color=None, border_width=2, border_radius=5,
border_color=(0, 0, 0), hover_color=None, click_color=None,
visible=True, enabled=True, on_click=None):
super().__init__(text, rect, font, text_color, bg_color, border_width,
border_radius, border_color, hover_color, click_color,
visible, enabled, on_click)
class Scene:
def __init__(self, game: 'Game'):
self.game = game
self.labels: list[Label] = []
def event(self, event):
if event.type == pg.MOUSEBUTTONDOWN:
for lbl in self.labels:
lbl.mouse_down(*event.pos)
elif event.type == pg.MOUSEBUTTONUP:
for lbl in self.labels:
lbl.mouse_up(*event.pos)
elif event.type == pg.MOUSEMOTION:
for lbl in self.labels:
lbl.hover(*event.pos)
def update(self):
pass
def draw(self, screen):
for lbl in self.labels:
lbl.draw(screen)
class StartScene(Scene):
def __init__(self, game):
super().__init__(game)
self.mode = Mode.KANA_TO_LATIN
self.charset = Charset.HIRAGANA
self.setup_ui()
def setup_ui(self):
main_label = Label('Kana/仮名', (WIDTH//2, HEIGHT//2-110),
SysFont(JAP_FONT, 60))
mode_button1 = Button(
'Kana→Latin', center_rect((WIDTH//2 - 80, HEIGHT//2 - 10), (130, 50)),
SysFont(LATIN_FONT, 24), hover_color=(64, 128, 255),
on_click=lambda b: self.set_mode(Mode.KANA_TO_LATIN))
mode_button2 = Button(
'Latin→Kana', center_rect((WIDTH//2 + 80, HEIGHT//2 - 10), (130, 50)),
SysFont(LATIN_FONT, 24), hover_color=(64, 128, 255),
on_click=lambda b: self.set_mode(Mode.LATIN_TO_KANA))
self.mode_buttons = {Mode.KANA_TO_LATIN: mode_button1,
Mode.LATIN_TO_KANA: mode_button2}
charset_button1 = Button(
'Hiragana', center_rect((WIDTH//2 - 80, HEIGHT//2 + 50), (130, 50)),
SysFont(LATIN_FONT, 24), hover_color=(64, 128, 255),
on_click=lambda b: self.set_charset(Charset.HIRAGANA))
charset_button2 = Button(
'Katakana', center_rect((WIDTH//2 + 80, HEIGHT//2 + 50), (130, 50)),
SysFont(LATIN_FONT, 24), hover_color=(64, 128, 255),
on_click=lambda b: self.set_charset(Charset.KATAKANA))
self.charset_buttons = {Charset.HIRAGANA: charset_button1,
Charset.KATAKANA: charset_button2}
start_button = Button(
'Start', center_rect((WIDTH//2, HEIGHT//2 + 150), (150, 80)),
SysFont(TITLE_FONT, 50), bg_color=(0, 255, 255),
hover_color=(64, 128, 255), on_click=self.start)
self.mode_lbl = Label('', (100, 50), visible=False)
self.charset_lbl = Label('', (200, 50), visible=False)
self.labels = [
main_label, mode_button1, mode_button2, charset_button1,
charset_button2, self.mode_lbl, self.charset_lbl, start_button
]
def set_mode(self, mode):
self.mode = mode
def set_charset(self, charset):
self.charset = charset
def start(self, button):
self.game.change_scene(GameScene(self.game, self.charset, self.mode))
def update(self):
self.mode_lbl.text = self.mode.name
self.charset_lbl.text = self.charset
for mode, button in self.mode_buttons.items():
if mode == self.mode:
button.bg_color = (0, 192, 255)
else:
button.bg_color = (192, 192, 255)
for charset, button in self.charset_buttons.items():
if charset == self.charset:
button.bg_color = (0, 192, 255)
else:
button.bg_color = (192, 192, 255)
def draw(self, screen):
screen.fill(BGCOLOR)
super().draw(screen)
class GameScene(Scene):
def __init__(self, game, charset=Charset.HIRAGANA, mode=Mode.KANA_TO_LATIN):
super().__init__(game)
self.charset = CHARSETS[charset]
self.charset_trans = list(set(map(kana_name, self.charset)))
self.mode = mode
self.current_char = ''
self.choices = []
self.running = False
self.score = 0
self.successes = 0
self.failures = 0
self.setup_ui()
self.next_round()
def setup_ui(self):
if self.mode == Mode.KANA_TO_LATIN:
char_font = SysFont(JAP_FONT, 100)
choice_font = SysFont(LATIN_FONT, 48)
else:
char_font = SysFont(LATIN_FONT, 100)
choice_font = SysFont(JAP_FONT, 48)
self.char_lbl = Label('', (WIDTH//2, HEIGHT//2 - 50), char_font)
self.choice_buttons: list[Label] = []
for i in range(NCHOICES):
x = int(WIDTH/2 + 100*(i - (NCHOICES - 1) / 2))
y = HEIGHT//2 + 100
but = Button('', center_rect((x, y), (65, 60)), choice_font,
hover_color=HOVERCOLOR, click_color=CLICKCOLOR)
self.choice_buttons.append(but)
self.wrong_lbl = Label('WRONG', (WIDTH//2, HEIGHT//2 + 180),
SysFont(LATIN_FONT, 48), WRONGCOLOR,
visible=False)
self.score_lbl = Label(str(self.score), (50, 50),
SysFont(LATIN_FONT, 48), SCORECOLOR)
self.successes_lbl = Label(str(self.successes), (100, 50),
SysFont(LATIN_FONT, 48), SUCCESSESCOLOR)
self.failures_lbl = Label(str(self.failures), (150, 50),
SysFont(LATIN_FONT, 48), FAILURESCOLOR)
self.labels = [self.char_lbl, *self.choice_buttons, self.wrong_lbl,
self.score_lbl, self.successes_lbl, self.failures_lbl]
def next_round(self):
self.current_char = char = random.choice(self.charset)
char_trans = kana_name(char)
if self.mode == Mode.KANA_TO_LATIN:
lbl_text = char
right_choice = char_trans
others = [c for c in self.charset_trans if c != char_trans]
else:
lbl_text = char_trans
right_choice = char
others = [c for c in self.charset if kana_name(c) != char_trans]
self.choices = choices = random.sample(others, NCHOICES-1)
choices.append(right_choice)
random.shuffle(choices)
self.wrong_lbl.visible = False
self.char_lbl.text = lbl_text
for but, choice in zip(self.choice_buttons, choices):
but.text = choice
but.on_click = (self.handle_right if choice == right_choice
else self.handle_wrong)
but.bg_color = None
but.hover_color = HOVERCOLOR
but.enabled = True
def handle_right(self, button):
# print('correct')
self.score += 1
self.successes += 1
self.next_round()
def handle_wrong(self, button):
# print('wrong')
self.score -= 1
self.failures += 1
self.wrong_lbl.visible = True
button.bg_color = WRONGCOLOR
button.hover_color = None
button.enabled = False
def update(self):
self.score_lbl.text = str(self.score)
self.successes_lbl.text = str(self.successes)
self.failures_lbl.text = str(self.failures)
def draw(self, screen):
screen.fill(BGCOLOR)
super().draw(screen)
class Game:
def __init__(self):
pg.init()
self.screen = pg.display.set_mode((WIDTH, HEIGHT))
self.scene: Scene = None
self.next_scene: Scene = None
self.change_scene(StartScene(self))
def main(self):
self.running = True
clock = pg.time.Clock()
while self.running:
self.scene = self.next_scene
self.events()
self.update()
self.draw()
clock.tick(FPS)
def change_scene(self, scene: Scene):
self.next_scene = scene
def quit(self):
self.running = False
def events(self):
for event in pg.event.get():
if event.type == pg.QUIT:
self.quit()
continue
elif event.type == pg.KEYDOWN:
if event.key == pg.K_F4 and event.mod & pg.KMOD_ALT:
self.quit()
continue
elif event.key == pg.K_ESCAPE:
if isinstance(self.scene, StartScene):
self.quit()
else:
self.change_scene(StartScene(self))
continue
self.scene.event(event)
def update(self):
self.scene.update()
def draw(self):
self.scene.draw(self.screen)
pg.display.flip()
if __name__ == '__main__':
try:
Game().main()
finally:
pg.quit()