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.

tut12.py 7.2KB

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