用python做了个桌球瞄准器
记得上次记录了个pyqt4做透明界面的代码,就是为了这个瞄准器,不过这几天一直没干正经事。。今天下午一鼓作气到现在,总算是搞定了,然后。。上次那个透明的代码弱爆了,不知道是哪些神坑欺负我们这些qt的门外汉,搞那么多花样。。找了官方文档,一句话搞定。。(改正,两个透明代码功能不同,之前不了解。我错了。。。。)用到了pyqt4、pywin32这些个库,废话不说,上代码先
#coding=utf-8
# Alan(Chaofan Zhang)
# gagazcfan@gmail.com
# http://weibo.com/alanslab
# https://gitcafe.com/activities
# https://github.com/zcfan
import sys, math, win32gui, win32api
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import Qt
class Trans(QtGui.QWidget):
# status code
STATUS_STARTPOINT = 0
STATUS_ENDPOINT = 1
STATUS_ESC = 2
BALL_SIZE = 25
def __init__(self):
super(Trans, self).__init__()
self.startpoint = ()
self.endpoint = ()
#self.targetpoint =()
self.status = 0
self.initUI()
def initUI(self):
# make a unvisable fullscreen window
self.setWindowOpacity(0.01)
self.setWindowFlags(Qt.FramelessWindowHint)
desktop = QtGui.QApplication.desktop()
rect = desktop.availableGeometry()
self.setGeometry(rect)
# set the style of mouse cursor
mcursor = QtGui.QCursor(QtGui.QPixmap("./cursor.png"))
self.setCursor(mcursor)
# not using anymore
## def paintEvent(self, event):
## paint = QtGui.QPainter()
## paint.begin(self)
##
## if len(self.startpoint) == 2:
## self.printStartpoint(paint)
## if len(self.endpoint) == 2:
## self.printEndpoint(paint)
## self.printLine(paint)
## #self.printTargetpoint(paint)
##
## paint.end()
##
## def printStartpoint(self, paint):
## paint.setPen(Qt.red)
## paint.drawPoint(self.startpoint, self.startpoint)
##
## def printEndpoint(self, paint):
## paint.setPen(Qt.red)
## paint.drawPoint(self.endpoint, self.endpoint)
##
## def printLine(self, paint):
## pen = QtGui.QPen(Qt.green)
## paint.setPen(pen)
## paint.drawLine(self.startpoint, self.startpoint,
## self.endpoint, self.endpoint)
#def printTargetpoint(self, paint):
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
if self.status == self.STATUS_STARTPOINT:
self.status = self.STATUS_ENDPOINT
self.endpoint = ()
self.startpoint = (event.x(), event.y())
self.update()
elif self.status == self.STATUS_ENDPOINT:
self.status = self.STATUS_ESC
self.endpoint = (event.x(), event.y())
lenX = self.endpoint-self.startpoint
lenY = self.endpoint-self.startpoint
tmpX = int(round(self.BALL_SIZE * (lenX / math.sqrt(
math.pow(lenX, 2) + math.pow(lenY, 2)))))
tmpY = int(round(self.BALL_SIZE * (lenY / math.sqrt(
math.pow(lenX, 2) + math.pow(lenY, 2)))))
# move mouse cursor to the right position
win32api.SetCursorPos((win32gui.GetCursorPos() + tmpX,
win32gui.GetCursorPos() + tmpY))
self.targetpoint = (self.endpoint + tmpX,
self.endpoint + tmpY)
self.update()
elif self.status == self.STATUS_ESC:
self.close()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
trans = Trans()
trans.show()
sys.exit(app.exec_())
另存为simple.pyw,同目录下有张图片cursor.png
用法:
1、游戏中需要用到瞄准器的时候双击pyw文件运行,鼠标会变成圆环,你想把目标球(用白球击中的那个)打到哪个位置就点哪里。
2、然后再点目标球(要精确点),瞄准器会把你的光标移动到合适的位置,鼠标不要再动了。
3、再点一次左键关闭看不见的瞄准器窗口,注意到光标恢复原状。(这步没用。。本来有别的想法。。)
4、也许还需要再点一次左键让游戏窗口获取焦点,然后点左键击球吧。
似乎参数没调整太准,动作太大就容易跑偏。。真想靠这种玩意拿高分的还是掂量着点吧。。自个调整也成。。
页:
[1]