SIP4 python bindings for TQt
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

tut13.py 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. #!/usr/bin/env python
  2. # TQt tutorial 13.
  3. import sys
  4. import math
  5. import random
  6. from python_tqt import qt
  7. class LCDRange(qt.TQWidget):
  8. def __init__(self, s=None, parent=None, name=None):
  9. qt.TQWidget.__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. l = qt.TQVBoxLayout(self)
  20. l.addWidget(lcd, 1)
  21. l.addWidget(self.slider)
  22. l.addWidget(self.label)
  23. if s is not None:
  24. self.setText(s)
  25. def value(self):
  26. return self.slider.value()
  27. def setValue(self, value):
  28. self.slider.setValue(value)
  29. def setRange(self, minVal, maxVal):
  30. if minVal < 0 or maxVal > 99 or minVal > maxVal:
  31. raise ValueError, "LCDRange.setRange(): invalid range"
  32. self.slider.setRange(minVal, maxVal)
  33. def text(self):
  34. return self.label.text()
  35. def setText(self, s):
  36. self.label.setText(s)
  37. class CannonField(qt.TQWidget):
  38. def __init__(self, parent=None, name=None):
  39. qt.TQWidget.__init__(self, parent, name)
  40. self.ang = 45
  41. self.f = 0
  42. self.timerCount = 0
  43. self.autoShootTimer = qt.TQTimer(self, "movement handler")
  44. self.connect(self.autoShootTimer, qt.SIGNAL("timeout()"), self.moveShot)
  45. self.shoot_ang = 0
  46. self.shoot_f = 0
  47. self.target = qt.TQPoint(0, 0)
  48. self.gameEnded = 0
  49. self.setPalette(qt.TQPalette(qt.TQColor(250, 250, 200)))
  50. self.barrelRect = qt.TQRect(33, -4, 15, 8)
  51. self.newTarget()
  52. def angle(self):
  53. return self.ang
  54. def setAngle(self, degrees):
  55. if degrees < 5:
  56. degrees = 5
  57. if degrees > 70:
  58. degrees = 70
  59. if self.ang == degrees:
  60. return
  61. self.ang = degrees
  62. self.repaint(self.cannonRect(), 0)
  63. self.emit(qt.PYSIGNAL("angleChanged(int)"), (self.ang, ))
  64. def force(self):
  65. return self.f
  66. def setForce(self, newton):
  67. if newton < 0:
  68. newton = 0
  69. if self.f == newton:
  70. return
  71. self.f = newton
  72. self.emit(qt.PYSIGNAL("forceChanged(int)"), (self.f, ))
  73. def shoot(self):
  74. if self.isShooting():
  75. return
  76. self.timerCount = 0
  77. self.shoot_ang = self.ang
  78. self.shoot_f = self.f
  79. self.autoShootTimer.start(50)
  80. self.emit(qt.PYSIGNAL("canShoot(bool)"), (0, ))
  81. def newTarget(self):
  82. r = qt.TQRegion(self.targetRect())
  83. self.target = qt.TQPoint(random.randint(200, 390), random.randint(10, 265))
  84. self.repaint(r.unite(qt.TQRegion(self.targetRect())))
  85. def gameOver(self):
  86. return self.gameEnded
  87. def setGameOver(self):
  88. if self.gameEnded:
  89. return
  90. if self.isShooting():
  91. self.autoShootTime.stop()
  92. self.gameEnded = 1
  93. self.repaint()
  94. def restartGame(self):
  95. if self.isShooting():
  96. self.autoShootTime.stop()
  97. self.gameEnded = 0
  98. self.repaint()
  99. self.emit(qt.PYSIGNAL("canShoot(bool)"), (1, ))
  100. def moveShot(self):
  101. r = qt.TQRegion(self.shotRect())
  102. self.timerCount = self.timerCount + 1
  103. shotR = self.shotRect()
  104. if shotR.intersects(self.targetRect()):
  105. self.autoShootTimer.stop()
  106. self.emit(qt.PYSIGNAL("hit()"), ())
  107. self.emit(qt.PYSIGNAL("canShoot(bool)"), (1, ))
  108. elif shotR.x() > self.width() or shotR.y() > self.height():
  109. self.autoShootTimer.stop()
  110. self.emit(qt.PYSIGNAL("missed()"), ())
  111. self.emit(qt.PYSIGNAL("canShoot(bool)"), (1, ))
  112. else:
  113. r = r.unite(qt.TQRegion(shotR))
  114. self.repaint(r)
  115. def paintEvent(self, ev):
  116. updateR = ev.rect()
  117. p = qt.TQPainter(self)
  118. if self.gameEnded:
  119. p.setPen(qt.TQt.black)
  120. p.setFont(qt.TQFont("Courier", 48, qt.TQFont.Bold))
  121. p.drawText(self.rect(), qt.TQt.AlignCenter, "Game Over")
  122. if updateR.intersects(self.cannonRect()):
  123. self.paintCannon(p)
  124. if self.isShooting() and updateR.intersects(self.shotRect()):
  125. self.paintShot(p)
  126. if not self.gameEnded and updateR.intersects(self.targetRect()):
  127. self.paintTarget(p)
  128. def paintShot(self, p):
  129. p.setBrush(qt.TQt.black)
  130. p.setPen(qt.TQt.NoPen)
  131. p.drawRect(self.shotRect())
  132. def paintTarget(self, p):
  133. p.setBrush(qt.TQt.red)
  134. p.setPen(qt.TQt.black)
  135. p.drawRect(self.targetRect())
  136. def paintCannon(self, p):
  137. cr = self.cannonRect()
  138. pix = qt.TQPixmap(cr.size())
  139. pix.fill(self, cr.topLeft())
  140. tmp = qt.TQPainter(pix)
  141. tmp.setBrush(qt.TQt.blue)
  142. tmp.setPen(qt.TQt.NoPen)
  143. tmp.translate(0, pix.height() - 1)
  144. tmp.drawPie(qt.TQRect(-35, -35, 70, 70), 0, 90 * 16)
  145. tmp.rotate(-self.ang)
  146. tmp.drawRect(self.barrelRect)
  147. tmp.end()
  148. p.drawPixmap(cr.topLeft(), pix)
  149. def cannonRect(self):
  150. r = qt.TQRect(0, 0, 50, 50)
  151. r.moveBottomLeft(self.rect().bottomLeft())
  152. return r
  153. def shotRect(self):
  154. gravity = 4.0
  155. time = self.timerCount / 4.0
  156. velocity = self.shoot_f
  157. radians = self.shoot_ang * 3.14159265 / 180
  158. velx = velocity * math.cos(radians)
  159. vely = velocity * math.sin(radians)
  160. x0 = (self.barrelRect.right() + 5) * math.cos(radians)
  161. y0 = (self.barrelRect.right() + 5) * math.sin(radians)
  162. x = x0 + velx * time
  163. y = y0 + vely * time - 0.5 * gravity * time * time
  164. r = qt.TQRect(0, 0, 6, 6)
  165. r.moveCenter(qt.TQPoint(x, self.height() - 1 - y))
  166. return r
  167. def targetRect(self):
  168. r = qt.TQRect(0, 0, 20, 10)
  169. r.moveCenter(qt.TQPoint(self.target.x(), self.height() - 1 - self.target.y()))
  170. return r
  171. def isShooting(self):
  172. return self.autoShootTimer.isActive()
  173. def sizePolicy(self):
  174. return qt.TQSizePolicy(qt.TQSizePolicy.Expanding, qt.TQSizePolicy.Expanding)
  175. class GameBoard(qt.TQWidget):
  176. def __init__(self, parent=None, name=None):
  177. qt.TQWidget.__init__(self, parent, name)
  178. quit = qt.TQPushButton("&Quit", self, "quit")
  179. quit.setFont(qt.TQFont("Times", 18, qt.TQFont.Bold))
  180. self.connect(quit, qt.SIGNAL("clicked()"), qt.tqApp, qt.SLOT("quit()"))
  181. self.angle = LCDRange("ANGLE", self, "angle")
  182. self.angle.setRange(5, 70)
  183. self.force = LCDRange("FORCE", self, "force")
  184. self.force.setRange(10, 50)
  185. self.cannonField = CannonField(self, "cannonField")
  186. self.connect(self.angle, qt.PYSIGNAL("valueChanged(int)"), self.cannonField.setAngle)
  187. self.connect(self.cannonField, qt.PYSIGNAL("angleChanged(int)"), self.angle.setValue)
  188. self.connect(self.force, qt.PYSIGNAL("valueChanged(int)"), self.cannonField.setForce)
  189. self.connect(self.cannonField, qt.PYSIGNAL("forceChanged(int)"), self.force.setValue)
  190. self.connect(self.cannonField, qt.PYSIGNAL("hit()"), self.hit)
  191. self.connect(self.cannonField, qt.PYSIGNAL("missed()"), self.missed)
  192. self.shoot = qt.TQPushButton("&Shoot", self, "shoot")
  193. self.shoot.setFont(qt.TQFont("Times", 18, qt.TQFont.Bold))
  194. self.connect(self.shoot, qt.SIGNAL("clicked()"), self.fire)
  195. self.connect(self.cannonField, qt.PYSIGNAL("canShoot(bool)"), self.shoot, qt.SLOT("setEnabled(bool)"))
  196. restart = qt.TQPushButton("&New Game", self, "newgame")
  197. restart.setFont(qt.TQFont("Times", 18, qt.TQFont.Bold))
  198. self.connect(restart, qt.SIGNAL("clicked()"), self.newGame)
  199. self.hits = qt.TQLCDNumber(2, self, "hits")
  200. self.shotsLeft = qt.TQLCDNumber(2, self, "shotsleft")
  201. hitsL = qt.TQLabel("HITS", self, "hitsLabel")
  202. shotsLeftL = qt.TQLabel("SHOTS LEFT", self, "shotsleftLabel")
  203. grid = qt.TQGridLayout(self, 2, 2, 10)
  204. grid.addWidget(quit, 0, 0)
  205. grid.addWidget(self.cannonField, 1, 1)
  206. grid.setColStretch(1, 10)
  207. leftBox = qt.TQVBoxLayout()
  208. grid.addLayout(leftBox, 1, 0)
  209. leftBox.addWidget(self.angle)
  210. leftBox.addWidget(self.force)
  211. topBox = qt.TQHBoxLayout()
  212. grid.addLayout(topBox, 0, 1)
  213. topBox.addWidget(self.shoot)
  214. topBox.addWidget(self.hits)
  215. topBox.addWidget(hitsL)
  216. topBox.addWidget(self.shotsLeft)
  217. topBox.addWidget(shotsLeftL)
  218. topBox.addStretch(1)
  219. topBox.addWidget(restart)
  220. self.angle.setValue(60)
  221. self.force.setValue(25)
  222. self.angle.setFocus()
  223. self.newGame()
  224. def fire(self):
  225. if self.cannonField.gameOver() or self.cannonField.isShooting():
  226. return
  227. self.shotsLeft.display(self.shotsLeft.intValue() - 1)
  228. self.cannonField.shoot()
  229. def hit(self):
  230. self.hits.display(self.hits.intValue() + 1)
  231. if self.shotsLeft.intValue() == 0:
  232. self.cannonField.setGameOver()
  233. else:
  234. self.cannonField.newTarget()
  235. def missed(self):
  236. if self.shotsLeft.intValue() == 0:
  237. self.cannonField.setGameOver()
  238. def newGame(self):
  239. self.shotsLeft.display(15)
  240. self.hits.display(0)
  241. self.cannonField.restartGame()
  242. self.cannonField.newTarget()
  243. qt.TQApplication.setColorSpec(qt.TQApplication.CustomColor)
  244. a = qt.TQApplication(sys.argv)
  245. gb = GameBoard()
  246. gb.setGeometry(100, 100, 500, 355)
  247. a.setMainWidget(gb)
  248. gb.show()
  249. sys.exit(a.exec_loop())