欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python3打造一款游戏辅助软件

程序员文章站 2022-04-25 20:28:07
...

许多连击游戏需要快速的按键,这次打造一款按键连击的软件,助玩家快速完成连击,效果图如下:
python3打造一款游戏辅助软件
废话不多说,直接写入代码,首先导入需要的模块:

from tkinter import *
from pynput import keyboard, mouse
import threading
import win32api
import win32con
import time
from configparser import ConfigParser

接着创建程序所需要的数据库,存放各按键对应的键值,数据库命名为数据.ini , 数据如下:

[键值]
jian = {
        'backspace':'8',
        'tab':'9',
        'enter':'13',
        'shift':'16',
        'ctrl':'17',
        'alt':'18',
        'break':'19',
        'caps lock':'20',
        'escape':'27',
        'page up':'33',
        'page down':'34',
        'end':'35',
        'home':'36',
        'left arrow':'37',
        'up arrow':'38',
        'right arrow':'39',
        'down arrow':'40',
        'insert':'45',
        'delete':'46',
        '0':'48',
        '1':'49',
        '2':'50',
        '3':'51',
        '4':'52',
        '5':'53',
        '6':'54',
        '7':'55',
        '8':'56',
        '9':'57',
        'a':'65',
        'b':'66',
        'c':'67',
        'd':'68',
        'e':'69',
        'f':'70',
        'g':'71',
        'h':'72',
        'i':'73',
        'j':'74',
        'k':'75',
        'l':'76',
        'm':'77',
        'n':'78',
        'o':'79',
        'p':'80',
        'q':'81',
        'r':'82',
        's':'83',
        't':'84',
        'u':'85',
        'v':'86',
        'w':'87',
        'x':'88',
        'y':'89',
        'z':'90',
        'left window key':'91',
        'right window key':'92',
        'select key':'93',
        'numpad 0':'96',
        'numpad 1':'97',
        'numpad 2':'98',
        'numpad 3':'99',
        'numpad 4':'100',
        'numpad 5':'101',
        'numpad 6':'102',
        'numpad 7':'103',
        'numpad 8':'104',
        'numpad 9':'105',
        'multiply':'106',
        'add':'107',
        'subtract':'109',
        'decimal point':'110',
        'divide':'111',
        'f1':'112',
        'f2':'113',
        'f3':'114',
        'f4':'115',
        'f5':'116',
        'f6':'117',
        'f7':'118',
        'f8':'119',
        'f9':'120',
        'f10':'121',
        'f11':'122',
        'f12':'123',
        'num lock':'144',
        'scroll lock':'145',
        'semi-colon':'186',
        'equal sign':'187',
        'comma':'188',
        'dash':'189',
        'period':'190',
        'forward slash':'191',
        'grave accent':'192',
        'open bracket':'219',
        'back slash':'220',
        'close braket':'221',
        'single quote':'222',

    }

然后编写程序的主函数:

