python模拟桌球运动_用python做了个桌球瞄准器

#coding=utf-8

# Alan(Chaofan Zhang)

# gagazcfan@gmail.com

# http://weibo.com/alanslab

# https://gitcafe.com/activities

# https://github.com/zcfan

importsys, math, win32gui, win32apifrom PyQt4 importQtGui, QtCorefrom PyQt4.QtCore importQtclassTrans(QtGui.QWidget):#status code

STATUS_STARTPOINT =0

STATUS_ENDPOINT= 1STATUS_ESC= 2BALL_SIZE= 25

def __init__(self):

super(Trans, self).__init__()

self.startpoint=()

self.endpoint=()#self.targetpoint =()

self.status =0

self.initUI()definitUI(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):

defmousePressEvent(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_())

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值