-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkana2.pyw
625 lines (522 loc) · 19.5 KB
/
kana2.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
#!/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
NTRIES = 4
JAP_FONT = ['MS Gothic', 'notosanscjkjp']
LATIN_FONT = ['Arial Narrow', 'Arial']
TITLE_FONT = 'Verdana'
class Mode(enum.Enum):
KANA_TO_LATIN = 1
LATIN_TO_KANA = 2
KANA_TO_LATIN_TEXT = 3
class Charset(enum.Flag):
HIRAGANA = 1
KATAKANA = 2
EXT = 4
class Align(enum.Enum):
LEFT = 1
CENTER = 2
RIGHT = 3
#region data
def nospace(s):
return ''.join(s.split())
HIRAGANA_CHARS = nospace('''
あいうえお
かきくけこ
さしすせそ
たちつてと
なにぬねの
はひふへほ
まみむめも
や ゆ よ
らりるれろ
わ を
ん
''')
HIRAGANA_EXT_CHARS = nospace('''
がぎぐげご
ざじずぜぞ
だぢづでど
ばびぶべぼ
ぱぴぷぺぽ
''')
HIRAGANA_ALL_CHARS = HIRAGANA_CHARS + HIRAGANA_EXT_CHARS
KATAKANA_CHARS = nospace('''
アイウエオ
カキクケコ
サシスセソ
タチツテト
ナニヌネノ
ハヒフヘホ
マミムメモ
ヤ ユ ヨ
ラリルレロ
ワ ヲ
ン
''')
KATAKANA_EXT_CHARS = nospace('''
ガギグゲゴ
ザジズゼゾ
ダヂヅデド
バビブベボ
パピプペポ
''')
KATAKANA_ALL_CHARS = KATAKANA_CHARS + KATAKANA_EXT_CHARS
ALL_CHARS = HIRAGANA_ALL_CHARS + KATAKANA_ALL_CHARS
#endregion
CHARSETS = {
Charset.HIRAGANA: HIRAGANA_CHARS,
Charset.KATAKANA: KATAKANA_CHARS,
Charset.HIRAGANA | Charset.KATAKANA: HIRAGANA_CHARS + KATAKANA_CHARS,
Charset.HIRAGANA | Charset.EXT: HIRAGANA_ALL_CHARS,
Charset.KATAKANA | Charset.EXT: KATAKANA_ALL_CHARS,
Charset.HIRAGANA | Charset.KATAKANA | Charset.EXT: ALL_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 mkrect(rect_or_size=(0, 0), **kwargs):
if len(rect_or_size) == 4:
r = pg.Rect(rect_or_size)
else:
r = pg.Rect((0, 0), rect_or_size)
for k, v in kwargs.items():
setattr(r, k, v)
return r
def crect(center, size):
r = pg.Rect((0, 0), size)
r.center = center
return r
def aligned_rect(anchor, size, align: Align):
if align == Align.CENTER:
anchor = anchor if len(anchor) == 2 else pg.Rect(anchor).center
return crect(anchor, size)
elif align == Align.LEFT:
anchor = anchor if len(anchor) == 2 else pg.Rect(anchor).midleft
return mkrect(size, midleft=anchor)
elif align == Align.RIGHT:
anchor = anchor if len(anchor) == 2 else pg.Rect(anchor).midright
return mkrect(size, midright=anchor)
class Widget:
def draw(self, screen: pg.Surface):
pass
def event(self, event: pg.event.Event) -> bool | None:
if event.type == pg.MOUSEBUTTONDOWN:
return self.mouse_down(event)
elif event.type == pg.MOUSEBUTTONUP:
return self.mouse_up(event)
elif event.type == pg.MOUSEMOTION:
return self.mouse_move(event)
elif event.type == pg.KEYDOWN:
return self.key_down(event)
elif event.type == pg.KEYUP:
return self.key_up(event)
def mouse_down(self, event: pg.event.Event) -> bool | None:
pass
def mouse_up(self, event: pg.event.Event) -> bool | None:
pass
def mouse_move(self, event: pg.event.Event) -> bool | None:
pass
def key_down(self, event: pg.event.Event) -> bool | None:
pass
def key_up(self, event: pg.event.Event) -> bool | None:
pass
class Label(Widget):
def __init__(self, text: str, 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,
select_color=None, disabled_color=None, align=Align.CENTER,
selected=False, visible=True, enabled=True, data=None,
on_click=None):
self.text = text
self.font: pg.font.Font = font or SysFont(LATIN_FONT, 24)
if len(rect) == 2:
size = self.font.size(text)
self.rect = aligned_rect(rect, size, align)
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.select_color = select_color
self.disabled_color = disabled_color
self.align = align
self.selected = selected
self.visible = visible
self.enabled = enabled
self.data = data
self.on_click = on_click
self.hovering = False
self.clicking = False
def mouse_down(self, event):
if self.enabled and self.rect.collidepoint(event.pos):
self.clicking = True
return True
def mouse_up(self, event):
if self.enabled and self.clicking and self.rect.collidepoint(event.pos):
if self.on_click:
self.on_click(self)
self.clicking = False
def mouse_move(self, event):
self.hovering = self.rect.collidepoint(event.pos)
def toggle_selected(self):
self.selected = not self.selected
def get_bg_color(self):
return (self.disabled_color if not self.enabled and self.disabled_color
else self.click_color if self.clicking and self.click_color
else self.hover_color if self.hovering and self.hover_color
else self.select_color if self.selected and self.select_color
else self.bg_color)
def get_text_color(self):
return self.text_color
def get_text(self):
return self.text
def text_rect(self):
size = self.font.size(self.get_text())
return aligned_rect(self.rect, size, self.align)
def draw(self, screen):
if not self.visible:
return
bg_color = self.get_bg_color()
if bg_color:
pg.draw.rect(screen, bg_color, self.rect, 0,
self.border_radius)
text_img = self.font.render(
self.get_text(), True, self.get_text_color())
screen.blit(text_img, self.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: str, rect, font=None, *, border_width=2,
border_radius=5, **kwargs):
super().__init__(text, rect, font, border_width=border_width,
border_radius=border_radius, **kwargs)
class TextBox(Label):
def __init__(self, text: str, rect, font=None, *, on_enter=None,
border_width=2, **kwargs):
super().__init__(text, rect, font, border_width=border_width, **kwargs)
self.on_enter = on_enter
self.cursor = 0
def clear(self):
self.text = ''
self.cursor = 0
def key_down(self, event):
if not self.enabled:
return False
if event.key == pg.K_RETURN:
if self.on_enter:
self.on_enter(self)
elif event.key == pg.K_BACKSPACE:
if self.cursor > 0:
self.text = self.text[:self.cursor-1] + self.text[self.cursor:]
self.cursor -= 1
elif event.key == pg.K_DELETE:
self.text = self.text[:self.cursor] + self.text[self.cursor+1:]
elif event.key == pg.K_LEFT:
self.cursor = max(self.cursor - 1, 0)
elif event.key == pg.K_RIGHT:
self.cursor = min(self.cursor + 1, len(self.text))
elif event.key == pg.K_HOME:
self.cursor = 0
elif event.key == pg.K_END:
self.cursor = len(self.text)
elif event.unicode and event.unicode.isprintable():
self.text = (self.text[:self.cursor] + event.unicode +
self.text[self.cursor:])
self.cursor += len(event.unicode)
else:
return False
return True
def draw(self, screen):
super().draw(screen)
if pg.time.get_ticks() % 1000 < 500:
bar_img = self.font.render('|', True, self.get_text_color())
rect = self.text_rect()
xpos = rect.left + self.font.size(self.text[:self.cursor])[0]
bar_rect = bar_img.get_rect(y=rect.y, centerx=xpos)
screen.blit(bar_img, bar_rect)
class Scene(Widget):
def __init__(self, game: 'Game'):
self.game = game
self.widgets: list[Widget] = []
self.bg_color = BGCOLOR
def event(self, event: pg.event.Event):
for w in self.widgets:
if w.event(event):
return True
return super().event(event)
def add(self, widget: Widget):
self.widgets.append(widget)
return widget
def add_all(self, *widgets: Widget):
for widget in widgets:
self.widgets.append(widget)
return widgets
def update(self):
pass
def draw_bg(self, screen: pg.Surface):
screen.fill(self.bg_color)
def draw_widgets(self, screen: pg.Surface):
for w in self.widgets:
w.draw(screen)
def draw(self, screen: pg.Surface):
self.draw_bg(screen)
self.draw_widgets(screen)
class StartScene(Scene):
def __init__(self, game):
super().__init__(game)
self.mode: Mode = Mode.KANA_TO_LATIN
self.charset: Charset = Charset.HIRAGANA
self.init_ui()
def init_ui(self):
# main_label = Label('Kana/仮名', (WIDTH//2, HEIGHT//2-110),
main_label = Label('Kana/仮名', (WIDTH//2, HEIGHT//2-110),
SysFont(JAP_FONT, 60))
bstyle = {
'font': SysFont(LATIN_FONT, 24),
'bg_color': (192, 192, 255),
'select_color': (0, 192, 255),
'hover_color': (64, 128, 255),
}
mode_button1 = Button(
'Kana→Latin', crect((WIDTH//2 - 150, HEIGHT//2 - 15), (130, 50)),
on_click=self.handle_mode, data=Mode.KANA_TO_LATIN, selected=True,
**bstyle)
mode_button2 = Button(
'Latin→Kana', crect((WIDTH//2, HEIGHT//2 - 15), (130, 50)),
on_click=self.handle_mode, data=Mode.LATIN_TO_KANA, **bstyle)
mode_button3 = Button(
'Kana→Text', crect((WIDTH//2 + 150, HEIGHT//2 - 15), (130, 50)),
on_click=self.handle_mode, data=Mode.KANA_TO_LATIN_TEXT, **bstyle)
self.mode_buttons = [mode_button1, mode_button2, mode_button3]
charset_button1 = Button(
'Hiragana', crect((WIDTH//2 - 150, HEIGHT//2 + 55), (130, 50)),
on_click=self.handle_charset, data=Charset.HIRAGANA, selected=True,
**bstyle)
charset_button2 = Button(
'Katakana', crect((WIDTH//2, HEIGHT//2 + 55), (130, 50)),
on_click=self.handle_charset, data=Charset.KATAKANA, **bstyle)
charset_button3 = Button(
'+ Dakuten', crect((WIDTH//2 + 150, HEIGHT//2 + 55), (130, 50)),
on_click=self.handle_charset, data=Charset.EXT, **bstyle)
self.charset_buttons = [charset_button1, charset_button2, charset_button3]
start_button = Button(
'Start', crect((WIDTH//2, HEIGHT//2 + 150), (150, 80)),
SysFont(TITLE_FONT, 50), bg_color=(0, 255, 255),
hover_color=(64, 128, 255), on_click=self.handle_start)
self.mode_lbl = Label('', (20, 30), align=Align.LEFT, visible=False)
self.charset_lbl = Label('', (20, 70), align=Align.LEFT, visible=False)
self.add_all(
main_label, *self.mode_buttons, *self.charset_buttons,
self.mode_lbl, self.charset_lbl, start_button,
)
def handle_mode(self, button: Button):
self.mode = button.data
for button2 in self.mode_buttons:
button2.selected = button2 is button
def handle_charset(self, button: Button):
if (self.charset ^ button.data) & ~Charset.EXT:
self.charset ^= button.data
button.toggle_selected()
def handle_start(self, button):
self.game.change_scene(GameScene, self.charset, self.mode)
def update(self):
self.mode_lbl.text = self.mode.name
self.charset_lbl.text = self.charset.name
def key_down(self, event: pg.event.Event):
if event.key == pg.K_ESCAPE:
self.game.quit()
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.score = 0
self.successes = 0
self.failures = 0
self.cur_failures = 0
self.init_ui()
self.next_round()
def init_ui(self):
latin_font_48 = SysFont(LATIN_FONT, 48)
if self.mode == Mode.LATIN_TO_KANA:
char_font = SysFont(LATIN_FONT, 100)
choice_font = SysFont(JAP_FONT, 48)
else:
char_font = SysFont(JAP_FONT, 100)
choice_font = latin_font_48
self.char_lbl = Label('', (WIDTH//2, HEIGHT//2 - 50), char_font)
if self.mode == Mode.KANA_TO_LATIN_TEXT:
tb_rect = crect((WIDTH//2, HEIGHT//2 + 100), (150, 60))
self.answer_enter = TextBox('', tb_rect, SysFont(LATIN_FONT, 42),
on_enter=self.handle_text_enter)
skip_rect = mkrect((100, 50), left=tb_rect.right + 20,
centery=tb_rect.centery)
self.skip_button = Button(
'Skip', skip_rect, bg_color=(255, 128, 0),
hover_color=(255, 0, 0), click_color=(128, 0, 0),
on_click=self.skip)
answer_widgets = [self.answer_enter, self.skip_button]
else:
self.choice_buttons: list[Button] = []
for i in range(NCHOICES):
x = int(WIDTH/2 + 100*(i - (NCHOICES - 1) / 2))
y = HEIGHT//2 + 100
but = Button('', crect((x, y), (65, 60)), choice_font,
hover_color=HOVERCOLOR, click_color=CLICKCOLOR,
disabled_color=WRONGCOLOR,
on_click=self.handle_choice)
self.choice_buttons.append(but)
answer_widgets = [*self.choice_buttons]
self.wrong_lbl = Label('WRONG', (WIDTH//2, HEIGHT//2 + 180),
latin_font_48, text_color=WRONGCOLOR,
visible=False)
self.score_lbl = Label(str(self.score), (50, 50),
latin_font_48, text_color=SCORECOLOR)
self.successes_lbl = Label(str(self.successes), (110, 50),
latin_font_48, text_color=SUCCESSESCOLOR)
self.failures_lbl = Label(str(self.failures), (170, 50),
latin_font_48, text_color=FAILURESCOLOR)
self.add_all(
self.char_lbl, *answer_widgets, 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.LATIN_TO_KANA:
lbl_text = char_trans
right_choice = self.right_choice = char
others = [c for c in self.charset if kana_name(c) != char_trans]
else:
lbl_text = char
right_choice = self.right_choice = char_trans
others = [c for c in self.charset_trans if c != char_trans]
self.wrong_lbl.visible = False
self.char_lbl.text = lbl_text
if self.mode == Mode.KANA_TO_LATIN_TEXT:
self.answer_enter.clear()
else:
choices = self.choices = random.sample(others, NCHOICES-1)
choices.append(right_choice)
random.shuffle(choices)
for but, choice in zip(self.choice_buttons, choices):
but.text = but.data = choice
but.enabled = True
self.cur_failures = 0
def handle_text_enter(self, textbox: TextBox):
if not textbox.text:
return
if textbox.text == self.right_choice:
self.handle_right()
else:
self.handle_wrong()
if self.cur_failures >= NTRIES:
self.skip()
else:
textbox.clear()
def handle_choice(self, button: Button):
if button.data == self.right_choice:
self.handle_right(button)
else:
self.handle_wrong(button)
def handle_right(self, button=None):
# print('correct')
self.score += 1
self.successes += 1
self.next_round()
def handle_wrong(self, button: Button = None):
# print('wrong')
self.score -= 1
self.failures += 1
self.cur_failures += 1
self.wrong_lbl.visible = True
if button:
button.enabled = False
def skip(self, button=None):
self.answer_enter.text = self.right_choice
failures = max(NTRIES - self.cur_failures, 0)
self.score -= failures
self.failures += failures
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 key_down(self, event: pg.event.Event):
if self.mode != Mode.KANA_TO_LATIN_TEXT:
num_keys = range(pg.K_1, pg.K_1 + NCHOICES)
if event.key in num_keys:
ind = num_keys.index(event.key)
self.handle_choice(self.choice_buttons[ind])
if event.key == pg.K_ESCAPE:
self.game.change_scene(StartScene)
class Game:
def __init__(self):
pg.init()
pg.key.set_repeat(500, 60)
self.screen = pg.display.set_mode((WIDTH, HEIGHT))
pg.display.set_caption('Kana/仮名')
self.scene: Scene = None
self.next_scene: Scene = None
self.running = False
self.change_scene(StartScene)
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_class: type[Scene], *args, **kwargs):
self.next_scene = scene_class(self, *args, **kwargs)
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
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()