def get_shi():
    def on_press(key):
        global tag
        tag = True
        target = ConfigParser()
        target.read('数据.ini', encoding='utf-8')
        pwd = target.get('键值', 'jian')
        jian_list = eval(pwd)
        # print(jian_list["tab"])

        a = str(key)
        b = a.replace("'", "")
        # print(b, type(b))
        # print(entry_11.get() , type(entry_11.get()))
        j = 0
        while tag:
            j += 1
            if b == entry_11.get():
                if CheckVar1.get() == 1 :
                    # print("OK")
                    win32api.keybd_event(int(jian_list[entry_1.get()]), 0, 0, 0)  # 按下按键
                    win32api.keybd_event(int(jian_list[entry_1.get()]), 0, win32con.KEYEVENTF_KEYUP, 0)  # 释放按键
                    time.sleep(float(entry_6.get()))
                if CheckVar2.get() == 2 :
                    win32api.keybd_event(int(jian_list[entry_2.get()]), 0, 0, 0)  # 按下按键
                    win32api.keybd_event(int(jian_list[entry_2.get()]), 0, win32con.KEYEVENTF_KEYUP, 0)  # 释放按键
                    time.sleep(float(entry_7.get()))
                if CheckVar3.get() == 3:
                    win32api.keybd_event(int(jian_list[entry_3.get()]), 0, 0, 0)  # 按下按键
                    win32api.keybd_event(int(jian_list[entry_3.get()]), 0, win32con.KEYEVENTF_KEYUP, 0)  # 释放按键
                    time.sleep(float(entry_8.get()))
                if CheckVar4.get() == 4 :
                    win32api.keybd_event(int(jian_list[entry_4.get()]), 0, 0, 0)  # 按下按键
                    win32api.keybd_event(int(jian_list[entry_4.get()]), 0, win32con.KEYEVENTF_KEYUP, 0)  # 释放按键
                    time.sleep(float(entry_9.get()))
                if CheckVar5.get() == 5 :
                    win32api.keybd_event(int(jian_list[entry_5.get()]), 0, 0, 0)  # 按下按键
                    win32api.keybd_event(int(jian_list[entry_5.get()]), 0, win32con.KEYEVENTF_KEYUP, 0)  # 释放按键
                    time.sleep(float(entry_10.get()))

            if str(j) == entry_13.get() :
                tag = False


    def on_release(key):
        # print('{0} released'.format(
        #     key))
        a = str(key)
        b = a.replace("'", "")
        # if key == keyboard.Key.backspace:
        if b == entry_12.get():
            # Stop listener
            return False

    keyboard_listener = keyboard.Listener(on_press=on_press, on_release=on_release)
    lst = [keyboard_listener]

    for t in lst:
        t.start()
    for t in lst:
        t.join()


def thread_it(func):
    '''将函数打包进线程'''
    # 创建
    t = threading.Thread(target=func)
    # 守护 !!!
    t.setDaemon(True)
    # 启动
    t.start()
    # 阻塞--卡死界面!
    # t.join()

最后编写设计好的GUI界面:

window = Tk()
# 设置标题
window.title('辅助软件')
# 窗口的位置和大小
window.geometry('550x400+460+150')
# 设置窗口是否可以变化长宽,默认可变
window.resizable(width=False, height=False)



# 设置文本信息框
lable1_1 = Label(window, text='按键连点', font=('微软雅黑', 30), fg='black')
lable1_1.place(x=200, y=10)

CheckVar1 = IntVar()
CheckVar2 = IntVar()
CheckVar3 = IntVar()
CheckVar4 = IntVar()
CheckVar5 = IntVar()

Checkbutton_1 = Checkbutton(window, text="有效", variable=CheckVar1, onvalue=1, offvalue=0, font=('微软雅黑', 15))
Checkbutton_1.place(x=100, y=80)

Checkbutton_2 = Checkbutton(window, text="有效", variable=CheckVar2, onvalue=2, offvalue=0, font=('微软雅黑', 15))
Checkbutton_2.place(x=100, y=120)

Checkbutton_3 = Checkbutton(window, text="有效", variable=CheckVar3, onvalue=3, offvalue=0, font=('微软雅黑', 15))
Checkbutton_3.place(x=100, y=160)

Checkbutton_4 = Checkbutton(window, text="有效", variable=CheckVar4, onvalue=4, offvalue=0, font=('微软雅黑', 15))
Checkbutton_4.place(x=100, y=200)

Checkbutton_5 = Checkbutton(window, text="有效", variable=CheckVar5, onvalue=5, offvalue=0, font=('微软雅黑', 15))
Checkbutton_5.place(x=100, y=240)

lable1_2 = Label(window, text='按键设置:', font=('微软雅黑', 15), fg='black')
lable1_2.place(x=190, y=85)

lable1_3 = Label(window, text='按键设置:', font=('微软雅黑', 15), fg='black')
lable1_3.place(x=190, y=125)

lable1_4 = Label(window, text='按键设置:', font=('微软雅黑', 15), fg='black')
lable1_4.place(x=190, y=165)

lable1_5 = Label(window, text='按键设置:', font=('微软雅黑', 15), fg='black')
lable1_5.place(x=190, y=205)

lable1_6 = Label(window, text='按键设置:', font=('微软雅黑', 15), fg='black')
lable1_6.place(x=190, y=245)

