-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathScratchOff.py
572 lines (534 loc) · 18.6 KB
/
ScratchOff.py
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
# Import
import os
os.environ["PYGAME_HIDE_SUPPORT_PROMPT"] = "0"
import pygame
import sys
import random
import ctypes
import time
import subprocess
import datetime
import tkinter
import tkinter.messagebox
from PIL import Image, UnidentifiedImageError
from _tkinter import TclError
# Constant
IMAGEDIR = "pictures"
SUPPORTEXTS = ["jpeg", "jpg", "png", "bmp"]
SCREENSIZE = (800, 750)
WHITE = (255, 255, 255, 27)
GRAY = (192, 192, 192)
sys_lang = hex(ctypes.windll.kernel32.GetSystemDefaultUILanguage())
IsZh_Hans = (sys_lang == "0x804")
DEBUG = False
BOMBTIME = "2022.8.1 00:00:00"
# Version
major = 1
minor = 2
releases = 0
build = 12
typenum = 6
x = 1
# Language
if IsZh_Hans:
Lang_Title = "刮刮乐"
Lang_Quit = "退出"
Lang_About = "关于"
Lang_About_Message = "本程序受 MIT 许可证保护\nCopyright © 2022 Class Tools Develop Team. All rights reserved.\nGithub 存储库:https://github.com/class-tools/ScratchOff\n"
Lang_About_Confidential = "Class Tools 机密\n以任何方式进行未经授权的使用或披露可能会招致惩戒处分,\n最严重的处罚可要求承担可能的民事与刑事责任。\n"
Lang_About_Test = "仅用于测试。版本号:"
Lang_About_Releases = "版本号:"
else:
Lang_Title = "Scratch Off"
Lang_Quit = "Quit"
Lang_About = "About"
Lang_About_Message = "This program uses MIT License\nCopyright © 2022 Class Tools Develop Team. All rights reserved.\nGithub Repository: https://github.com/class-tools/ScratchOff\n"
Lang_About_Confidential = "Class Tools Confidential\nUnauthorized use or disclosure in any way may result in disciplinary action, \nand the most serious punishment may require possible civil and criminal liability.\n"
Lang_About_Test = "Only for test. Version: "
Lang_About_Releases = "Version: "
def noPicturesError():
"""No pictures error"""
quit(1)
root.wm_attributes('-topmost', 1)
printLog("ERROR", "Cannot read pictures file data.")
if IsZh_Hans:
if tkinter.messagebox._show(
"错误",
"无法读取图片数据。\n如果你认为这是程序的问题,你可以点击“是”跳转到 Issues 界面反馈。",
tkinter.messagebox.ERROR,
tkinter.messagebox.YESNO) == "yes":
subprocess.run("start website/issues.url", shell=True)
else:
if tkinter.messagebox._show(
"Error",
"Cannot read pictures file data.\nIf you think it is a bug, you can click \"yes\" to skip to the issues page.",
tkinter.messagebox.ERROR,
tkinter.messagebox.YESNO) == "yes":
subprocess.run("start website/issues.url", shell=True)
quit(3)
def noIconError():
"""No icon error"""
quit(1)
root.wm_attributes('-topmost', 1)
printLog("ERROR", "Cannot read icon.")
if IsZh_Hans:
if tkinter.messagebox._show(
"错误",
"无法读取图标。\n如果你认为这是程序的问题,你可以点击“是”跳转到 Issues 界面反馈。",
tkinter.messagebox.ERROR,
tkinter.messagebox.YESNO) == "yes":
subprocess.run("start website/issues.url", shell=True)
else:
if tkinter.messagebox._show(
"Error",
"Cannot read icon.\nIf you think it is a bug, you can click \"yes\" to skip to the issues page.",
tkinter.messagebox.ERROR,
tkinter.messagebox.YESNO) == "yes":
subprocess.run("start website/issues.url", shell=True)
quit(3)
def noFontError():
"""No font error"""
quit(1)
root.wm_attributes('-topmost', 1)
printLog("ERROR", "Cannot read font data.")
if IsZh_Hans:
if tkinter.messagebox._show(
"错误",
"无法读取字体数据。\n如果你认为这是程序的问题,你可以点击“是”跳转到 Issues 界面反馈。",
tkinter.messagebox.ERROR,
tkinter.messagebox.YESNO) == "yes":
subprocess.run("start website/issues.url", shell=True)
else:
if tkinter.messagebox._show(
"Error",
"Cannot read font data.\nIf you think it is a bug, you can click \"yes\" to skip to the issues page.",
tkinter.messagebox.ERROR,
tkinter.messagebox.YESNO) == "yes":
subprocess.run("start website/issues.url", shell=True)
quit(3)
def cannotWriteLogError():
"""Cannot write log error"""
quit(1)
root.wm_attributes('-topmost', 1)
if IsZh_Hans:
if tkinter.messagebox._show(
"错误",
"无法写入日志。\n如果你认为这是程序的问题,你可以点击“是”跳转到 Issues 界面反馈。",
tkinter.messagebox.ERROR,
tkinter.messagebox.YESNO) == "yes":
subprocess.run("start website/issues.url", shell=True)
else:
if tkinter.messagebox._show(
"Error",
"Cannot write log file.\nIf you think it is a bug, you can click \"yes\" to skip to the issues page.",
tkinter.messagebox.ERROR,
tkinter.messagebox.YESNO) == "yes":
subprocess.run("start website/issues.url", shell=True)
quit(4)
def resize(size):
while True:
if size[0] < 800 and size[1] < 800:
size = (size[0] * 1.5, size[1] * 1.5)
else:
break
while True:
if size[0] > 800 and size[1] > 800:
size = (size[0] * 0.5, size[1] * 0.5)
else:
break
size = (int(size[0]), int(size[1]))
return size
def readImageRandomly():
"""Random a image."""
global SCREENSIZE, ONEIMAGE
filenames = os.listdir(IMAGEDIR)
filenames = [f for f in filenames if f.split(".")[-1] in SUPPORTEXTS]
if len(filenames) == 1:
ONEIMAGE = True
else:
ONEIMAGE = False
imgpath = os.path.join(IMAGEDIR, random.choice(filenames))
img = Image.open(imgpath)
SCREENSIZE = img.size
SCREENSIZE = resize(SCREENSIZE)
return [pygame.transform.scale(pygame.image.load(
imgpath), SCREENSIZE), imgpath[9:], img.size]
def printLog(logType, logContent):
"""Print log"""
if logType != "DEBUG" or DEBUG:
try:
with open("log/latest.log", "a") as file:
file.write(
time.strftime("[%Y.%m.%d %H:%M:%S] [") +
str(logType) +
"]: " +
str(logContent) +
"\n")
print(" [" + logType + "]: " + logContent)
except FileNotFoundError:
cannotWriteLogError()
def getTime(log):
"""Get log time"""
try:
with open(log, "r") as file:
firstLine = file.readline()
time = ""
for i in firstLine:
if i == "]":
break
try:
time += str(int(i))
except ValueError:
continue
return time
except FileNotFoundError:
return 1145141919810
def watermark():
font20 = pygame.font.Font("font.ttc", 20)
font15 = pygame.font.Font("font.ttc", 15)
if typenum <= 4:
text1 = font20.render("Class Tools 机密", True, (0, 0, 0))
text2 = font15.render("以任何方式进行未经授权的使用或披露", True, (0, 0, 0))
text3 = font15.render("可能会招致惩戒处分,最严重的处罚可", True, (0, 0, 0))
text4 = font15.render("要求承担可能的民事与刑事责任。", True, (0, 0, 0))
text5 = font15.render(
"仅用于测试。版本号:" +
str(major) +
"." +
str(minor) +
"." +
str(releases) +
"." +
str(build) +
"." +
str(typenum) +
"." +
str(x),
True,
(0,
0,
0))
screen.blit(text1, (43, 0))
screen.blit(text2, (0, 30))
screen.blit(text3, (0, 45))
screen.blit(text4, (0, 60))
screen.blit(text5, (0, 90))
elif typenum <= 8:
text = font15.render(
"仅用于测试。版本号:" +
str(major) +
"." +
str(minor) +
"." +
str(releases) +
"." +
str(build) +
"." +
str(typenum) +
"." +
str(x),
True,
(0,
0,
0))
screen.blit(text, (0, 0))
def Menu_About():
"""About in menu bar"""
printLog("INFO", "Use menu command \"About\".")
about = tkinter.Toplevel()
try:
about.iconbitmap("SO.ico")
except TclError:
noIconError()
about.title(Lang_About)
about.resizable(width=False, height=False)
about.wm_attributes('-topmost', 1)
image = tkinter.PhotoImage(file="about.png")
icon = tkinter.Label(about, image=image)
icon.image = image
icon.pack()
if typenum <= 4:
message = tkinter.Label(
about, text=Lang_About_Message)
messageConfidential = tkinter.Label(
about, text=Lang_About_Confidential, fg="red")
messageVersion = tkinter.Label(about,
text=Lang_About_Test +
str(major) +
"." +
str(minor) +
"." +
str(releases) +
"." +
str(build) +
"." +
str(typenum) +
"." +
str(x))
message.pack()
messageConfidential.pack()
messageVersion.pack()
elif typenum <= 8:
message = tkinter.Label(about,
text=Lang_About_Message)
messageVersion = tkinter.Label(about,
text=Lang_About_Test +
str(major) +
"." +
str(minor) +
"." +
str(releases) +
"." +
str(build) +
"." +
str(typenum) +
"." +
str(x))
message.pack()
messageVersion.pack()
else:
message = tkinter.Label(about,
text=Lang_About_Message)
messageVersion = tkinter.Label(about,
text=Lang_About_Releases +
str(major) +
"." +
str(minor) +
"." +
str(releases) +
"." +
str(build) +
"." +
str(typenum) +
"." +
str(x))
message.pack()
messageVersion.pack()
def Menu_Quit():
"""Quit in menu bar"""
printLog("INFO", "Use menu command \"Quit\".")
quit(2)
def init(inittype):
if inittype == 1:
global root, embed
root = tkinter.Tk()
title = ""
if typenum == 1:
title += "[内部预览版] "
elif typenum == 2:
title += "[内部调试版] "
elif typenum == 4:
title += "[内部测试版] "
elif typenum == 6:
title += "[公共预览版] "
elif typenum == 8:
title += "[公共测试版] "
elif typenum == 10:
title += "[预先发布版] "
if DEBUG:
title += "[开发人员模式] "
root.title(title + Lang_Title)
root.geometry(str(SCREENSIZE[0]) +
"x" +
str(SCREENSIZE[1]) +
"+" +
str(int(root.winfo_screenwidth() /
2 -
SCREENSIZE[0] /
2)) +
"+" +
str(int(root.winfo_screenheight() /
2 -
SCREENSIZE[1] /
2)))
root.resizable(width=False, height=False)
try:
root.iconbitmap("SO.ico")
except TclError:
noIconError()
menubar = tkinter.Menu(root)
root["menu"] = menubar
menubar.add_command(label=Lang_About, command=Menu_About)
menubar.add_command(label=Lang_Quit, command=Menu_Quit)
embed = tkinter.Frame(root, width=SCREENSIZE[0], height=SCREENSIZE[1])
embed.grid(columnspan=(600), rowspan=500)
embed.pack(side=tkinter.LEFT)
os.environ["SDL_WINDOWID"] = str(embed.winfo_id())
os.environ["SDL_VIDEODRIVER"] = "windib"
elif inittype == 2:
global screen
pygame.init()
pygame.mouse.set_cursor(*pygame.cursors.diamond)
screen = pygame.display.set_mode(SCREENSIZE)
def quit(quittype):
"""Quit the program."""
if quittype == 1:
pygame.quit()
elif quittype == 2:
root.destroy()
elif quittype == 3:
printLog("INFO", "Exiting ScratchOff.")
sys.exit(0)
elif quittype == 4:
sys.exit(0)
# Create log
subprocess.run("md log > temp.txt 2> temp2.txt", shell=True)
subprocess.run("del temp.txt", shell=True)
subprocess.run("del temp2.txt", shell=True)
lastTime = getTime("log/latest.log")
if lastTime != 1145141919810:
subprocess.run("cd log && ren latest.log " + lastTime + ".log", shell=True)
file = open("log/latest.log", "w")
file.close()
printLog("INFO", "Starting ScratchOff.")
# Timebomb
if typenum <= 12:
if BOMBTIME != "1145141919810":
now = datetime.datetime.now()
bomb = datetime.datetime.strptime(BOMBTIME, "%Y.%m.%d %H:%M:%S")
if now > bomb:
printLog("INFO", "You use the program after the timeless.")
quit(3)
# Read image
while True:
try:
image = readImageRandomly()
except FileNotFoundError:
init(1)
noPicturesError()
except IndexError:
init(1)
noPicturesError()
except UnidentifiedImageError as errorMsg:
subprocess.run(
"del \"pictures\\" +
str(errorMsg)[
38:-
1] +
"\"",
shell=True)
printLog(
"WARNING",
"You have a unsupported picture, its name is: \"" +
str(errorMsg)[
38:-
1] +
"\". We delete the picture.")
continue
else:
break
printLog("INFO", "Use image " + image[1] + ", image size: " + str(image[2]))
printLog("INFO", "Set screen size to " + str(SCREENSIZE))
# Init window
init(1)
# Warn
if typenum <= 4:
root.wm_attributes('-topmost', 1)
if tkinter.messagebox._show(
"警告",
"此版本为内部版本,版本号:" +
str(major) +
"." +
str(minor) +
"." +
str(releases) +
"." +
str(build) +
"." +
str(typenum) +
"." +
str(x) +
"。\n以任何方式进行未经授权的使用或披露可能会招致惩戒处分,最严重的处罚可要求承担可能的民事与刑事责任。\n若以认真阅读这段文字,请点击否确认。",
tkinter.messagebox.WARNING,
tkinter.messagebox.YESNO) == "yes":
printLog("INFO", "You click yes.")
quit(1)
quit(3)
elif typenum <= 8:
root.wm_attributes('-topmost', 1)
if tkinter.messagebox._show(
"警告",
"此版本为测试版本,仅用于测试,版本号:" +
str(major) +
"." +
str(minor) +
"." +
str(releases) +
"." +
str(build) +
"." +
str(typenum) +
"." +
str(x) +
"。\n若以认真阅读这段文字,请点击否确认。",
tkinter.messagebox.WARNING,
tkinter.messagebox.YESNO) == "yes":
printLog("INFO", "You click yes.")
quit(1)
quit(3)
root.wm_attributes('-topmost', 0)
# Init game
init(2)
surface = pygame.Surface(SCREENSIZE).convert_alpha()
surface.fill(GRAY)
# Main
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit(1)
quit(3)
mouse_event_flags = pygame.mouse.get_pressed()
if mouse_event_flags[0]:
pygame.draw.circle(surface, WHITE, pygame.mouse.get_pos(), 40)
printLog("DEBUG", "Left button pressed, position: " +
str(pygame.mouse.get_pos()) + ".")
elif mouse_event_flags[-1]:
printLog("INFO", "Right button pressed, game restart.")
old = image
while True:
try:
image = readImageRandomly()
except FileNotFoundError:
noPicturesError()
except UnidentifiedImageError as errorMsg:
subprocess.run(
"del \"pictures\\" +
str(errorMsg)[
38:-
1] +
"\"",
shell=True)
printLog(
"WARNING",
"You have a unsupported picture, its name is: \"" +
str(errorMsg)[
38:-
1] +
"\". We delete the picture.")
continue
if image[1] != old[1]:
break
if ONEIMAGE:
printLog("WARNING", "You only have one image.")
break
printLog("INFO", "Use image " +
image[1] + ", image size: " + str(image[2]))
printLog("INFO", "Set screen size to " + str(SCREENSIZE))
quit(1)
quit(2)
init(1)
init(2)
surface = pygame.Surface(SCREENSIZE).convert_alpha()
surface.fill(GRAY)
screen.blit(image[0], (0, 0))
screen.blit(surface, (0, 0))
try:
watermark()
except FileNotFoundError:
noFontError()
pygame.display.update()
try:
root.update()
except TclError:
quit(1)
quit(3)