语音控制游戏

语音合成

需要安装win32

import  win32com.client
sp=win32com.client.Dispatch("SAPI.SPVOICE") #打开语音播放的接口
for i  in range(3):
    sp.Speak("我是凤姐,我爱王毅,wangyi is lovely") #声音组建
else:
    sp.Speak("老娘喊了那么久,还不从了老娘")

语音识别框架

安装完win32直接运行会报错,需要再打一个文件(百度一下那个报错,我忘掉了~),我们把需要的内容写入mytext中

from win32com.client import constants
import os
import win32com.client
import pythoncom
class SpeechRecognition:
    def __init__(self, wordsToAdd):
        self.listener = win32com.client.Dispatch("SAPI.SpSharedRecognizer")
        self.context = self.listener.CreateRecoContext()
        self.grammar = self.context.CreateGrammar()
        self.grammar.DictationSetState(0)
        self.wordsRule = self.grammar.Rules.Add("wordsRule", constants.SRATopLevel + constants.SRADynamic, 0)
        self.wordsRule.Clear()
        [self.wordsRule.InitialState.AddWordTransition(None, word) for word in wordsToAdd]
        self.grammar.Rules.Commit()
        self.grammar.CmdSetRuleState("wordsRule", 1)
        self.grammar.Rules.Commit()
        self.eventHandler = ContextEvents(self.context)


class ContextEvents(win32com.client.getevents("SAPI.SpSharedRecoContext")):
    def OnRecognition(self, StreamNumber, StreamPosition, RecognitionType, Result):
        newResult = win32com.client.Dispatch(Result)
        print("他妈的你在说 ", newResult.PhraseInfo.GetText())
        mytext=newResult.PhraseInfo.GetText()#识别的词语
        if  mytext=="关机":
            os.system("shutdown -s -t  300")
        elif mytext=="取消关机":
            os.system("shutdown -a")
        elif mytext=="记事本":
            os.system("notepad")
        elif mytext=="画图板":
            os.system("mspaint")
        elif mytext=="写字板":
            os.system("write")
        elif mytext=="百度":
            os.system("chrome  http://www.baidu.com")

if __name__ == '__main__':
    os.system("chrome  http://www.baidu.com")
    wordsToAdd = ["关机", "取消关机", "记事本", "画图板","写字板","百度" ]
    speechReco = SpeechRecognition(wordsToAdd)
    while True:
        pythoncom.PumpWaitingMessages()

语音对话

from win32com.client import constants
import os
import win32com.client
import pythoncom

class SpeechRecognition:
    def __init__(self, wordsToAdd):
        self.listener = win32com.client.Dispatch("SAPI.SpSharedRecognizer")
        self.context = self.listener.CreateRecoContext()
        self.grammar = self.context.CreateGrammar()
        self.grammar.DictationSetState(0)
        self.wordsRule = self.grammar.Rules.Add("wordsRule", constants.SRATopLevel + constants.SRADynamic, 0)
        self.wordsRule.Clear()
        [self.wordsRule.InitialState.AddWordTransition(None, word) for word in wordsToAdd]
        self.grammar.Rules.Commit()
        self.grammar.CmdSetRuleState("wordsRule", 1)
        self.grammar.Rules.Commit()
        self.eventHandler = ContextEvents(self.context)


class ContextEvents(win32com.client.getevents("SAPI.SpSharedRecoContext")):
    def OnRecognition(self, StreamNumber, StreamPosition, RecognitionType, Result):
        newResult = win32com.client.Dispatch(Result)
        print("他妈的你在说 ", newResult.PhraseInfo.GetText())
        mytext=newResult.PhraseInfo.GetText()#识别的词语
        sp = win32com.client.Dispatch("SAPI.SPVOICE")  # 打开语音播放的接口
        if  mytext=="你是谁":
            sp.Speak("老娘是凤姐")
        elif mytext=="你好":
            sp.Speak("好你妹")
        elif mytext=="干哈":
            sp.Speak("干你")
        elif mytext=="你的老公":
            sp.Speak("我们家亲爱的小好好")
        elif mytext=="国庆快乐":
            sp.Speak("国庆快乐,就是辛苦了我们家小好好")
        elif mytext=="郝仁":
            sp.Speak("帅的惊动了党中央")
        else:
            sp.Speak("你说啥?")

if __name__ == '__main__':
    os.system("chrome  http://www.baidu.com")
    wordsToAdd = ["你是谁", "你好", "干哈", "你的老公","国庆快乐","王毅" ]
    speechReco = SpeechRecognition(wordsToAdd)
    while True:
        pythoncom.PumpWaitingMessages()

流星蝴蝶剑里的语音控制技能

其实什么都能将控制,只要知道键盘的需要一个按下,一个松开,就可以完成一次按键。

from win32com.client import constants
import os
import win32com.client
import pythoncom
import win32con #定义
import win32api


class SpeechRecognition:
    def __init__(self, wordsToAdd):
        self.listener = win32com.client.Dispatch("SAPI.SpSharedRecognizer")
        self.context = self.listener.CreateRecoContext()
        self.grammar = self.context.CreateGrammar()
        self.grammar.DictationSetState(0)
        self.wordsRule = self.grammar.Rules.Add("wordsRule", constants.SRATopLevel + constants.SRADynamic, 0)
        self.wordsRule.Clear()
        [self.wordsRule.InitialState.AddWordTransition(None, word) for word in wordsToAdd]
        self.grammar.Rules.Commit()
        self.grammar.CmdSetRuleState("wordsRule", 1)
        self.grammar.Rules.Commit()
        self.eventHandler = ContextEvents(self.context)


