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.

tut14.py 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. #!/usr/bin/env python
  2. # TQt tutorial 14.
  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.barrelPressed = 0
  50. self.setPalette(qt.TQPalette(qt.TQColor(250, 250, 200)))
  51. self.barrelRect = qt.TQRect(33, -4, 15, 8)
  52. self.newTarget()
  53. def angle(self):
  54. return self.ang
  55. def setAngle(self, degrees):
  56. if degrees < 5:
  57. degrees = 5
  58. if degrees > 70:
  59. degrees = 70
  60. if self.ang == degrees:
  61. return
  62. self.ang = degrees
  63. self.repaint(self.cannonRect(), 0)
  64. self.emit(qt.PYSIGNAL("angleChanged(int)"), (self.ang, ))
  65. def force(self):
  66. return self.f
  67. def setForce(self, newton):
  68. if newton < 0:
  69. newton = 0
  70. if self.f == newton:
  71. return
  72. self.f = newton
  73. self.emit(qt.PYSIGNAL("forceChanged(int)"), (self.f, ))
  74. def shoot(self):
  75. if self.isShooting():
  76. return
  77. self.timerCount = 0
  78. self.shoot_ang = self.ang
  79. self.shoot_f = self.f
  80. self.autoShootTimer.start(50)
  81. self.emit(qt.PYSIGNAL("canShoot(bool)"), (0, ))
  82. def newTarget(self):
  83. r = qt.TQRegion(self.targetRect())
  84. self.target = qt.TQPoint(random.randint(200, 390), random.randint(10, 265))
  85. self.repaint(r.unite(qt.TQRegion(self.targetRect())))
  86. def gameOver(self):
  87. return self.gameEnded
  88. def setGameOver(self):
  89. if self.gameEnded:
  90. return
  91. if self.isShooting():
  92. self.autoShootTime.stop()
  93. self.gameEnded = 1
  94. self.repaint()
  95. def restartGame(self):
  96. if self.isShooting():
  97. self.autoShootTime.stop()
  98. self.gameEnded = 0
  99. self.repaint()
  100. self.emit(qt.PYSIGNAL("canShoot(bool)"), (1, ))
  101. def moveShot(self):
  102. r = qt.TQRegion(self.shotRect())
  103. self.timerCount = self.timerCount + 1
  104. shotR = self.shotRect()
  105. if shotR.intersects(self.targetRect()):
  106. self.autoShootTimer.stop()
  107. self.emit(qt.PYSIGNAL("hit()"), ())
  108. self.emit(qt.PYSIGNAL("canShoot(bool)"), (1, ))
  109. elif shotR.x() > self.width() or shotR.y() > self.height() or shotR.intersects(self.barrierRect()):
  110. self.autoShootTimer.stop()
  111. self.emit(qt.PYSIGNAL("missed()"), ())
  112. self.emit(qt.PYSIGNAL("canShoot(bool)"), (1, ))
  113. else:
  114. r = r.unite(qt.TQRegion(shotR))
  115. self.repaint(r)
  116. def mousePressEvent(self, ev):
  117. if ev.button() != qt.TQt.LeftButton:
  118. return
  119. if self.barrelHit(ev.pos()):
  120. self.barrelPressed = 1
  121. def mouseMoveEvent(self, ev):
  122. if not self.barrelPressed:
  123. return
  124. pnt = ev.pos()
  125. if pnt.x() <= 0:
  126. pnt.setX(1)
  127. if pnt.y() >= self.height():
  128. pnt.setY(self.height() - 1)
  129. rad = math.atan(float(self.rect().bottom() - pnt.y()) / pnt.x())
  130. self.setAngle(int(round(rad * 180 / math.pi)))
  131. def mouseReleaseEvent(self, ev):
  132. if ev.button() == qt.TQt.LeftButton:
  133. self.barrelPressed = 0
  134. def paintEvent(self, ev):
  135. updateR = ev.rect()
  136. p = qt.TQPainter(self)
  137. if self.gameEnded:
  138. p.setPen(qt.TQt.black)
  139. p.setFont(qt.TQFont("Courier", 48, qt.TQFont.Bold))
  140. p.drawText(self.rect(), qt.TQt.AlignCenter, "Game Over")
  141. if updateR.intersects(self.cannonRect()):
  142. self.paintCannon(p)
  143. if updateR.intersects(self.barrierRect()):
  144. self.paintBarrier(p)
  145. if self.isShooting() and updateR.intersects(self.shotRect()):
  146. self.paintShot(p)
  147. if not self.gameEnded and updateR.intersects(self.targetRect()):
  148. self.paintTarget(p)
  149. def paintShot(self, p):
  150. p.setBrush(qt.TQt.black)
  151. p.setPen(qt.TQt.NoPen)
  152. p.drawRect(self.shotRect())
  153. def paintTarget(self, p):
  154. p.setBrush(qt.TQt.red)
  155. p.setPen(qt.TQt.black)
  156. p.drawRect(self.targetRect())
  157. def paintBarrier(self, p):
  158. p.setBrush(qt.TQt.yellow)
  159. p.setPen(qt.TQt.black)
  160. p.drawRect(self.barrierRect())
  161. def paintCannon(self, p):
  162. cr = self.cannonRect()
  163. pix = qt.TQPixmap(cr.size())
  164. pix.fill(self, cr.topLeft())
  165. tmp = qt.TQPainter(pix)
  166. tmp.setBrush(qt.TQt.blue)
  167. tmp.setPen(qt.TQt.NoPen)
  168. tmp.translate(0, pix.height() - 1)
  169. tmp.drawPie(qt.TQRect(-35, -35, 70, 70), 0, 90 * 16)
  170. tmp.rotate(-self.ang)
  171. tmp.drawRect(self.barrelRect)
  172. tmp.end()
  173. p.drawPixmap(cr.topLeft(), pix)
  174. def cannonRect(self):
  175. r = qt.TQRect(0, 0, 50, 50)
  176. r.moveBottomLeft(self.rect().bottomLeft())
  177. return r
  178. def shotRect(self):
  179. gravity = 4.0
  180. time = self.timerCount / 4.0
  181. velocity = self.shoot_f
  182. radians = self.shoot_ang * math.pi / 180
  183. velx = velocity * math.cos(radians)
  184. vely = velocity * math.sin(radians)
  185. x0 = (self.barrelRect.right() + 5) * math.cos(radians)
  186. y0 = (self.barrelRect.right() + 5) * math.sin(radians)
  187. x = x0 + velx * time
  188. y = y0 + vely * time - 0.5 * gravity * time * time
  189. r = qt.TQRect(0, 0, 6, 6)
  190. r.moveCenter(qt.TQPoint(x, self.height() - 1 - y))
  191. return r
  192. def targetRect(self):
  193. r = qt.TQRect(0, 0, 20, 10)
  194. r.moveCenter(qt.TQPoint(self.target.x(), self.height() - 1 - self.target.y()))
  195. return r
  196. def barrierRect(self):
  197. return qt.TQRect(145, self.height() - 100, 15, 100)
  198. def barrelHit(self, p):
  199. mtx = qt.TQWMatrix()
  200. mtx.translate(0, self.height() - 1)
  201. mtx.rotate(-self.ang)
  202. (mtx, invertable) = mtx.invert()
  203. return self.barrelRect.contains(mtx.map(p))
  204. def isShooting(self):
  205. return self.autoShootTimer.isActive()
  206. def sizePolicy(self):
  207. return qt.TQSizePolicy(qt.TQSizePolicy.Expanding, qt.TQSizePolicy.Expanding)
  208. class GameBoard(qt.TQWidget):
  209. def __init__(self, parent=None, name=None):
  210. qt.TQWidget.__init__(self, parent, name)
  211. quit = qt.TQPushButton("&Quit", self, "quit")
  212. quit.setFont(qt.TQFont("Times", 18, qt.TQFont.Bold))
  213. self.connect(quit, qt.SIGNAL("clicked()"), qt.tqApp, qt.SLOT("quit()"))
  214. self.angle = LCDRange("ANGLE", self, "angle")
  215. self.angle.setRange(5, 70)
  216. self.force = LCDRange("FORCE", self, "force")
  217. self.force.setRange(10, 50)
  218. box = qt.TQVBox(self, "cannonFrame")
  219. box.setFrameStyle(qt.TQFrame.WinPanel | qt.TQFrame.Sunken)
  220. self.cannonField = CannonField(box, "cannonField")
  221. self.connect(self.angle, qt.PYSIGNAL("valueChanged(int)"), self.cannonField.setAngle)
  222. self.connect(self.cannonField, qt.PYSIGNAL("angleChanged(int)"), self.angle.setValue)
  223. self.connect(self.force, qt.PYSIGNAL("valueChanged(int)"), self.cannonField.setForce)
  224. self.connect(self.cannonField, qt.PYSIGNAL("forceChanged(int)"), self.force.setValue)
  225. self.connect(self.cannonField, qt.PYSIGNAL("hit()"), self.hit)
  226. self.connect(self.cannonField, qt.PYSIGNAL("missed()"), self.missed)
  227. self.shoot = qt.TQPushButton("&Shoot", self, "shoot")
  228. self.shoot.setFont(qt.TQFont("Times", 18, qt.TQFont.Bold))
  229. self.connect(self.shoot, qt.SIGNAL("clicked()"), self.fire)
  230. self.connect(self.cannonField, qt.PYSIGNAL("canShoot(bool)"), self.shoot, qt.SLOT("setEnabled(bool)"))
  231. restart = qt.TQPushButton("&New Game", self, "newgame")
  232. restart.setFont(qt.TQFont("Times", 18, qt.TQFont.Bold))
  233. self.connect(restart, qt.SIGNAL("clicked()"), self.newGame)
  234. self.hits = qt.TQLCDNumber(2, self, "hits")
  235. self.shotsLeft = qt.TQLCDNumber(2, self, "shotsleft")
  236. hitsL = qt.TQLabel("HITS", self, "hitsLabel")
  237. shotsLeftL = qt.TQLabel("SHOTS LEFT", self, "shotsleftLabel")
  238. accel = qt.TQAccel(self)
  239. accel.connectItem(accel.insertItem(qt.TQt.Key_Enter), self.fire)
  240. accel.connectItem(accel.insertItem(qt.TQt.Key_Return), self.fire)
  241. accel.connectItem(accel.insertItem(qt.TQt.CTRL + qt.TQt.Key_Q), qt.tqApp, qt.SLOT("quit()"))
  242. grid = qt.TQGridLayout(self, 2, 2, 10)
  243. grid.addWidget(quit, 0, 0)
  244. grid.addWidget(box, 1, 1)
  245. grid.setColStretch(1, 10)
  246. leftBox = qt.TQVBoxLayout()
  247. grid.addLayout(leftBox, 1, 0)
  248. leftBox.addWidget(self.angle)
  249. leftBox.addWidget(self.force)
  250. topBox = qt.TQHBoxLayout()
  251. grid.addLayout(topBox, 0, 1)
  252. topBox.addWidget(self.shoot)
  253. topBox.addWidget(self.hits)
  254. topBox.addWidget(hitsL)
  255. topBox.addWidget(self.shotsLeft)
  256. topBox.addWidget(shotsLeftL)
  257. topBox.addStretch(1)
  258. topBox.addWidget(restart)
  259. self.angle.setValue(60)
  260. self.force.setValue(25)
  261. self.angle.setFocus()
  262. self.newGame()
  263. def fire(self):
  264. if self.cannonField.gameOver() or self.cannonField.isShooting():
  265. return
  266. self.shotsLeft.display(self.shotsLeft.intValue() - 1)
  267. self.cannonField.shoot()
  268. def hit(self):
  269. self.hits.display(self.hits.intValue() + 1)
  270. if self.shotsLeft.intValue() == 0:
  271. self.cannonField.setGameOver()
  272. else:
  273. self.cannonField.newTarget()
  274. def missed(self):
  275. if self.shotsLeft.intValue() == 0:
  276. self.cannonField.setGameOver()
  277. def newGame(self):
  278. self.shotsLeft.display(15)
  279. self.hits.display(0)
  280. self.cannonField.restartGame()
  281. self.cannonField.newTarget()
  282. qt.TQApplication.setColorSpec(qt.TQApplication.CustomColor)
  283. a = qt.TQApplication(sys.argv)
  284. gb = GameBoard()
  285. gb.setGeometry(100, 100, 500, 355)
  286. a.setMainWidget(gb)
  287. gb.show()
  288. sys.exit(a.exec_loop())