SIP4 python bindings for TQt
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tut11.py 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #!/usr/bin/env python
  2. # TQt tutorial 11.
  3. import sys
  4. import math
  5. from python_tqt.qt import *
  6. class LCDRange(TQVBox):
  7. def __init__(self,parent=None,name=None):
  8. TQVBox.__init__(self,parent,name)
  9. lcd = TQLCDNumber(2,self,'lcd')
  10. self.slider = TQSlider(TQt.Horizontal,self,'slider')
  11. self.slider.setRange(0,99)
  12. self.slider.setValue(0)
  13. self.connect(self.slider,SIGNAL('valueChanged(int)'),lcd,SLOT('display(int)'))
  14. self.connect(self.slider,SIGNAL('valueChanged(int)'),self,PYSIGNAL('valueChanged(int)'))
  15. self.setFocusProxy(self.slider)
  16. def value(self):
  17. return self.slider.value()
  18. def setValue(self,value):
  19. self.slider.setValue(value)
  20. def setRange(self,minVal,maxVal):
  21. if minVal < 0 or maxVal > 99 or minVal > maxVal:
  22. raise ValueError, 'LCDRange.setRange(): invalid range'
  23. self.slider.setRange(minVal,maxVal)
  24. class CannonField(TQWidget):
  25. def __init__(self,parent=None,name=None):
  26. TQWidget.__init__(self,parent,name)
  27. self.ang = 45
  28. self.f = 0
  29. self.timerCount = 0
  30. self.autoShootTimer = TQTimer(self,'movement handler')
  31. self.connect(self.autoShootTimer,SIGNAL('timeout()'),self.moveShot)
  32. self.shoot_ang = 0
  33. self.shoot_f = 0
  34. self.setPalette(TQPalette(TQColor(250,250,200)))
  35. self.barrelRect = TQRect(33,-4,15,8)
  36. def angle(self):
  37. return self.ang
  38. def setAngle(self,degrees):
  39. if degrees < 5:
  40. degrees = 5
  41. if degrees > 70:
  42. degrees = 70
  43. if self.ang == degrees:
  44. return
  45. self.ang = degrees
  46. self.repaint(self.cannonRect(),0)
  47. self.emit(PYSIGNAL('angleChanged(int)'),(self.ang,))
  48. def force(self):
  49. return self.f
  50. def setForce(self,newton):
  51. if newton < 0:
  52. newton = 0
  53. if self.f == newton:
  54. return
  55. self.f = newton
  56. self.emit(PYSIGNAL('forceChanged(int)'),(self.f,))
  57. def shoot(self):
  58. if self.autoShootTimer.isActive():
  59. return
  60. self.timerCount = 0
  61. self.shoot_ang = self.ang
  62. self.shoot_f = self.f
  63. self.autoShootTimer.start(50)
  64. def moveShot(self):
  65. r = TQRegion(self.shotRect())
  66. self.timerCount = self.timerCount + 1
  67. shotR = self.shotRect()
  68. if shotR.x() > self.width() or shotR.y() > self.height():
  69. self.autoShootTimer.stop()
  70. else:
  71. r = r.unite(TQRegion(shotR))
  72. self.repaint(r)
  73. def paintEvent(self,ev):
  74. updateR = ev.rect()
  75. p = TQPainter(self)
  76. if updateR.intersects(self.cannonRect()):
  77. self.paintCannon(p)
  78. if self.autoShootTimer.isActive() and updateR.intersects(self.shotRect()):
  79. self.paintShot(p)
  80. def paintShot(self,p):
  81. p.setBrush(TQt.black)
  82. p.setPen(TQt.NoPen)
  83. p.drawRect(self.shotRect())
  84. def paintCannon(self,p):
  85. cr = self.cannonRect()
  86. pix = TQPixmap(cr.size())
  87. pix.fill(self,cr.topLeft())
  88. tmp = TQPainter(pix)
  89. tmp.setBrush(TQt.blue)
  90. tmp.setPen(TQt.NoPen)
  91. tmp.translate(0,pix.height() - 1)
  92. tmp.drawPie(TQRect(-35,-35,70,70),0,90 * 16)
  93. tmp.rotate(-self.ang)
  94. tmp.drawRect(self.barrelRect)
  95. tmp.end()
  96. p.drawPixmap(cr.topLeft(),pix)
  97. def cannonRect(self):
  98. r = TQRect(0,0,50,50)
  99. r.moveBottomLeft(self.rect().bottomLeft())
  100. return r
  101. def shotRect(self):
  102. gravity = 4.0
  103. time = self.timerCount / 4.0
  104. velocity = self.shoot_f
  105. radians = self.shoot_ang * 3.14159265 / 180
  106. velx = velocity * math.cos(radians)
  107. vely = velocity * math.sin(radians)
  108. x0 = (self.barrelRect.right() + 5) * math.cos(radians)
  109. y0 = (self.barrelRect.right() + 5) * math.sin(radians)
  110. x = x0 + velx * time
  111. y = y0 + vely * time - 0.5 * gravity * time * time
  112. r = TQRect(0,0,6,6)
  113. r.moveCenter(TQPoint(x,self.height() - 1 - y))
  114. return r
  115. def sizePolicy(self):
  116. return TQSizePolicy(TQSizePolicy.Expanding,TQSizePolicy.Expanding)
  117. class MyWidget(TQWidget):
  118. def __init__(self,parent=None,name=None):
  119. TQWidget.__init__(self,parent,name)
  120. quit = TQPushButton('&Quit',self,'quit')
  121. quit.setFont(TQFont('Times',18,TQFont.Bold))
  122. self.connect(quit,SIGNAL('clicked()'),tqApp,SLOT('quit()'))
  123. self.angle = LCDRange(self,'angle')
  124. self.angle.setRange(5,70)
  125. self.force = LCDRange(self,'force')
  126. self.force.setRange(10,50)
  127. self.cannonField = CannonField(self,'cannonField')
  128. self.connect(self.angle,PYSIGNAL('valueChanged(int)'),self.cannonField.setAngle)
  129. self.connect(self.cannonField,PYSIGNAL('angleChanged(int)'),self.angle.setValue)
  130. self.connect(self.force,PYSIGNAL('valueChanged(int)'),self.cannonField.setForce)
  131. self.connect(self.cannonField,PYSIGNAL('forceChanged(int)'),self.force.setValue)
  132. shoot = TQPushButton('&Shoot',self,'shoot')
  133. shoot.setFont(TQFont('Times',18,TQFont.Bold))
  134. self.connect(shoot,SIGNAL('clicked()'),self.cannonField.shoot)
  135. grid = TQGridLayout(self,2,2,10)
  136. grid.addWidget(quit,0,0)
  137. grid.addWidget(self.cannonField,1,1)
  138. grid.setColStretch(1,10)
  139. leftBox = TQVBoxLayout()
  140. grid.addLayout(leftBox,1,0)
  141. leftBox.addWidget(self.angle)
  142. leftBox.addWidget(self.force)
  143. topBox = TQHBoxLayout()
  144. grid.addLayout(topBox,0,1)
  145. topBox.addWidget(shoot)
  146. topBox.addStretch(1)
  147. self.angle.setValue(60)
  148. self.force.setValue(25)
  149. self.angle.setFocus()
  150. TQApplication.setColorSpec(TQApplication.CustomColor)
  151. a = TQApplication(sys.argv)
  152. w = MyWidget()
  153. w.setGeometry(100,100,500,355)
  154. a.setMainWidget(w)
  155. w.show()
  156. a.exec_loop()