-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
146 lines (122 loc) · 5.33 KB
/
client.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
import os
import sys
import socket
import threading
from time import sleep
from json import loads, dumps
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "1"
import pygame
from pygame._sdl2 import Window
from utils import assets, preferences, config, ClientServer
SERVER_ADDRESS = ('localhost', 2527)
class Client:
def __init__(self, server_address, music_setting, sfx_setting):
self.id = -1
self.position = [0, -300]
self.highlight_amount = 0
self.alive = True
self.wants_to_quit = False
self.clicked = False
self.clickable = False
self.success = False
self.game_over = False
self.sfx = sfx_setting
self.music = music_setting
self.server_address = server_address
self.id_surface = pygame.Surface((0, 0))
self.listener_thread = threading.Thread(target=self.listening_thread)
self.listener_thread.start()
def listening_thread(self):
buffer = ""
assigned_client_id = False
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect(self.server_address)
s.sendall(dumps({"quit": False, "clicked": False}).encode('ascii') + b'\n')
while not self.game_over:
sleep(0.02)
try:
response = s.recv(1024).decode('ascii')
except ConnectionResetError:
self.game_over = True
self.alive = False
break
if not response:
break
buffer += response
while '\n' in buffer:
raw_message, buffer = buffer.split('\n', 1)
try:
msg = loads(raw_message)
except ValueError:
print("Received malformed JSON message")
continue
if msg.get("close"):
self.game_over = True
self.alive = False
break
self.id = msg.get("id", self.id)
self.position = msg.get("position", self.position)
self.alive = msg.get("alive", self.alive)
self.success = msg.get("success", self.success)
self.clickable = msg.get("clickable", self.clickable)
self.highlight_amount = min(1, max(self.highlight_amount + msg.get("highlight", 0) * 4 / 60, 0))
if not assigned_client_id and self.id == 0 and self.music:
assets.assets['music'].set_volume(config.config['audio']['musicVolume'])
assets.assets['music'].play()
assigned_client_id = True
s.sendall(dumps({"quit": self.wants_to_quit, "clicked": self.clicked}).encode('ascii') + b'\n')
if self.wants_to_quit or not self.alive or self.clicked:
self.game_over = True
@staticmethod
def game_loop():
global client
client = Client(SERVER_ADDRESS, config.config["audio"]["musicEnabled"], config.config["audio"]["sfxEnabled"])
pgwindow = Window.from_display_module()
if preferences.get('transparent', False):
hwnd = pygame.display.get_wm_info()['window']
ClientServer.set_window_transparency(hwnd, (33, 33, 33))
running = True
while running and client.alive:
for event in pygame.event.get():
if event.type == pygame.QUIT:
client.wants_to_quit = True
running = False
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1 and client.clickable:
client.wants_to_quit = True
client.clicked = True
if client.game_over:
running = False
screen.fill((33, 33, 33))
if preferences['doNotOption']:
dno0 = assets.assets['blueFace']
dno1 = assets.assets['greenFace']
else:
dno0 = assets.assets['redKey']
dno1 = assets.assets['greenKey']
image_rect = dno0.get_rect(center=(150 / 2, 150 / 2))
if client.highlight_amount != 0:
screen.blit(dno1, image_rect)
dno0.set_alpha(255 - int(client.highlight_amount * 255))
screen.blit(dno0, image_rect)
else:
screen.blit(dno0, image_rect)
if isinstance(client.position[0], list):
pgwindow.position = [int(pos) for pos in client.position[0]]
else:
pgwindow.position = [int(pos) for pos in client.position]
pygame.display.flip()
clock.tick(60)
pygame.quit()
sys.exit()
if __name__ == "__main__":
try:
pygame.init()
pygame.display.set_caption("Limbo!")
pygame.display.set_icon(assets.assets['logo'])
clock = pygame.time.Clock()
flags = pygame.NOFRAME if preferences["borderless"] else 0
screen = pygame.display.set_mode((150, 150), flags=flags | pygame.SRCALPHA)
Client.game_loop()
except KeyboardInterrupt:
pygame.quit()
sys.exit()