forked from quasar098/limbos32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
134 lines (117 loc) · 4.53 KB
/
main.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
import socket
import pygame
# noinspection PyUnresolvedReferences,PyProtectedMember
from pygame._sdl2 import Window
from typing import Any
from _thread import start_new_thread
import winsound
from time import sleep
from json import loads, dumps, load
from pymsgbox import alert
class LimboKeysClient:
def __init__(self):
self.id = -1 # 0-7 assigned by server, -1 if unknown
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
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.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)
borderless = True
transparent = True
music = True
sfx = True
# ==============================
try:
with open("config.json") as f:
data: dict[str, Any] = load(f)
borderless = data.get("borderless", True)
transparent = data.get("transparent", True)
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 transparent:
import win32api
import win32con
import win32gui
# 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:
screen.blit(key, key.get_rect(center=(WIDTH / 2, HEIGHT / 2)))
# screen.blit(client.id_surface, (10, 10))
pgwindow.position = [int(pos) for pos in client.position]
pygame.display.flip()
clock.tick(FRAMERATE)
if client.clicked:
if client.success:
alert("You win")
else:
if sfx:
start_new_thread(winsound.PlaySound, ("SystemExclamation", winsound.SND_ALIAS))
alert("Wrong guess")
pygame.quit()