-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
135 lines (96 loc) · 3.13 KB
/
client.py
File metadata and controls
135 lines (96 loc) · 3.13 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
128
129
130
131
132
133
134
135
import system.network as network
from system.processes import set_appid, redirect_except
import logging
import asyncio
import sys, os
import socket
import json
import time
import win32event
import win32api
import winerror
import warnings
warnings.simplefilter("always")
import psutil
def is_local_address(ip_to_check):
interfaces = psutil.net_if_addrs()
for interface_name, interface_addresses in interfaces.items():
for address in interface_addresses:
if address.address == ip_to_check:
return True
return False
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
import pygame
MAX_EVENTS_PER_TICK = 50
LOG_FILE = r'log\client_log.txt'
CONFIG_FILE = r'config\client_config.json'
APP_CONFIG = r'config\app_config.json'
with open(APP_CONFIG, "r") as f:
app_config = json.load(f)
APP_ID = fr'PythonLive.{app_config["app_name"]}.Client.{app_config["app_version"]}'
logging.basicConfig(
filename=LOG_FILE,
filemode="a",
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
redirect_except()
set_appid(APP_ID)
mutex = win32event.CreateMutex(None, False, APP_ID)
last_error = win32api.GetLastError()
has_another_client = last_error == winerror.ERROR_ALREADY_EXISTS
pygame.init()
with open(CONFIG_FILE, "r") as f:
client_config = json.load(f)
is_device_server = is_local_address(client_config["host"])
is_device_instance = not (has_another_client or is_device_server) # Useful for overlapping sounds
pygame.display.set_caption(app_config['window_title'])
pygame.display.set_icon(pygame.image.load(app_config['window_icon']))
screen = pygame.display.set_mode(tuple(app_config['screen_dimensions'].values()))
fill_color = app_config["fill_color"]
def quit():
pygame.event.post(pygame.event.Event(pygame.QUIT))
async def main():
client = network.Client(
ws_port=client_config["ws_port"]
)
await client.connect(
host=client_config["host"],
port=client_config["port"],
family=socket.AF_INET
)
running = True
delta_time = 1 / 60
accumulator = 0.0
last_time = time.perf_counter()
frame_counter = 0
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
await client.disconnect()
running = False
# Handle pygame events
for _ in range(MAX_EVENTS_PER_TICK):
try:
event = client.event_queue.get_nowait()
except asyncio.QueueEmpty:
break
# Handle network events
now = time.perf_counter()
frame_time = min(now - last_time, 0.25)
last_time = now
accumulator += frame_time
while accumulator >= delta_time:
# Update your game state using delta_time
accumulator -= delta_time
screen.fill(fill_color)
alpha = accumulator / delta_time
# Render the game state
pygame.display.flip()
await asyncio.sleep(0)
frame_counter += 1
if __name__ == "__main__":
asyncio.run(main())
logging.info("Client closed")
pygame.quit()
sys.exit()