-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathkey.py
176 lines (153 loc) · 5.84 KB
/
key.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
import socket
import pygame
from random import randint
import os
if os.name == "nt":
on_windows = True
else:
on_windows = False
# noinspection PyUnresolvedReferences,PyProtectedMember
from pygame._sdl2 import Window
from typing import Any
from _thread import start_new_thread
if on_windows:
import winsound
from time import sleep
from json import loads, dumps, load
if on_windows:
from pymsgbox import alert
from tkinter import messagebox as mb
class LimboKeysClient:
def __init__(self):
self.id = -1 # 0-7 assigned by server, -1 if unknown
self.unique_keys = [pygame.image.load("key{}.png".format(i)).convert_alpha() for i in range(8)]
self.position = [0, -300]
self.id_surface = pygame.Surface((0, 0))
self.wants_to_quit = False
self.alive = True
self.highlight_amount: float = 0
self.clicked = False
self.clickable = False
self.success = False
self.movement_finished = False
start_new_thread(self.listening_thread, ())
def listening_thread(self):
try:
assigned_client_id = False
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect(("localhost", 6666))
s.sendall(dumps({"quit": False, "clicked": False}).encode('ascii'))
while True:
sleep(0.02)
msg: dict[str, Any] = loads(s.recv(1024).decode('ascii'))
self.id = msg["id"]
self.position = msg["position"]
self.alive = msg["alive"]
self.success = msg["success"]
self.clickable = msg["clickable"]
self.movement_finished = msg["movement_finished"]
self.highlight_amount = min(1, max(self.highlight_amount + msg["highlight"] * 4 / FRAMERATE, 0))
if not assigned_client_id:
if self.id == 0:
pygame.mixer.music.load("LIMBO.mp3")
pygame.mixer.music.set_volume(0.3)
if music:
pygame.mixer.music.play()
pygame.mixer.music.set_pos(176)
self.id_surface = font.render(str(self.id), True, (0, 0, 0))
assigned_client_id = True
s.sendall(dumps({"quit": self.wants_to_quit, "clicked": self.clicked}).encode('ascii'))
except Exception as e:
print(e)
WIDTH, HEIGHT, FRAMERATE = 150, 150, 75
# configurables (do config.json)
colored = False
borderless = False
transparent = False
music = True
sfx = True
# ==============================
try:
with open("config.json") as f:
data: dict[str, Any] = load(f)
colored = data.get("colored", False)
borderless = data.get("borderless", False)
transparent = data.get("transparent", False)
music = data.get("music", True)
sfx = data.get("sfx", True)
except FileNotFoundError:
pass
pygame.init()
flags = 0
if borderless:
flags |= pygame.NOFRAME
screen = pygame.display.set_mode([WIDTH, HEIGHT], flags=flags)
clock = pygame.time.Clock()
font = pygame.font.SysFont("Arial", 20)
key = pygame.image.load("key.png").convert_alpha()
green_key = pygame.image.load("green-key.png").convert_alpha()
pygame.display.set_caption("LIMBO")
client = LimboKeysClient()
pgwindow = Window.from_display_module()
if on_windows:
import win32api
import win32com.client
import win32con
import win32gui
if transparent:
# Create layered window
hwnd = pygame.display.get_wm_info()["window"]
win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE,
win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) | win32con.WS_EX_LAYERED)
# Set window transparency color
win32gui.SetLayeredWindowAttributes(hwnd, win32api.RGB(*(1, 1, 1)), 0, win32con.LWA_COLORKEY)
running = True
while running and client.alive:
for event in pygame.event.get():
if event.type == pygame.QUIT:
client.wants_to_quit = True
sleep(0.1)
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if client.clickable:
client.clicked = True
screen.fill((1, 1, 1))
if client.highlight_amount != 0:
screen.blit(green_key, green_key.get_rect(center=(WIDTH / 2, HEIGHT / 2)))
key.set_alpha(255 - int(client.highlight_amount * 255))
screen.blit(key, key.get_rect(center=(WIDTH / 2, HEIGHT / 2)))
else:
if client.movement_finished & colored:
screen.blit(client.unique_keys[client.id], client.unique_keys[client.id].get_rect(center=(WIDTH / 2, HEIGHT / 2)))
else:
screen.blit(key, key.get_rect(center=(WIDTH / 2, HEIGHT / 2)))
pgwindow.position = [int(pos) for pos in client.position]
if on_windows and not client.movement_finished:
try:
if randint(1, 140) == 4:
shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys('%')
win32gui.SetForegroundWindow(pygame.display.get_wm_info()["window"])
except Exception:
pass
pygame.display.flip()
clock.tick(FRAMERATE)
# after clicked
if client.clicked:
if client.success:
if on_windows:
alert("You win")
else:
mb.showinfo(title="", message="You win")
else:
if on_windows:
if sfx:
start_new_thread(winsound.PlaySound, ("SystemExclamation", winsound.SND_ALIAS))
alert("Wrong guess")
else:
if sfx:
pygame.mixer.music.load("error.mp3")
pygame.mixer.music.play()
mb.showerror(title="", message="Wrong guess")
pygame.quit()