You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.2 KiB
Python
51 lines
1.2 KiB
Python
5 years ago
|
import atexit
|
||
|
import time
|
||
|
import threading
|
||
|
import queue
|
||
|
|
||
|
from .gameloop import GameLoop
|
||
|
|
||
|
|
||
|
def tick(dt, first, second):
|
||
|
print('%.2f' % dt, first, second)
|
||
|
pass
|
||
|
|
||
|
|
||
|
def worker(state, action_queue):
|
||
|
state['counter'] = 0
|
||
|
state['actions'] = []
|
||
|
loop = GameLoop(tick=tick, fps=1, fixed_dt=True, args=(1, 2))
|
||
|
while True:
|
||
|
item = action_queue.get()
|
||
|
if item['action'] == 'STOP':
|
||
|
loop.stop()
|
||
|
if item['action'] == 'START':
|
||
|
loop.start()
|
||
|
state['counter'] += 1
|
||
|
state['actions'].append(item['action'])
|
||
|
|
||
|
|
||
|
class EbermergenGame:
|
||
|
"""Runs a single game instance
|
||
|
|
||
|
Holds the serialized state.
|
||
|
Provides methods to perform actions.
|
||
|
"""
|
||
|
|
||
|
def __init__(self):
|
||
|
self.state = {}
|
||
|
self.actions = queue.Queue()
|
||
|
self.thread = threading.Thread(
|
||
|
target=worker, args=(self.state, self.actions))
|
||
|
|
||
|
def start_game(self):
|
||
|
self.thread.start()
|
||
|
atexit.register(self.interupt)
|
||
|
|
||
|
def interupt(self):
|
||
|
# handle gracefull shutdown
|
||
|
pass
|
||
|
|
||
|
def perform_action(self, action, payload={}):
|
||
|
self.actions.put({'action': action, 'payload': payload})
|