众所周知,minecraft java版的pvp端一般都有按键显示功能,于是作者根据cm端的按键显示做了python版
![](https://img-blog.csdnimg.cn/img_convert/6182b07197e54925b0f1da8217a91808.png)
python按键显示,图为minecraft网页版
做这个程序的时候最先完成的是cps显示,下面是代码
from pynput.mouse import Listener
import time
import threading
rclicks = []
lclicks = []
def on_click(*args):
global rclicks
global lclicks
global lcps
global rcps
if args[-1]:
mousetype = args[-2].name
print(mousetype)
if mousetype == 'left':
lclicks.append(time.time())
if mousetype == 'right':
rclicks.append(time.time())
lcps = 0
rcps = 0
'''try:
while lcps <= len(lclicks):
lclicks.pop(0)
while rcps <= len(rclicks):
rclicks.pop(0)
except Exception:
pass'''
for info in lclicks:
if time.time()-info<=1:
lcps += 1
for infO in rclicks:
if time.time()-infO<=1:
rcps += 1
try:
if time.time()-lclicks[-1]>=1:
lclicks = []
print('qkl')
except Exception:
pass
try:
if time.time()-rclicks[-1]>=1:
rclicks = []
print('qkr')
except Exception:
pass
try:
l.config(text=lcps)
r.config(text=rcps)
except Exception as e:
print(e)
lcps = 0
rcps = 0
def show_cps():
with Listener(on_click=on_click) as listener:
listener.join()
Showcps = threading.Thread(target=show_cps)
Showcps.setDaemon(False)
Showcps.start()
def show_cp():
while True:
on_click('','','','')
Showcpss = threading.Thread(target=show_cp)
Showcpss.setDaemon(True)
Showcpss.start()
from tkinter import *
window = Tk()
window.title('showcps')
window.geometry('400x300')
window.resizable(0,0)
l = Label(window,text='0')
l.pack()
r = Label(window,text='0')
r.pack()
window.mainloop()
![](https://img-blog.csdnimg.cn/img_convert/12de9125a1786b71d2db7abf3e82e315.png)
简洁大气精美的gui
由于是第一版程序所以有部分代码冗余,整体思路是把左键和右键按下时的时间存到列表里,再遍历确定当前时间到1秒前按下按键的次数,通过一个多线程重复刷新并计算
接下来就是检测wasd、空格、右shift、鼠标左右键的松开和按下,松开就变灰,按下就变蓝,把左右键cps计算好显示出来
显示上述东西我用的是tkinter库,python3最棒的gui库
用画布(canvas控件)画出来显然比tk自带的控件漂亮许多(进阶操作:canvas的妙用)
window.wm_attributes('-transparentcolor','#114514')
这行代码可以让tkinter中任何你想把其变为透明的内容变透明,将tkinter的背景、canvas的背景均设为透明,再把tk窗口上方的状态栏隐藏(window.overrideredirect(True)),就能得到文章开头的效果
作者预留了一个设置的控件,连续按4次右shift即可调出窗口(玩过电子烟V4的都知道这键是干嘛的)(ps:如果你想要维普4.04破解版可以私信作者/b站:元气少女营长酱)
最后是源代码
from pynput import *
import time
import threading
rclicks = []
lclicks = []
shifts = []
def on_click(*args):
global rclicks
global lclicks
global lcps
global rcps
try:
if args[-2].name == 'left':
if args[-1] == True:
lclicks.append(time.time())
canvas.itemconfigure(r1,fill='blue')
if args[-1] == False:
canvas.itemconfigure(r1,fill='#acb6b9')
if args[-2].name == 'right':
if args[-1] == True:
rclicks.append(time.time())
canvas.itemconfigure(r2,fill='blue')
if args[-1] == False:
canvas.itemconfigure(r2,fill='#acb6b9')
lcps = 0
rcps = 0
except Exception:
pass
for info in lclicks:
if time.time()-info<=1:
lcps += 1
for infO in rclicks:
if time.time()-infO<=1:
rcps += 1
try:
if time.time()-lclicks[-1]>=1:
lclicks = []
except Exception:
pass
try:
if time.time()-rclicks[-1]>=1:
rclicks = []
except Exception:
pass
try:
canvas.itemconfigure(l,text=str(lcps)+'cps')
canvas.itemconfigure(r,text=str(rcps)+'cps')
except Exception:
pass
lcps = 0
rcps = 0
def key_on(key):
global shifts
try:
if str(key) == 'Key.space':
canvas.itemconfigure(r3,fill='blue')
if str(key) == 'Key.shift':
canvas.itemconfigure(r4,fill='blue')
if str(key) == "'w'" or str(key) == "'W'":
canvas.itemconfigure(r5,fill='blue')
if str(key) == "'a'" or str(key) == "'A'":
canvas.itemconfigure(r6,fill='blue')
if str(key) == "'s'" or str(key) == "'S'":
canvas.itemconfigure(r7,fill='blue')
if str(key) == "'d'" or str(key) == "'D'":
canvas.itemconfigure(r8,fill='blue')
if str(key) == 'Key.shift_r':
if len(shifts) < 3:
shifts.append(time.time())
else:
if time.time()-shifts[0] <=1 and time.time()-shifts[1] <=1 and time.time()-shifts[2]<=1:
setup = Toplevel()
setup.title('设置')
setup.geometry('250x150')
setup.resizable(0,0)
setup.attributes('-topmost','true')
Button(setup,text='全部退出',command=lambda:exit()).place(x=170,y=110)
shifts = []
else:
shifts = []
except Exception:
pass
def key_release(key):
try:
if str(key) == 'Key.space':
canvas.itemconfigure(r3,fill='#acb6b9')
if str(key) == 'Key.shift':
canvas.itemconfigure(r4,fill='#acb6b9')
if str(key) == "'w'" or str(key) == "'W'":
canvas.itemconfigure(r5,fill='#acb6b9')
if str(key) == "'a'" or str(key) == "'A'":
canvas.itemconfigure(r6,fill='#acb6b9')
if str(key) == "'s'" or str(key) == "'S'":
canvas.itemconfigure(r7,fill='#acb6b9')
if str(key) == "'d'" or str(key) == "'D'":
canvas.itemconfigure(r8,fill='#acb6b9')
except Exception:
pass
listener = keyboard.Listener(on_press=key_on,on_release=key_release)
listener.start()
listeneR = mouse.Listener(on_click=on_click)
listeneR.start()
def show_cps():
while True:
on_click(None,None,None,None)
Showcps = threading.Thread(target=show_cps)
Showcps.setDaemon(True)
Showcps.start()
from tkinter import *
window = Tk()
window.overrideredirect(True)
window.config(bg='#114514')
window.wm_attributes('-transparentcolor','#114514')
window.attributes('-topmost','true')
window.attributes('-alpha',0.90)
width = 210
height = 280
window.geometry('%dx%d'%(width,height))
canvas = Canvas(window,highlightthickness=0,bg='#114514')
canvas.place(width=width,height=height)
r1 = canvas.create_rectangle(10,140,102.5,210,fill='#acb6b9')
r2 = canvas.create_rectangle(107.5,140,200,210,fill='#acb6b9')
r3 = canvas.create_rectangle(10,215,200,240,fill='#acb6b9')
r4 = canvas.create_rectangle(10,245,200,270,fill='#acb6b9')
r5 = canvas.create_rectangle(75,10,135,70,fill='#acb6b9')
r6 = canvas.create_rectangle(10,75,70,135,fill='#acb6b9')
r7 = canvas.create_rectangle(75,75,135,135,fill='#acb6b9')
r8 = canvas.create_rectangle(140,75,200,135,fill='#acb6b9')
canvas.create_text(56.25,170,text='LMB',font=('微软雅黑',15))
canvas.create_text(153.75,170,text='RMB',font=('微软雅黑',15))
canvas.create_text(105,227.5,text='SPACE',font=('微软雅黑',10))
canvas.create_text(105,257.5,text='LSHIFT',font=('微软雅黑',10))
canvas.create_text(105,40,text='W',font=('微软雅黑',13))
canvas.create_text(40,105,text='A',font=('微软雅黑',13))
canvas.create_text(105,105,text='S',font=('微软雅黑',13))
canvas.create_text(170,105,text='D',font=('微软雅黑',13))
l = canvas.create_text(56.25,192,text='0cps',font=('微软雅黑',12))
r = canvas.create_text(153.75,192,text='0cps',font=('微软雅黑',12))
window.mainloop()