lable1_7 = Label(window, text='按键间隔:', font=('微软雅黑', 15), fg='black')
lable1_7.place(x=360, y=85)

lable1_8 = Label(window, text='按键间隔:', font=('微软雅黑', 15), fg='black')
lable1_8.place(x=360, y=125)

lable1_9 = Label(window, text='按键间隔:', font=('微软雅黑', 15), fg='black')
lable1_9.place(x=360, y=165)

lable1_10 = Label(window, text='按键间隔:', font=('微软雅黑', 15), fg='black')
lable1_10.place(x=360, y=205)

lable1_11 = Label(window, text='按键间隔:', font=('微软雅黑', 15), fg='black')
lable1_11.place(x=360, y=245)

lable1_12 = Label(window, text='键\n盘\n辅\n助', font=('微软雅黑', 30), fg='green')
lable1_12.place(x=40, y=70)

lable1_13 = Label(window, text='秒', font=('微软雅黑', 15), fg='black')
lable1_13.place(x=510, y=85)

lable1_14 = Label(window, text='秒', font=('微软雅黑', 15), fg='black')
lable1_14.place(x=510, y=125)

lable1_15 = Label(window, text='秒', font=('微软雅黑', 15), fg='black')
lable1_15.place(x=510, y=165)

lable1_16 = Label(window, text='秒', font=('微软雅黑', 15), fg='black')
lable1_16.place(x=510, y=205)

lable1_17 = Label(window, text='秒', font=('微软雅黑', 15), fg='black')
lable1_17.place(x=510, y=245)

lable1_18 = Label(window, text='开始热键:', font=('微软雅黑', 15), fg='black')
lable1_18.place(x=50, y=300)

lable1_19 = Label(window, text='结束热键:', font=('微软雅黑', 15), fg='black')
lable1_19.place(x=250, y=300)

lable1_20 = Label(window, text='连按次数:', font=('微软雅黑', 15), fg='black')
lable1_20.place(x=360, y=50)

lable1_21 = Label(window, text='次', font=('微软雅黑', 15), fg='black')
lable1_21.place(x=510, y=50)

entry_1 = Entry(window, font=('微软雅黑', 12), width=5)
entry_1.place(x=300, y=92)

entry_2 = Entry(window, font=('微软雅黑', 12), width=5)
entry_2.place(x=300, y=132)

entry_3 = Entry(window, font=('微软雅黑', 12), width=5)
entry_3.place(x=300, y=172)

entry_4 = Entry(window, font=('微软雅黑', 12), width=5)
entry_4.place(x=300, y=212)

entry_5 = Entry(window, font=('微软雅黑', 12), width=5)
entry_5.place(x=300, y=252)

entry_6 = Entry(window, font=('微软雅黑', 12), width=5)
entry_6.place(x=460, y=92)

entry_7 = Entry(window, font=('微软雅黑', 12), width=5)
entry_7.place(x=460, y=132)

entry_8 = Entry(window, font=('微软雅黑', 12), width=5)
entry_8.place(x=460, y=172)

entry_9 = Entry(window, font=('微软雅黑', 12), width=5)
entry_9.place(x=460, y=212)

entry_10 = Entry(window, font=('微软雅黑', 12), width=5)
entry_10.place(x=460, y=252)

entry_11 = Entry(window, font=('微软雅黑', 15), width=5)
entry_11.place(x=150, y=300)

entry_12 = Entry(window, font=('微软雅黑', 15), width=5)
entry_12.place(x=350, y=300)

entry_13 = Entry(window, font=('微软雅黑', 10), width=5)
entry_13.place(x=460, y=60)

button_1 = Button(window, text="退出", font=("隶书", 22), width=35, bg='green', activeforeground='gold',
                  activebackground='Snow', command=window.quit)
button_1.place(x=10, y=340)

button_2 = Button(window, text="保存设置", font=("隶书", 15), bg='green', activeforeground='gold',
                  activebackground='Snow', command=lambda: thread_it(get_shi))
button_2.place(x=440, y=300)

window.mainloop()

代码写到这里了,代码量不多,有疑问的读者可以在博文下方评论或私信博主,博主会在第一时间为各位解答,喜欢博主代码可以关注博主哦,我是活动的笑脸。