用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[0], self.startpoint[1]) ## ## def printEndpoint(self, paint): ## paint.setPen(Qt.red) ## paint.drawPoint(self.endpoint[0], self.endpoint[1]) ## ## def printLine(self, paint): ## pen = QtGui.QPen(Qt.green) ## paint.setPen(pen) ## paint.drawLine(self.startpoint[0], self.startpoint[1], ## self.endpoint[0], self.endpoint[1]) #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[0]-self.startpoint[0] lenY = self.endpoint[1]-self.startpoint[1] 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()[0] + tmpX, win32gui.GetCursorPos()[1] + tmpY)) self.targetpoint = (self.endpoint[0] + tmpX, self.endpoint[1] + 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、也許還需要再點一次左鍵讓游戲窗口獲取焦點,然后點左鍵擊球吧。

似乎參數沒調整太准,動作太大就容易跑偏。。真想靠這種玩意拿高分的還是掂量着點吧。。自個調整也成。。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM