Async base

This commit is contained in:
Suwako Moriya 2020-05-30 19:19:14 +02:00
parent 14a43f8214
commit f82d146b32
Signed by: SuwakoMmh
GPG Key ID: A27482B806F13CD5
12 changed files with 73 additions and 4 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
__pycache__/

View File

@ -4,7 +4,6 @@ url = "https://pypi.org/simple"
verify_ssl = true verify_ssl = true
[dev-packages] [dev-packages]
spyder = "*"
[packages] [packages]
pygame = "*" pygame = "*"

2
gui/__init__.py Normal file
View File

@ -0,0 +1,2 @@
from . import main
from . import modules

43
gui/main.py Normal file
View File

@ -0,0 +1,43 @@
import asyncio
import time
import sys
import pygame
from . import modules
class GUI:
def __init__(self, loop, size=(600,600)):
self.loop = loop
self.events = asyncio.Queue()
self.size = self.width, self.height = size
self.screen = None
self.modules = [module.MainClass(self) for module in modules.imports]
async def start(self):
pygame.init()
self.screen = pygame.display.set_mode(self.size, flags=pygame.RESIZABLE)
self.loop.run_in_executor(None, self.blocking_event_loop)
def blocking_event_loop(self):
while True:
event = pygame.event.wait()
self.dispatch("pygame_" + pygame.event.event_name(event.type).lower(), event)
def dispatch(self, event_type, *args, **kwargs):
print(event_type)
for module in self.modules:
asyncio.run_coroutine_threadsafe(module.dispatch_event(event_type, *args, **kwargs), loop=self.loop)
def stop(self):
pass
if True:
loop = asyncio.get_event_loop()
gui = GUI(loop)
asyncio.ensure_future(gui.start())
try:
loop.run_forever()
except KeyboardInterrupt:
pass
finally:
gui.stop()

2
gui/modules/__init__.py Normal file
View File

@ -0,0 +1,2 @@
from . import base
imports = [base]

View File

@ -0,0 +1,21 @@
class BaseClass:
def __init__(self, gui):
self.gui = gui
self.drawing = True
async def draw(self):
pass
async def dispatch_event(self, event_type, *args, **kwargs):
try:
coro = self.__getattribute__("on_" + event_type)
except AttributeError:
pass
else:
await coro(*args, **kwargs)
class MainClass(BaseClass):
async def on_pygame_keydown(self, event):
print("prout")

View File

View File

@ -6,7 +6,5 @@ Created on Thu May 28 19:45:33 2020
@author: suwako @author: suwako
""" """
import gui
import lifelib
import pygame

3
networking/__init__.py Normal file
View File

@ -0,0 +1,3 @@
import p2p
import server
import client

0
networking/client.py Normal file
View File

0
networking/p2p.py Normal file
View File

0
networking/server.py Normal file
View File