class CannonField
Public Class Methods
new(parent = nil)
click to toggle source
Calls superclass method
# File examples/tutorial/t10/cannon.rb, line 7 def initialize(parent = nil) super @ang = 45 @f = 0 setPalette(Qt::Palette.new(Qt::Color.new(250, 250, 200))) setAutoFillBackground(true) end
Public Instance Methods
angle()
click to toggle source
# File examples/tutorial/t13/cannon.rb, line 29 def angle() return @currentAngle end
angle=(degrees)
click to toggle source
# File examples/tutorial/t14/cannon.rb, line 41 def angle=(degrees) if degrees < 5 degrees = 5 elsif degrees > 70 degrees = 70 end if @currentAngle == degrees return end @currentAngle = degrees update(cannonRect()) emit angleChanged(@currentAngle) end
force()
click to toggle source
# File examples/tutorial/t13/cannon.rb, line 33 def force() return @currentForce end
force=(newton)
click to toggle source
# File examples/tutorial/t14/cannon.rb, line 55 def force=(newton) if newton < 0 newton = 0 end if @currentForce == newton return end @currentForce = newton emit forceChanged(@currentForce) end
gameOver()
click to toggle source
# File examples/tutorial/t13/cannon.rb, line 37 def gameOver() return @gameEnded end
isShooting()
click to toggle source
# File examples/tutorial/t13/cannon.rb, line 203 def isShooting() return @autoShootTimer.isActive() end
mouseMoveEvent(e)
click to toggle source
# File examples/tutorial/t14/cannon.rb, line 141 def mouseMoveEvent(e) if !@barrelPressed return end pnt = e.pos(); if pnt.x() <= 0 pnt.setX(1) end if pnt.y() >= height() pnt.setY(height() - 1) end rad = atan2((rect().bottom() - pnt.y()), pnt.x()) self.angle = (rad * 180 / 3.14159265).round end
mousePressEvent(e)
click to toggle source
# File examples/tutorial/t14/cannon.rb, line 132 def mousePressEvent(e) if e.button() != Qt::LeftButton return end if barrelHit(e.pos()) @barrelPressed = true end end
mouseReleaseEvent(e)
click to toggle source
# File examples/tutorial/t14/cannon.rb, line 156 def mouseReleaseEvent(e) if e.button() == Qt::LeftButton @barrelPressed = false end end
newTarget()
click to toggle source
# File examples/tutorial/t12/cannon.rb, line 62 def newTarget() if @@currentForceirst_time @@currentForceirst_time = false midnight = Qt::Time.new(0, 0, 0) srand(midnight.secsTo(Qt::Time.currentTime())) end r = Qt::Region.new(targetRect()) @target = Qt::Point.new(200 + rand(190), 10 + rand(255)) repaint(r.unite(Qt::Region.new(targetRect()))) end
paintEvent(e)
click to toggle source
# File examples/tutorial/t10/cannon.rb, line 40 def paintEvent(e) if !e.rect().intersects(cannonRect()) return end cr = cannonRect() pix = Qt::Pixmap.new(cr.size()) pix.fill(self, cr.topLeft()) painter = Qt::Painter.new(pix) painter.setBrush(Qt::Brush.new(Qt::blue)) painter.setPen(Qt::NoPen) painter.translate(0, pix.height() - 1) painter.drawPie(Qt::Rect.new(-35, -35, 70, 70), 0, 90 * 16) painter.rotate(- @ang) painter.drawRect(Qt::Rect.new(33, -4, 15, 8)) painter.end() painter.begin(self) painter.drawPixmap(cr.topLeft(), pix) painter.end() end
restartGame()
click to toggle source
# File examples/tutorial/t13/cannon.rb, line 100 def restartGame() if isShooting() @autoShootTimer.stop() end @gameEnded = false update() emit canShoot(true) end
setAngle(degrees)
click to toggle source
# File examples/tutorial/t10/cannon.rb, line 15 def setAngle(degrees) if degrees < 5 degrees = 5 elsif degrees > 70 degrees = 70 end if @ang == degrees return end @ang = degrees repaint() emit angleChanged(@ang) end
setForce(newton)
click to toggle source
# File examples/tutorial/t10/cannon.rb, line 29 def setForce(newton) if newton < 0 newton = 0 end if @f == newton return end @f = newton emit forceChanged(@f) end
setGameOver()
click to toggle source
# File examples/tutorial/t13/cannon.rb, line 89 def setGameOver() if @gameEnded return end if isShooting() @autoShootTimer.stop() end @gameEnded = true update() end
shoot()
click to toggle source
# File examples/tutorial/t11/cannon.rb, line 48 def shoot() if @autoShootTimer.isActive() return end; @timerCount = 0 @shootAngle = @currentAngle @shootForce = @currentForce @autoShootTimer.start(5) end
shooting?()
click to toggle source
# File examples/tutorial/t14/cannon.rb, line 259 def shooting? return @autoShootTimer.active? end
sizePolicy()
click to toggle source
# File examples/tutorial/t10/cannon.rb, line 69 def sizePolicy() return Qt::SizePolicy.new(Qt::SizePolicy::Expanding, Qt::SizePolicy::Expanding) end