class ContextEvents(win32com.client.getevents("SAPI.SpSharedRecoContext")):
    def OnRecognition(self, StreamNumber, StreamPosition, RecognitionType, Result):
        newResult = win32com.client.Dispatch(Result)
        print("他妈的你在说 ", newResult.PhraseInfo.GetText())
        mytext=newResult.PhraseInfo.GetText()#识别的词语
        if  mytext=="趴下"  or mytext=="装死"  :
            win32api.keybd_event(89, 0, 0, 0)  # 按下
            win32api.keybd_event(89, 0, win32con.KEYEVENTF_KEYUP, 0)  # 松开
        elif mytext=="起飞"  or mytext=="跳跃"   or mytext=="起来" :
            win32api.keybd_event(32, 0, 0, 0)  # 按下
            win32api.keybd_event(32, 0, win32con.KEYEVENTF_KEYUP, 0)# 松开
        elif mytext == "狂风吹": #adw ml
            win32api.keybd_event(65, 0, 0, 0)  # 按下
            win32api.keybd_event(65, 0, win32con.KEYEVENTF_KEYUP, 0)
            win32api.keybd_event(68, 0, 0, 0)  # 按下
            win32api.keybd_event(68, 0, win32con.KEYEVENTF_KEYUP, 0)
            win32api.keybd_event(87, 0, 0, 0)  # 按下
            win32api.keybd_event(87, 0, win32con.KEYEVENTF_KEYUP, 0)
            win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
            win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
        elif mytext == "旋风刀":  # ads ml
            win32api.keybd_event(65, 0, 0, 0)  # 按下
            win32api.keybd_event(65, 0, win32con.KEYEVENTF_KEYUP, 0)
            win32api.keybd_event(68, 0, 0, 0)  # 按下
            win32api.keybd_event(68, 0, win32con.KEYEVENTF_KEYUP, 0)
            win32api.keybd_event(83, 0, 0, 0)  # 按下
            win32api.keybd_event(83, 0, win32con.KEYEVENTF_KEYUP, 0)
            win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
            win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
        elif mytext == "前进":  # ads ml
            win32api.keybd_event(65, 0, 0, 0)  # 按下
            win32api.keybd_event(65, 0, win32con.KEYEVENTF_KEYUP, 0)
        elif mytext == "后退":  # ads ml
            win32api.keybd_event(83, 0, 0, 0)  # 按下
            win32api.keybd_event(83, 0, win32con.KEYEVENTF_KEYUP, 0)

if __name__ == '__main__':
    #os.system("chrome  http://www.baidu.com")
    wordsToAdd = ["跳跃","起飞","装死","趴下","起来","狂风吹" ,"旋风刀","前进","后退"]
    speechReco = SpeechRecognition(wordsToAdd)
    while True:
        pythoncom.PumpWaitingMessages()

键盘

我们可以明显知道这是2次按键,91是win键。

import win32con #定义
import win32api
win32api.keybd_event(91,0,0,0) #按下
win32api.keybd_event(69,0,0,0) #按下
win32api.keybd_event(69,0,win32con.KEYEVENTF_KEYUP,0)#松开
win32api.keybd_event(91,0,win32con.KEYEVENTF_KEYUP,0)#松开
#修改91,按下win键盘

附一个表:
键盘键与虚拟键码对照表

字母和数字键 数字小键盘的键 功能键 其它键
      键   键码   键   键码    键   键码    键    键码
      A   65   0   96     F1   112   Backspace    8
      B   66   1   97     F2   113   Tab       9
      C   67    2   98     F3   114   Clear      12
      D   68   3   99     F4   115  Enter      13
      E   69    4   100    F5   116  Shift      16
      F   70    5   101    F6   117  Control     17
      G   71    6   102    F7   118   Alt       18
      H   72    7   103    F8   119  Caps Lock    20
      I   73    8   104    F9   120  Esc       27
      J   74    9   105    F10  121  Spacebar    32
      K   75    *   106    F11  122  Page Up     33
      L   76    +   107    F12  123  Page Down    34
      M   77    Enter 108    –   –   End       35
      N   78    -   109    –   –    Home      36
      O   79    .   110    –   –     Left Arrow   37
      P   80    /   111    –   –     Up Arrow    38
      Q   81    –   –     –   –     Right Arrow   39
      R   82    –   –    –   –      Down Arrow    40
      S   83    –   –     –   –     Insert      45
      T   84    –   –     –   –     Delete      46
      U   85    –   –     –   –     Help       47
      V   86    –   –    –   –     Num Lock     144
      W   87          
      X   88      
      Y   89      
      Z   90      
      0   48      
      1   49      
      2   50       
      3   51       
      4   52       
      5   53       
      6   54       
      7   55       
      8   56       
      9   57

‘’’

鼠标

鼠标先设定位置,一次按下鼠标一次松开鼠标算一次完成。代码中需要修改的部分是rightdown跟leftdown。

import win32con #定义
import win32api
import time

time.sleep(1)
win32api.SetCursorPos([30,30]) #设定鼠标位置
time.sleep(10)
#鼠标右键按下
win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN,0,0,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP,0,0,0,0)
#双击
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0,0,0)

你可能感兴趣的:(python,游戏程序)