Async base
This commit is contained in:
parent
14a43f8214
commit
f82d146b32
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
__pycache__/
|
1
Pipfile
1
Pipfile
@ -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
2
gui/__init__.py
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
from . import main
|
||||||
|
from . import modules
|
43
gui/main.py
Normal file
43
gui/main.py
Normal 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
2
gui/modules/__init__.py
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
from . import base
|
||||||
|
imports = [base]
|
21
gui/modules/base/__init__.py
Normal file
21
gui/modules/base/__init__.py
Normal 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")
|
0
gui/modules/main_menu/__init__.py
Normal file
0
gui/modules/main_menu/__init__.py
Normal file
4
main.py
4
main.py
@ -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
3
networking/__init__.py
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
import p2p
|
||||||
|
import server
|
||||||
|
import client
|
0
networking/client.py
Normal file
0
networking/client.py
Normal file
0
networking/p2p.py
Normal file
0
networking/p2p.py
Normal file
0
networking/server.py
Normal file
0
networking/server.py
Normal file
Loading…
Reference in New Issue
Block a user