由於mush和zmud本身並不能直接使用python
但神人前輩總有妙法,繞道而行
雖然接入python會有些曲折,但為了強大的python,值得多走兩步
而且python高效簡潔,易懂,熱門流行,不是只能玩玩mud,也是值得一學
---------------------------------------------------------------------------------------------
由於python2已經停止更新支援了,
以下都是以python3在windows為例
---------------------------------------------------------------------------------------------
mushclient 篇
先安裝好mushclient,
到上面下載最新的python3.7 32bit 版本 (Windows x86 executable installer)
ps. 3.8太新,test時暫時無法跟mush對接成功
安裝時記得選add python to PATH
安裝位置要記下,或自定義到c:\
到上面下載最新的pywin32 python3.7 32bit 版本(eg. pywin32-228.win32-py3.7.exe)
安裝完成後,
按windows menu, 搜cmd, 右點, 以系統管理員身份執行
輸入python C:\Python37-32\Lib\site-packages\win32comext\axscript\client\pyscript.py
成功的話會出現 Registered: Python
打開mushclient,開好要連的mud world,
按shift+ctrl+6,打開scripting 介面,language選python
script file按new建一個新的
OK
python就能用了,
在mush trigger的send script內直接打python的code
或者在script欄輸入要用的function name就可以直接用到
建議使用後者,可以避免mush+python擦出的火花(潛在bug,越用越佔ram)
另在command欄也可以用前綴"/" + python的指令直接用
還有就是可以直接用mush內建lua的function, 要加"world."
eg. /world.note('hi')
test成功的參考環境:
windows 7 + python3.7.8 + pywin32-228 + mushclient 5.01
---------------------------------------------------------------------------------------------
zmud 篇
zmud要通過一個叫dde的server來跟python對接
安裝好zmud, python
新建一個空txt, 改名為dde.py
copy以下示範code到dde.py
這展示的是一個簡單的叫telltime的function, 告訴某人現在的時間
# -*- coding:utf-8 -*-
from pywin.mfc import object
import win32ui
import dde
import time
from datetime import datetime
class muddde(object.Object):
def __init__(self, topicName):
topic = dde.CreateTopic(topicName)
topic.AddItem(dde.CreateStringItem(""))
object.Object.__init__(self, topic)
class call_func(muddde):
def Request(self,input):
now=datetime.now().strftime("%Y-%m-%d.%H:%M:%S")
tozmud('tell %s %s' % (input,now))
server = dde.CreateServer()
server.AddTopic(call_func("telltime"))
server.Create('python')
conversation = dde.CreateConversation(server)
conversation.ConnectTo("ZMUD", "ZMUD")
print("dde server start")
def tozmud(cmds):
conversation.Exec(cmds)
while True:
win32ui.PumpWaitingMessages(0, -1)
time.sleep(0.01)
先打開zmud,
按windows menu, 搜cmd, 右點, 以系統管理員身份執行
輸入python c:\dde.py
dde.py的位置自己按所在改
成功的話會見到 dde server start
然後在zmud輸入%dde( 'python', 'telltime','someone')
輸出結果是tell someone 2020-08-15.19:32:05
對接成功!
更多應用就自己研究吧~
test成功的參考環境:
windows 7 + python3.7.8 + zmud7.21
---------------------------------------------------------------------------------------------
入門就到此為止,祝大家happy coding~