-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPY.txt
1962 lines (1652 loc) · 66.5 KB
/
PY.txt
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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
@.\main.py
from GUI import Window
win = Window("啦八機", 450, 800) #先初始化 Tkinter 才能創建 ImageTk
from src.Game import P, PlayLaBaG, JsonLaBaG
from src.element import (
Gss, Hhh, Hentai, Handsun, Kachu, Rrr,
BG, SuperBG, GreenBG, KachuBG,
QST, SuperQST, GreenQST, KachuQST,
Title, SuperTitle, GreenTitle, KachuTitle,
SuperPOP, GreenPOP, KachuPOP,
image_dict, music,
SuperCircle,
back, BeginPIC, AgainPIC, SB,
super_hhh,
green_wei, GreenLeft, GreenMid, GreenRight,
pikachu
)
from src.Sheet import Sheet
Sheet.GetData() #獲取啦八機試算表的資料
Games = {
"Game": PlayLaBaG(),
"Json_Game" : JsonLaBaG() #.json檔案模擬用
}
Game = Games["Game"] #預設
P.Dict["A"].picture = Gss
P.Dict["B"].picture = Hhh
P.Dict["C"].picture = Hentai
P.Dict["D"].picture = Handsun
P.Dict["E"].picture = Kachu
P.Dict["F"].picture = Rrr
win.save_icon_use(image_dict, "Superhhh")
#創建 Screen(frame & canvas)
win.setup_frame_and_canvas("Home", BG) #首頁
win.setup_frame_and_canvas("Game", BG) #遊戲畫面
win.setup_frame_and_canvas("End", BG) #結算畫面
win.temp_files = music.temp_files # 連結臨時檔案 list
#region Home Screen
win.load_picture("Home", SuperCircle, 50, 130, "SuperCircle")
def into_game():
"""進入Game畫面"""
global Game
Game = Games["Game"]
Game.Name = win.get_input("Name")
if Game.Name:
win.update_by_tag("Game", "PlayerName", f"玩家名:{Game.Name}")
print(f"玩家名:{Game.Name}")
else :
win.update_by_tag("Game", "PlayerName", f"")
print(f"玩家名:無")
Game.history_score = Sheet.GetScore(Game.Name)
win.keyboard_unbind('ENTER') # 解除綁定ENTER
BeginAble()
Game.Reset()
init_Game_screen_item()
bgm_on_off()
win.switch_frame("Home", "Game") #切換畫面
win.Canva("Home").tag_bind("SuperCircle", "<Button-1>", lambda event :into_game()) #綁定 SuperCircle 圖片被點擊->進入遊戲
win.keyboard_bind('ENTER', into_game) #綁定ENTER進入遊戲
win.input_box(
"Home",
"Name", #Name 輸入盒
"",
225, 550,
18, 15
)
win.add_text(
"Home",
"作者IG:fan._.yuuu",
225, 100,
30,
"#00FFFF",
"fanyu"
)
win.add_text(
"Home",
"點擊上方圖片(或 ENTER )\n 進入遊戲 >>>>>",
225, 500,
15,
"#FFFFAA",
"click"
)
win.add_text(
"Home",
"輸入你的稱呼",
225, 575,
12,
"white",
"hint"
)
def into_json():
"""進入Json_Game畫面"""
import os
#判斷路徑是否為空、是否存在、是否為.json檔
json_path = win.open_file()
if json_path is not None and os.path.exists(json_path) and os.path.splitext(json_path)[1] == ".json":
global Game
Game = Games["Json_Game"]
win.Canva("Game").itemconfig("PlayerName", text = "正在使用 .json檔案模擬")
print(f"使用 .json檔案模擬中")
Game.setup_path(f"{json_path}")
BeginAble()
Game.Reset()
init_Game_screen_item()
bgm_on_off()
win.switch_frame("Home", "Game") #切換畫面
else:
win.message_box(
f"無效的路徑:\n{json_path}",
)
print(f"無效的路徑:\n{json_path}")
win.txt_button(
"toJson",
into_json,
"Home",
"使用 .json 檔案模擬遊戲",
10, 2,
110, 770,
12,
"black", "yellow"
)
def rank_subwindow():
"""排行榜子視窗"""
win.Button("toRank").config(state='disabled') # toRank Button 停用
win.Button("toRank").config(text="資料載入中")
Sheet.GetData() #獲取資料
win.setup_subwindow("Rank", 450, 800, BG)
win.SubWindow("Rank").add_text(
"排行榜",
225, 50,
30,
"yellow",
"Title"
)
for index, data in enumerate(list(Sheet.RankedData().items())[:10]): # 顯示前 10 名
win.SubWindow("Rank").add_text(
f"{index + 1 :<2}. {data[0]:<10s} : {data[1] :>8,}",
50, 125 + index * 50,
16,
"white",
f"rank_{index}",
"w"
)
def off_rank():
"""關閉排行榜子視窗"""
win.Button("toRank").config(state='normal')
win.Button("toRank").config(text="查看排行榜")
win.SubWindow("Rank").destroy()
win.SubWindow("Rank").txt_button(
"off_window",
off_rank,
"關閉視窗",
100, 50,
225, 700,
16
)
win.SubWindow("Rank").protocol("WM_DELETE_WINDOW", off_rank) #綁定關閉視窗
win.txt_button(
"toRank",
rank_subwindow,
"Home",
"查看排行榜",
10, 2,
225, 50,
12,
"black", "white"
)
#endregion
#region Game Screen
def init_Game_screen_item():
"""初始化 Game 畫面物件"""
win.update_by_tag("Game", "BG", BG)
win.update_by_tag("Game", "LP", QST)
win.update_by_tag("Game", "MP", QST)
win.update_by_tag("Game", "RP", QST)
win.update_by_tag("Game", "Title", Title)
win.update_by_tag("Game", "MarginScore", f"")
win.update_by_tag("Game", "Score", f"目前分數:{Game.score}")
win.update_by_tag("Game", "Times", f"剩餘次數:{Game.times - Game.played}")
win.update_by_tag("Game", "history_score", f"歷史最高分數:{Game.history_score}")
win.update_by_tag("Game", "mod_1", f"")
win.update_by_tag("Game", "mod_2", f"")
win.update_by_tag("Game", "gss", f"咖波累積數:{Game.GssNum}")
def Game_to_Home():
"""返回首頁"""
win.reset_input_box("Name", Game.Name)
win.keyboard_unbind('SPACE') # 取消space鍵的綁定
win.keyboard_bind('ENTER',into_game)
bgm_on_off(game_running=False) #關閉音樂
win.switch_frame("Game", "Home")
print("返回首頁")
win.image_button(
win,
Game_to_Home,
"Game",
back,
18, 18
)
win.load_picture("Game" , Title , 0 , 25 , "Title")
win.load_picture("Game" , QST, 0, 250 , "LP")
win.load_picture("Game" , QST, 150, 250 , "MP")
win.load_picture("Game" , QST, 300, 250 , "RP")
def BeginAble():
"""可 Begin"""
win.keyboard_bind('SPACE', Begin) # 綁定 Space 鍵
win.Button("Begin").config(state='normal') # Begin Button 啟用
def BeginUnable() :
"""不可 Begin"""
win.keyboard_unbind('SPACE') # 取消 Space 鍵的綁定
win.Button("Begin").config(state='disabled') # Begin Button 停用
def Begin():
"""開始"""
def resetQST():
"""根據模式重置QST圖片"""
match Game.NowMode():
case "SuperHHH":
qstpic = SuperQST
case "GreenWei":
qstpic = GreenQST
case "PiKaChu":
qstpic = KachuQST
case _:
qstpic = QST
win.update_by_tag("Game", "LP", qstpic)
win.update_by_tag("Game", "MP", qstpic)
win.update_by_tag("Game", "RP", qstpic)
win.update_by_tag("Game", "MarginScore", "") #邊際分數文字清除
win.update_by_tag("Game", "mod_2", "")
def change_pic_per500ms():
"""每隔0.5秒改圖片"""
win.after(500, lambda: win.update_by_tag("Game", "LP", Game.Ps[0].picture))
win.after(1000, lambda: win.update_by_tag("Game", "MP", Game.Ps[1].picture))
win.after(1500, lambda: win.update_by_tag("Game", "RP", Game.Ps[2].picture))
# Ding 音效
win.after(500, lambda: music.play_sound("Ding"))
win.after(1000, lambda: music.play_sound("Ding"))
win.after(1500, lambda: music.play_sound("Ding"))
def picture_and_sound():
match Game.NowMode():
case "Normal":
return
case "SuperHHH":
if Game.Ps[0].code == "B":
win.update_by_tag("Game", "LP" , super_hhh)
if Game.Ps[1].code == "B":
win.update_by_tag("Game", "MP" , super_hhh)
if Game.Ps[2].code == "B":
win.update_by_tag("Game", "RP" , super_hhh)
music.play_sound("SuperUP")
case "GreenWei":
if all(p.code == "A" for p in Game.Ps):
win.update_by_tag("Game", "LP" , GreenLeft)
win.update_by_tag("Game", "MP" , GreenMid)
win.update_by_tag("Game", "RP" , GreenRight)
elif any(p.code == "A" for p in Game.Ps):
if Game.Ps[0].code == "A":
win.update_by_tag("Game", "LP" , green_wei)
if Game.Ps[1].code == "A":
win.update_by_tag("Game", "MP" , green_wei)
if Game.Ps[2].code == "A":
win.update_by_tag("Game", "RP" , green_wei)
else:
win.update_by_tag("Game", "LP" , green_wei)
win.update_by_tag("Game", "MP" , green_wei)
win.update_by_tag("Game", "RP" , green_wei)
music.play_sound("GreenUP")
case "PiKaChu":
music.switch_music("KachuMusic")
if Game.Ps[0].code == "E":
win.update_by_tag("Game", "LP" , pikachu)
if Game.Ps[1].code == "E":
win.update_by_tag("Game", "MP" , pikachu)
if Game.Ps[2].code == "E":
win.update_by_tag("Game", "RP" , pikachu)
def screen_pop_music():
"""畫面、彈出圖片、音樂"""
match Game.NowMode():
case "Normal":
win.update_by_tag("Game", "BG", BG)
win.update_by_tag("Game", "Title", Title)
win.update_by_tag("Game", "mod_1", f"")
music.switch_music("bgm")
case "SuperHHH":
win.image_button("pop", lambda: win.delete_canvas_tag("Game", "pop"), "Game", SuperPOP, 225 , 400, "flat", 0)
win.update_by_tag("Game", "BG", SuperBG)
win.update_by_tag("Game", "Title", SuperTitle)
win.Canva("Game").itemconfig("mod_1", text = f"超級阿禾剩餘次數:{Game.SuperTimes}次", fill = "#FF00FF")
music.switch_music("SuperMusic")
if Game.double_score > 0:
win.Canva("Game").itemconfig("mod_2", text = f"(超級阿禾加倍分:{Game.double_score})", fill = "yellow")
case "GreenWei":
win.image_button("pop", lambda: win.delete_canvas_tag("Game", "pop"), "Game", GreenPOP, 225 , 400, "flat", 0)
win.update_by_tag("Game", "BG", GreenBG)
win.update_by_tag("Game", "Title", GreenTitle)
win.Canva("Game").itemconfig("mod_1", text = f"綠光阿瑋剩餘次數:{Game.GreenTimes}次", fill = "#00FF00")
music.switch_music("GreenMusic")
case "PiKaChu":
win.image_button("pop", lambda: win.delete_canvas_tag("Game", "pop"), "Game", KachuPOP, 225 , 400, "flat", 0)
win.update_by_tag("Game", "BG", KachuBG)
win.update_by_tag("Game", "Title", KachuTitle)
win.Canva("Game").itemconfig("mod_1", text = f"已觸發 {Game.kachu_times} 次皮卡丘充電", fill = "#FFFF00")
def result_txt():
"""顯示結果文字"""
win.update_by_tag("Game", "MarginScore", f"+{Game.margin_score}")
win.update_by_tag("Game", "Score", f"目前分數:{Game.score}")
win.update_by_tag("Game", "Times", f"剩餘次數:{Game.times - Game.played}")
win.update_by_tag("Game", "gss", f"咖波累積數:{Game.GssNum}")
match Game.NowMode():
case "SuperHHH":
win.Canva("Game").itemconfig("mod_1", text = f"超級阿禾剩餘次數:{Game.SuperTimes}次", fill = "#FF00FF")
case "GreenWei":
win.Canva("Game").itemconfig("mod_1", text = f"綠光阿瑋剩餘次數:{Game.GreenTimes}次", fill = "#00FF00")
#Main
win.delete_canvas_tag("Game", "pop")
BeginUnable()
resetQST()
Game.Logic()
change_pic_per500ms()
win.after(3000, result_txt)
if not Game.GameRunning():
Game.GameOver()
Sheet.CommitScore(Game.Name, Game.score)
win.after(3500, Game_over_to_End)
return
if Game.ModtoScreen:
win.after(2800, picture_and_sound)
win.after(3500, screen_pop_music)
win.after(3500, BeginAble)
return
#開始按紐
win.image_button(
"Begin",
Begin,
"Game",
BeginPIC,
225, 575
)
win.add_text(
"Game",
"",
5, 50,
15,
"white",
"PlayerName", #Game.Name
"w"
)
win.add_text(
"Game" ,
"" ,
225 , 478 ,
16 ,
"yellow" ,
"MarginScore" # Game.margin_score
)
win.add_text(
"Game" ,
"" ,
225 , 500 ,
16 ,
"white" ,
"Score" #Game.score
)
win.add_text(
"Game",
"",
225, 525,
16,
"white",
"Times" #Game.times - Game.played
)
win.add_text(
"Game" ,
"" ,
5, 775 ,
16 ,
"#FFBF00",
"history_score", #Game.history_score
"w" #靠左對齊
)
def bgm_on_off(game_running: bool= True) :
"""音樂開 & 關"""
match Game.NowMode():
case "SuperHHH":
file_name = "SuperMusic"
case "GreenWei":
file_name = "GreenMusic"
case "PiKaChu":
file_name = "KachuMusic"
case _:
file_name = "bgm"
#關
if music.bgm_playing or not game_running:
music.stop_music()
win.Button("music").config(text="關", bg="#C0C0C0")
print("BGM已停止")
#開
else :
music.play_music(file_name)
win.Button("music").config(text="開", bg="#00FF00")
print("BGM已開啟")
win.txt_button(
"music",
bgm_on_off,
"Game",
"關",
33, 33,
415, 765,
14,
"black",
"#C0C0C0"
)
#特殊模式顯示次數文字
win.add_text(
"Game",
"" ,
225 , 650 ,
16 ,
"white",
"mod_1",
)
win.add_text(
"Game",
"" ,
225 , 460 ,
10 ,
"white",
"mod_2",
)
win.add_text(
"Game",
f"咖波累積數:{Game.GssNum}" ,
445 , 50 ,
14 ,
"#00FF00",
"gss",
"e"
)
#endregion
#region End Screen
def Game_over_to_End():
bgm_on_off(Game.GameRunning())
music.play_sound("Ding")
print("切換至結束畫面")
win.update_by_tag("End", "PlayerName", f"{Game.Name}")
win.update_by_tag("End","over", "遊戲結束!")
win.update_by_tag("End","final_score", f"最終分數:{Game.score}") # 最終分數顯示
win.update_by_tag("End","history_score", f"歷史最高分數:{Game.history_score}")
win.switch_frame("Game", "End")
win.keyboard_bind("CTRL+S", save_json) #綁定CTRL+S保存.json檔案
def game_again():
"""再玩一次遊戲"""
BeginAble()
Game.Reset()
init_Game_screen_item()
bgm_on_off()
win.switch_frame("End", "Game")
win.keyboard_unbind("CTRL+S") #解除綁定CTRL+S
win.add_text(
"End",
"",
225, 175,
22,
"skyblue",
"PlayerName", #Game.Name
)
win.add_text(
"End" ,
"遊戲結束!" ,
225 , 260 ,
42 ,
"white" ,
"over"
)
win.add_text(
"End" ,
"" ,
225 , 325 ,
32 ,
"#FF0000" ,
"final_score" #Game.score
)
win.add_text(
"End" ,
f"歷史最高分數:{Game.history_score}" ,
225, 450 ,
16 ,
"#FFBF00" ,
"history_score" #Game.history_score
)
win.image_button(
"Again",
game_again,
"End",
AgainPIC,
225, 400
)
def save_json():
"""保存.json檔案"""
if isinstance(Game, JsonLaBaG):
print(f"此為json檔模擬模式 無法再次保存")
win.message_text(
2000,
"End",
f"此為json檔模擬模式 無法再次保存",
225, 730
)
return
import os
import json
from datetime import datetime
# 確保目錄存在
output_dir = "C:\\JsonLaBaG\\"
os.makedirs(output_dir, exist_ok=True)
# 使用時間戳作為部分文件名
timestamp = datetime.now().strftime("%Y%m%d")
filename = f"{output_dir}{Game.score}_{timestamp}.json"
try:
with open(filename, "w", encoding="utf-8") as file:
json.dump(Game.AllData, file, indent=4)
print(f"保存成功: {filename}")
win.message_text(
2000,
"End",
f"已成功保存至: {filename}",
225, 730
)
except Exception as e:
print(f"無法保存: {e}")
win.message_box(
f"無法保存: {e}",
)
win.txt_button(
"save_json",
save_json,
"End",
"保存本次紀錄(.json)",
10, 2,
225, 700,
12,
"black", "#00FF00"
)
win.load_picture("End" , SB, 0, 500, "SB")
#endregion
win.first_window("Home")
win.mainloop()
@.\Target.py
#目標分數
import math
from src.Sheet import Sheet
from src.LaBaG import LaBaG
class TargetLaBaG(LaBaG):
def __init__(self):
super().__init__()
delattr(self, "kachu_times")
self.superS = 0
self.greenS = 0
self.kachuS = 0
def Reset(self):
super().Reset()
self.superS = 0
self.greenS = 0
self.kachuS = 0
def JudgeMode(self):
"""判斷模式"""
if not self.GameRunning():
#關掉其他模式
self.SuperHHH = False
self.GreenWei = False
#判斷皮卡丘充電
if any(p.code == "E" for p in self.Ps) :
self.PiKaChu = True
self.played -= 5
self.kachuS += 1
self.ModtoScreen = True
else:
self.PiKaChu = False
return
match self.NowMode():
case "Normal" | "PiKaChu":
#判斷超級阿禾
hhh_appear = any(p.code == "B" for p in self.Ps) #判斷是否有任何阿禾
if self.SuperNum <= self.SuperRate and hhh_appear:
self.SuperHHH = True
self.SuperTimes += 6
self.superS += 1
if self.PiKaChu:
self.PiKaChu = False
self.ModtoScreen = True
#超級阿禾加倍
if all(p.code == "B" for p in self.Ps):
self.double_score = int(round(self.score / 2)) * self.score_time
self.score += self.double_score
return
#判斷綠光阿瑋
gss_all = all(p.code == "A" for p in self.Ps) #判斷是否有出現並全部咖波
if self.GreenNum <= self.GreenRate and gss_all :
self.GreenWei = True
self.GreenTimes += 2
self.greenS += 1
if self.PiKaChu:
self.PiKaChu = False
self.ModtoScreen = True
return
elif self.GssNum >= 20 : #咖波累積數達到20
self.GreenWei = True
self.GreenTimes += 2
self.greenS += 1
self.GssNum = 0
if self.PiKaChu:
self.PiKaChu = False
self.ModtoScreen = True
return
case "SuperHHH":
self.SuperTimes -= 1
if all(p.code == "B" for p in self.Ps):
self.SuperTimes += 2
if self.SuperTimes <= 0 : #超級阿禾次數用完
self.SuperHHH = False
self.JudgeMode() #判斷是否可再進入特殊模式
self.ModtoScreen = True
return
case "GreenWei":
self.GreenTimes -= 1
if all(p.code == "A" for p in self.Ps):
self.GreenTimes += 1
if self.GreenTimes <= 0 : #綠光阿瑋次數用完
self.GreenWei = False
self.JudgeMode() #判斷是否可再進入特殊模式
self.ModtoScreen = True
return
while True:
try:
target = int (input("請輸入目標分數"))
if target > 0:
break
else:
print("目標分數必須大於 0")
except ValueError as e:
print(f"請輸入有效的數字: {e}")
Game = TargetLaBaG()
recent_max = 0
i = 0
while True :
i += 1
if i < 10 :
LOG = 2
else:
LOG = int (round(math.log10(i)) + 2)
Game.Logic()
print(f"第{i : {LOG}}次 分數:{Game.score : 8} ({Game.superS : 2} 次 超級阿禾 )({Game.greenS : 2} 次 綠光阿瑋 )({Game.kachuS : 2} 次 皮卡丘充電)【目前最大值:{recent_max}】")
# 檢查是否達到目標
if Game.score >= target:
break # 如果達到目標,則退出迴圈
elif Game.score > recent_max:
recent_max = Game.score
if recent_max >= 1000000:
Sheet.CommitScore('模擬測試最高分', recent_max)
print (f"第{i: {LOG}}次達成:{Game.score : 8} ({Game.superS : 2} 次 超級阿禾 )({Game.greenS : 2} 次 綠光阿瑋 )({Game.kachuS : 2} 次 皮卡丘充電)")
@.\TargetJson.py
#產生目標分數的隨機數 json 檔
import math
import os
from datetime import datetime
import json
from src.Sheet import Sheet
from src.LaBaG import LaBaG
while True:
try:
target = int (input("請輸入目標分數"))
if target > 0:
break
else:
print("目標分數必須大於 0")
except ValueError as e:
print(f"請輸入有效的數字: {e}")
Game = LaBaG()
recent_max = 0
recent_total = 0
i = 0
while True :
Game.Logic()
i += 1
recent_total = (recent_total + Game.score) % 1000000000000000
if Game.score > recent_max:
recent_max = Game.score
print(f"第{i : {2 if i < 10 else int (round(math.log10(i)) + 2)}}次 分數:{Game.score : 8}【目前最大值:{recent_max}】【目前平均值:{recent_total / i % 1000000000000000 :.2f}】")
# 檢查是否達到目標
if Game.score >= target:
break # 如果達到目標,則退出迴圈
if Game.score > 1000000:
Sheet.CommitScore('模擬測試最高分', Game.score)
# 確保目錄存在
output_dir = "C:\\JsonLaBaG\\"
os.makedirs(output_dir, exist_ok=True)
# 使用時間戳作為部分文件名
timestamp = datetime.now().strftime("%Y%m%d")
with open(f"{output_dir}{Game.score}_{timestamp}.json", "w", encoding="utf-8") as file:
json.dump(Game.AllData, file, indent=4)
@.\test.py
from GUI import Window
win = Window("Main")
win.setup_frame_and_canvas("Main")
i = 0
def OpenSub(root):
global i
i += 1
root.setup_subwindow(f"Sub{i}")
now_win = root.SubWindow(f"Sub{i}")
now_win.txt_button(
"OpenSub",
lambda :OpenSub(now_win),
f"master: {root.title()}",
10, 10,
150, 150
)
win.txt_button(
"OpenSub",
lambda :OpenSub(win),
"Main",
"Main",
10, 10,
150, 150
)
win.first_window("Main")
win.mainloop()
@.\GUI\yieldb64.py
import os
import base64
#產生 base64 音訊與圖像
asset_dir_items = os.listdir(".\\Asset")
images = list()
sounds = list()
def sort_item(dir_lsit:list):
for item in dir_lsit:
item_path = os.path.join(".\\Asset", item)
_, extension = os.path.splitext(item_path) # 使用 splitext 分離文件名與副檔名
if extension == ".jpg" or extension == ".png" or extension == ".ico":
images.append(item)
elif extension == ".wav":
sounds.append(item)
def encode_image(files: list):
"""產生base64編碼的圖像字典檔"""
image_dict = dict()
for file in files:
with open(f".\\Asset\\{file}", mode = "rb") as f :
image_b64 = base64.b64encode(f.read())
image_dict[file.split(".")[0]] = image_b64
with open((f".\\src\\imageb64.py"), mode = "w") as f:
f.write("image_dict =")
f.write(image_dict.__repr__())
def encode_wav(files: list):
"""產生base64編碼的音訊字典檔"""
sounds = dict()
for file in files:
with open(f'.\\Asset\\{file}', mode = 'rb') as f:
sound_b64 = base64.b64encode(f.read())
sounds[file.split('.')[0]] = sound_b64
with open(f'.\\src\\soundb64.py', mode='w') as f:
f.write("wav_dict =")
f.write(sounds.__repr__())
sort_item(asset_dir_items)
encode_image(images)
encode_wav(sounds)
@.\GUI\__init__.py
import logging
from typing import Union
import tkinter as tk
from tkinter import messagebox, filedialog
from PIL import Image, ImageTk
import base64
from io import BytesIO
import os
import wave
import tempfile
from pygame import mixer
# 設置基本日誌配置
logging.basicConfig(
level=logging.DEBUG, # 設置最低日誌級別(DEBUG, INFO, WARNING, ERROR, CRITICAL)
format='%(asctime)s - %(levelname)s - %(message)s', # 設置日誌格式
handlers=[
logging.StreamHandler() # 只输出到控制台
]
)
class BaseWindow():
"""基本視窗類"""
KEYSDICT = {
# 滑鼠按鍵
"CLICK_LEFT": "<Button-1>", # 左鍵
"CLICK_RIGHT": "<Button-3>", # 右鍵
"SCROLL": "<Button-2>", # 滾輪
"SCROLL_UP": "<Button-4>", # 滾輪上滾
"SCROLL_DOWN": "<Button-5>", # 滾輪下滾
# 鍵盤按鍵
**{str(i): str(i) for i in range(10)}, # 0~9
**{chr(i): chr(i) for i in range(65, 91)}, # A~Z
**{chr(i): chr(i) for i in range(97, 123)}, # a~z
"ENTER": "<Return>", "ESC": "<Escape>", "SPACE": "<space>", "TAB": "<Tab>",
"BACKSPACE": "<BackSpace>", "DELETE": "<Delete>", "INSERT": "<Insert>",
"UP": "<Up>", "DOWN": "<Down>", "LEFT": "<Left>", "RIGHT": "<Right>",
"PAGE_UP": "<Prior>", "PAGE_DOWN": "<Next>", "HOME": "<Home>", "END": "<End>",
**{f"F{i}": f"<F{i}>" for i in range(0, 13)}, # F1~F12
"LEFT_CTRL": "<Control_L>", "RIGHT_CTRL": "<Control_R>",
"LEFT_ALT": "<Alt_L>", "RIGHT_ALT": "<Alt_R>", # Alt 鍵
"LEFT_SHIFT": "<Shift_L>", "RIGHT_SHIFT": "<Shift_R>", # Shift 鍵
"CAPS_LOCK": "<Caps_Lock>","CAPSLOCK": "CAPSLOCK", "NUM_LOCK": "<Num_Lock>", "SCROLL_LOCK": "<Scroll_Lock>",
"LEFT_WIN": "<Super_L>", "RIGHT_WIN": "<Super_R>", # Windows 鍵
"LEFT_COMMAND": "<Super_L>", "RIGHT_COMMAND": "<Super_R>", # MacOS Command 鍵
"LEFT_OPTION": "<Alt_L>", "RIGHT_OPTION": "<Alt_R>", # MacOS Option 鍵
"LEFT_META": "<Meta_L>", "RIGHT_META": "<Meta_R>", # Meta 鍵(某些鍵盤的特殊鍵)
"MENU": "<Menu>", "CONTEXT_MENU": "<Menu>", # Menu 鍵
"PRINT_SCREEN": "<Print>", "PAUSE": "<Pause>", "BREAK": "<Break>", # PrintScreen, Pause/Break 鍵
"SYSREQ": "<Sys_Req>", # 系統請求鍵
"HELP": "<Help>", # 幫助鍵
# 數字鍵盤
**{f"NUM_{i}": f"<KP_{i}>" for i in range(0, 10)}, # 0~9
"NUM_ADD": "<KP_Add>", "NUM_SUB": "<KP_Subtract>",
"NUM_MUL": "<KP_Multiply>", "NUM_DIV": "<KP_Divide>", # 數字鍵盤上的運算符
"NUM_ENTER": "<KP_Enter>", "NUM_POINT": "<KP_Decimal>", # 數字鍵盤上的Enter和小數點
# 組合鍵
"CTRL+SHIFT+ALT+DEL": "<Control-Shift-Alt-Delete>", # Ctrl + Shift + Alt + Delete
**{f"SHIFT+{chr(i)}".upper(): f"<Shift-{chr(i)}>" for i in range(97, 123)}, # Shift + a~z
**{f"SHIFT+{i}".upper(): f"<Shift-{i}>" for i in range(10)}, # Shift + 0~9
**{f"CTRL+{chr(i)}".upper(): f"<Control-{chr(i)}>" for i in range(97, 123)}, # Ctrl + a~z
**{f"CTRL+{i}".upper(): f"<Control-{i}>" for i in range(10)}, # Ctrl + 0~9
**{f"ALT+{chr(i)}".upper(): f"<Alt-{chr(i)}>" for i in range(97, 123)}, # Alt + a~z
**{f"ALT+{i}".upper(): f"<Alt-{i}>" for i in range(10)}, # Alt + 0~9
"CTRL+SHIFT": "<Control-Shift>", # Ctrl + Shift
**{f"CTRL+SHIFT+{chr(i)}".upper(): f"<Control-Shift-{chr(i)}>" for i in range(97, 123)}, # Ctrl + Shift + a~z
**{f"CTRL+SHIFT+{i}".upper(): f"<Control-Shift-{i}>" for i in range(10)}, # Ctrl + Shift + 0~9
"CTRL+ALT": "<Control-Alt>", # Ctrl + Alt
**{f"CTRL+ALT+{chr(i)}".upper(): f"<Control-Alt-{chr(i)}>" for i in range(97, 123)}, # Ctrl + Alt + a~z
**{f"CTRL+ALT+{i}".upper(): f"<Control-Alt-{i}>" for i in range(10)}, # Ctrl + Alt + 0~9
"SHIFT+ALT": "<Shift-Alt>", # Shift + Alt
**{f"SHIFT+ALT+{chr(i)}".upper(): f"<Shift-Alt-{chr(i)}>" for i in range(97, 123)}, # Shift + Alt + a~z
**{f"SHIFT+ALT+{i}".upper(): f"<Shift-Alt-{i}>" for i in range(10)}, # Shift + Alt + 0~9
"CTRL+SHIFT+ALT": "<Control-Shift-Alt>", # Ctrl + Shift + Alt
**{f"CTRL+SHIFT+ALT+{chr(i)}".upper(): f"<Control-Shift-Alt-{chr(i)}>" for i in range(97, 123)}, # Ctrl + Shift + Alt + a~z
**{f"CTRL+SHIFT+ALT+{i}".upper(): f"<Control-Shift-Alt-{i}>" for i in range(10)}, # Ctrl + Shift + Alt + 0~9
}
def __init__(self, title: str = None, width: int = 300, height: int = 300):
self.window_title = title
self.width = width
self.height = height
self.__frame_dict = dict()
self.__canvas_dict = dict()
self.__button_dict = dict()
self.__entry_dict = dict()
self.__subwindow_dict = dict()
def Canva(self, canvas_name: str= None) -> tk.Canvas:
"使用 Tkinter.Canvas 相關操作"
if canvas_name in self.__canvas_dict:
return self.__canvas_dict[canvas_name]
else:
raise KeyError(f"無法從 {type(self).__name__}.__canvas_dict 找到名為 {canvas_name} 的canvas")
def Frame(self, frame_name: str= None) -> tk.Frame:
"使用 Tkinter.Frame 相關操作"
if frame_name in self.__frame_dict:
return self.__frame_dict[frame_name]
else:
raise KeyError(f"無法從 {type(self).__name__}.__frame_dict 找到名為 {frame_name} 的frame")