generated from python-discord/code-jam-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
60 lines (48 loc) · 1.46 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
import asyncio
import sys
import threading
import time
import traceback
import pygame
from src.game import Game
from src.true_client import Player
async def main():
"""Main asyncio function to start connection"""
await player.estab_comms()
if __name__ == "__main__":
loop = asyncio.new_event_loop()
ws_thread = threading.Thread(target=loop.run_forever)
game = Game()
if len(sys.argv) > 1:
websocket_url = sys.argv[1]
else:
websocket_url = 'ws://localhost:8001' # Default to localhost
player = Player(websocket_url)
try:
ws_thread.start()
asyncio.run_coroutine_threadsafe(main(), loop)
print("Connecting...")
while player.pid is None:
time.sleep(0.25)
print("Starting client")
player.attach(game)
game.add_sprite(-1, player)
game.add_handler(player.on_event, pygame.KEYUP, pygame.KEYDOWN, pygame.MOUSEBUTTONDOWN)
game.start()
except KeyboardInterrupt:
player.running = False
game.running = False
loop.call_soon_threadsafe(loop.stop)
ws_thread.join(1)
if ws_thread.is_alive():
exit(2)
exit(0)
except Exception as _e: # noqa: F841
print("Out:", traceback.format_exc())
player.running = False
game.running = False
loop.call_soon_threadsafe(loop.stop)
ws_thread.join(1)
if ws_thread.is_alive():
exit(2)
exit(1)