-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
127 lines (90 loc) · 3.06 KB
/
Copy pathmain.py
File metadata and controls
127 lines (90 loc) · 3.06 KB
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
import win32gui
import win32con
from PIL import ImageGrab
import time
import numpy as np
import ddddocr
from io import BytesIO
import re
import pygame
import threading
def choose_right(old,normal,ezh):
try:
normal = re.sub(r'[^0-9]', '', normal)
ezh = re.sub(r'[^0-9]', '', ezh)
normal = float(normal)/100
ezh = float(ezh)/100
except:
return old
if normal < 1 or ezh < 1:
if old >= 3:
return old
if normal == 0 or ezh == 0:
return 0
elif abs(normal - old) <=5:
return normal
elif abs(ezh - old) <=5:
return ezh
else:
return old
ocr = ddddocr.DdddOcr()
old = 0
hwnd = win32gui.FindWindow(None, "openBVE")
if hwnd == 0:
print("Cannot find openBVE window.")
exit()
else:
client_rect = win32gui.GetClientRect(hwnd)
left_top = win32gui.ClientToScreen(hwnd, (0, 0))
right_bottom = win32gui.ClientToScreen(hwnd, (client_rect[2], client_rect[3]))
rect = (left_top[0], left_top[1], right_bottom[0], right_bottom[1])
scale_factor = 1.5 # YOU SHOULD CHANGE IT.(150%=1.5,and so on)
corrected_rect = (
int(rect[0] * scale_factor),
int((rect[1] * scale_factor)+2),
int(rect[2] * scale_factor),
int(rect[3] * scale_factor)
)
def get_speed():
while True:
global old
screenshot = ImageGrab.grab(bbox=corrected_rect)
h = screenshot.height
speed_roi = screenshot.crop((10, h - 93, 72, h - 76))
img_bytes = BytesIO()
speed_roi.save(img_bytes, format='PNG')
result_normal = ocr.classification(img_bytes.getvalue())
speed_gray = speed_roi.convert('L')
threshold = 150 # You can change it.
speed_roi = speed_gray.point(lambda x: 255 if x > threshold else 0, 'L')
img_bytes = BytesIO()
speed_roi.save(img_bytes, format='PNG')
result_ezh = ocr.classification(img_bytes.getvalue())
old = choose_right(old,result_normal,result_ezh)
print(f'{old}')
pygame.init()
screen = pygame.display.set_mode((200, 200))
CENTER = (100,100)
bg = pygame.image.load('bg.png').convert()
pt = pygame.image.load('pt.png').convert_alpha()
bg = pygame.transform.smoothscale(bg, (200, 200))
pt = pygame.transform.smoothscale(pt, (200, 200))
font = pygame.font.Font(None, 30)
clock = pygame.time.Clock()
threading.Thread(target=get_speed, daemon=True).start()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
speed = old
screen.blit(bg, (0, 0))
angle = speed * (23 / 10)
rotated_needle = pygame.transform.rotate(pt, -angle)
needle_rect = rotated_needle.get_rect(center=CENTER)
screen.blit(rotated_needle, needle_rect)
text = font.render(f"{int(speed)}", True, (255, 255, 255))
screen.blit(text, (85,89))
pygame.display.flip()
clock.tick(10)
pygame.quit()