Archive:Development/Tutorials/Qt4 Ruby Tutorial/Chapter 11 (zh TW)

From KDE TechBase
Revision as of 15:58, 14 January 2010 by Alisha (talk | contribs)

Template:I18n/Language Navigation Bar (zh TW)

Template:TutorialBrowser (zh TW)

Giving It a Shot

檔案:

概覽

在這個範例中,我們引入一個計時器來實現動畫的射擊。

一行一行的瀏覽

cannon.rb

CannonField 現在有射擊能力了。

include Math

我們含入(include)Math,因為我們需要 sin()cos() 函數。

@timerCount = 0

@autoShootTimer = Qt::Timer.new(self) connect(@autoShootTimer, SIGNAL('timeout()'),

        self, SLOT('moveShot()'))

@shootAngle = 0 @shootForce = 0

我們初始化新的私有變數,並且連接 Qt::Timer::timeout() 訊號到 moveShot() 槽。我們將在每次計時器超時(times out)時移動砲彈。

timerCount 會不斷追踪發射後經過的時間。shootAngle 是加農砲發射時的角度,而 shootForce 是加農砲發射時的力量。

def shoot()

 if @autoShootTimer.isActive()
   return
 end;
 @timerCount = 0
 @shootAngle = @currentAngle
 @shootForce = @currentForce
 @autoShootTimer.start(5)

end

除非有砲彈在空中,否則這個函式會發射砲彈。timerCount 重設為零。shootAngleshootForce 變數會被設定為當前加農砲的角度和力量。最後,我們啟動計時器。

def moveShot()

 region = Qt::Region.new(shotRect())
 @timerCount += 1
 shotR = shotRect()
 if shotR.x() > width() || shotR.y() > height()
   @autoShootTimer.stop()
 else
   region = region.unite(Qt::Region.new(shotR))
 end
 update(region)

end

moveShot() 是一個移動砲彈的槽,當Qt::Timer啟動後,每5毫秒被呼叫一次。

它的工作是計算新的位置、更新螢幕上的砲彈到新的位置,並且在必要時停止計時器。

首先,我們建立一個 Qt::Region 來保留舊的 shotRect()。A Qt::Region is capable of holding any sort of region, and we'll use it here to simplify the painting. shotRect() returns the rectangle where the shot is now. It is explained in detail later.

Then we increment the timerCount, which has the effect of moving the shot one step along its trajectory.

Next we fetch the new shot rectangle.

If the shot has moved beyond the right or bottom edge of the widget we stop the timer, or we add the new shotRect() to the Qt::Region.

Finally, we repaint the Qt::Region. This will send a single paint event for just the one or two rectangles that need updating.

def paintEvent(event)

 painter = Qt::Painter.new(self)
 paintCannon(painter)
 if @autoShootTimer.isActive()
   paintShot(painter)
 end
 painter.end()

end

The paint event function has been simplified since the previous chapter. Most of the logic has been moved to the new paintShot() and paintCannon() functions.

def paintShot(painter)

 painter.setPen(Qt::NoPen)
 painter.setBrush(Qt::Brush.new(Qt::black))
 painter.drawRect(shotRect())

end

This private function paints the shot by drawing a black filled rectangle.

We leave out the implementation of paintCannon(); it is the same as the Qt::Widget::paintEvent() reimplementation from the previous chapter.

def shotRect()

 gravity = 4.0
 time = @timerCount / 20.0
 velocity = @shootForce
 radians = @shootAngle * 3.14159265 / 180.0
 velx = velocity * cos(radians)
 vely = velocity * sin(radians)
 x0 = (@barrelRect.right() + 5.0) * cos(radians)
 y0 = (@barrelRect.right() + 5.0) * sin(radians)
 x = x0 + velx * time
 y = y0 + vely * time - 0.5 * gravity * time * time
 result = Qt::Rect.new(0, 0, 6, 6)
 result.moveCenter(Qt::Point.new(x.round, height() - 1 - y.round))
 return result

end

This private function calculates the center point of the shot and returns the enclosing rectangle of the shot. It uses the initial cannon force and angle in addition to timerCount, which increases as time passes.

The formula used is the standard Newtonian formula for frictionless movement in a gravity field. For simplicity, we've chosen to disregard any Einsteinian effects.

We calculate the center point in a coordinate system where y coordinates increase upward. After we have calculated the center point, we construct a Qt::Rect with size 6 x 6 and move its center point to the point calculated above. In the same operation we convert the point into the widget's coordinate system (see The Coordinate System).

t11.rb

The only addition is the Shoot button.

shoot = Qt::PushButton.new(tr('&Shoot')) shoot.setFont(Qt::Font.new('Times', 18, Qt::Font::Bold))

In the constructor we create and set up the Shoot button exactly like we did with the Quit button.

connect(shoot, SIGNAL('clicked()'), cannonField, SLOT('shoot()'))

Connects the clicked() signal of the Shoot button to the shoot() slot of the CannonField.

執行應用程式

加農砲可以射擊,但沒有東西會被射中。

練習

使砲彈變成一個填滿的圓。[提示:Qt::Painter::drawEllipse() 可能會有幫助。]

當砲彈在空中時,改變加農砲的顏色。