2 Star 1 Fork 1

Neo Anderson/rpicker

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
GameManager.py 2.20 KB
一键复制 编辑 原始数据 按行查看 历史
from Consts import *
from time import time, sleep
from traceback import extract_stack
from keyboard import add_hotkey
from threading import Thread
import pygetwindow
@Singleme
class GameManager(Thread):
def __init__(self, behaviors=None):
Thread.__init__(self)
self.cbs = []
self.fps = CLI_FPS
self.isRun = True
self.isRolling = True
self.startTime = time()
self.window = pygetwindow.getActiveWindow()
add_hotkey("q", self.destroy)
# add_hotkey("esc", self.destroy)
add_hotkey("space", self.toggle)
add_hotkey("r", self.resetRolling)
if behaviors:
for b in behaviors:
b.setHandler(self.onHandle)
self.cbs = behaviors
def __cb(self, *args, offset=0, name=None):
if name:
mname = name
else:
es = extract_stack()
mname = es[-2 - offset][2]
if mname:
for cb in self.cbs:
if hasattr(cb, mname):
cbf = getattr(cb, mname)
if callable(cbf):
cbf(*args)
def foo(self, bar='.zZ'):
self.__cb(bar)
def run(self):
lastTime = self.startTime
while self.isRun:
if self.isRolling:
self.__cb(name='update')
else:
if time() - lastTime > 1:
self.__cb(name='idle')
lastTime = time()
sleep(1 / self.fps)
def onHandle(self, what, msg=None):
if WHAT_TOGGLE_TOO_FAST == what:
self.isRolling = False
def add(self, behavior):
behavior.setHandler(self.onHandle)
self.cbs.append(behavior)
def clear(self):
self.cbs.clear()
def destroy(self):
if self.window and self.window.isActive and self.isRun:
self.__cb()
self.isRun = False
def toggle(self):
if self.window and self.window.isActive and self.isRun:
self.isRolling = not self.isRolling
self.__cb(self.isRolling)
def resetRolling(self):
if self.window and self.window.isActive:
self.isRolling = True
self.__cb()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/neo5simple/rpicker.git
[email protected]:neo5simple/rpicker.git
neo5simple
rpicker
rpicker
master

搜索帮助