⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 t13.html

📁 qtopiaphone英文帮助,用于初学者和开发人员,初学者可以用来学习,开发人员可以用来资料查询.
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Qt Tutorial - Chapter 13: Game Over</title></head><body bgcolor="#ffffff"><p><table width="100%"><tr><td><a href="index.html"><img width="100" height="100" src="qtlogo.png"alt="Home" border="0"><img width="100"height="100" src="face.png" alt="Home" border="0"></a><td valign="top"><div align="right"><img src="dochead.png" width="472" height="27"><br><a href="classes.html"><b>Classes</b></a>- <a href="annotated.html">Annotated</a>- <a href="hierarchy.html">Tree</a>- <a href="functions.html">Functions</a>- <a href="index.html">Home</a>- <a href="topicals.html"><b>Structure</b>  <font face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular" align="center" size=32>Qte</font></a></div></table><p><h1 align=center>Chapter 13: Game Over</h1><br clear="all"><p><center><img src="t13.png" alt="Screenshot of tutorial thirteen"></center><p>In this example we start to approach a real playable game, with ascore.  We give MyWidget a new name (GameBoard) and add some slots.<p>We put the definition in gamebrd.h and the implementation in gamebrd.cpp.<p>The CannonField now has a game over state.<p>The layout problems in LCDRange are fixed.<p><ul><li><a href="t13-lcdrange-h.html">lcdrange.h</a> contains the LCDRangeclass definition<li><a href="t13-lcdrange-cpp.html">lcdrange.cpp</a> contains the LCDRangeimplementation<li><a href="t13-cannon-h.html">cannon.h</a> contains the CannonField classdefinition<li><a href="t13-cannon-cpp.html">cannon.cpp</a> contains the CannonFieldimplementation<li><a href="t13-gamebrd-h.html">gamebrd.h</a> contains the GameBoardclass definition<li><a href="t13-gamebrd-cpp.html">gamebrd.cpp</a> contains the GameBoardimplementation<li><a href="t13-main-cpp.html">main.cpp</a> contains MyWidget and main.<li><a href="t13-makefile.html">Makefile</a> contains some rules forgenerating the meta object information necessary for <ahref="signalsandslots.html">signal/slot creation.</a></ul><p><h2>Line by Line Walk-Through</h2><p><h3><a href="t13-lcdrange-h.html">lcdrange.h</a></h3>   <pre>    #include &lt;<a href="qwidget-h.html">qwidget.h</a>&gt;        class QSlider;    class QLabel;        class LCDRange : public QWidget</pre><p>We inherit QWidget rather than QVBox: QVBox is very easy to use, butagain it showed its limitations, so we switch to the more powerful andslightly harder to use QVBoxLayout.  (As you remember, QVBoxLayout isnot a widget, it manages one.)<p><h3><a href="t13-lcdrange-cpp.html">lcdrange.cpp</a></h3>   <pre>    #include &lt;<a href="qlayout-h.html">qlayout.h</a>&gt;</pre><p>We need to include qlayout.h now, to get the other layout managementAPI. <pre>    LCDRange::LCDRange( <a href="qwidget.html">QWidget</a> *parent, const char *name )            : <a href="qwidget.html">QWidget</a>( parent, name )</pre><p>We inherit QWidget in the usual way.<p>The other constructor has the same change.  init() is unchanged,except that we've added some lines at the end:  <pre>        <a href="qvboxlayout.html">QVBoxLayout</a> * l = new <a href="qvboxlayout.html">QVBoxLayout</a>( this );</pre><p>We create a QVBoxLayout with all the default values, managing thiswidget's children. <pre>        l-&gt;<a href="qboxlayout.html#ebba99">addWidget</a>( lcd, 1 );</pre><p>At the top, we add the QLCDNumber, with a non-zero stretch. <pre>        l-&gt;<a href="qboxlayout.html#ebba99">addWidget</a>( slider );        l-&gt;<a href="qboxlayout.html#ebba99">addWidget</a>( label );</pre><p>Then we add the other two, both with the default zero stretch.<p>This stretch control is something QVBoxLayout (and QHBoxLayout, andQGridLayout) offers, but classes like QVBox do not.  In this case,we're saying that the QLCDNumber should stretch and the others shouldnot.<p><h3><a href="t13-cannon-h.html">cannon.h</a></h3><p>The CannonField now has a game over state and a few new functions.   <pre>        bool  gameOver() const { return gameEnded; }</pre><p>This function returns TRUE if the game is over, or FALSE if a gameis going on.  <pre>        void  setGameOver();        void  restartGame();</pre><p>Here are two new slots; setGameOver() and restartGame().  <pre>        void  canShoot( bool );</pre><p>This new signal indicates that the CannonField is in a state where theshoot() slot makes sense.  We'll use it below to enable/disable theShoot button.  <pre>        bool gameEnded;</pre><p>This private variable contains the game state.  TRUE means that thegame is over, and FALSE means that a game is going on.<p><h3><a href="t13-cannon-cpp.html">cannon.cpp</a></h3>    <pre>        gameEnded = FALSE;</pre><p>This line has been added to the constructor.  Initially, the game is notover (luckily for the player :-).  <pre>    void CannonField::shoot()    {        if ( isShooting() )            return;        timerCount = 0;        shoot_ang = ang;        shoot_f = f;        autoShootTimer-&gt;start( 50 );        emit canShoot( FALSE );    }</pre><p>We added a new isShooting() function, so shoot() uses it instead oftesting directly.  Also, shoot tells the world that the CannonFieldcannot shot now.  <pre>    void CannonField::setGameOver()    {        if ( gameEnded )            return;        if ( isShooting() )            autoShootTimer-&gt;stop();        gameEnded = TRUE;        <a href="qwidget.html#7569b1">repaint</a>();    }</pre><p>This slot ends the game.  It must be called from outside CannonField,because this widget does not know when to end the game.  This is is animportant design principle in component programming.  We choose tomake the component as flexible as possible to make it usable withdifferent rules.  For example, a multi-player version of this, wherethe first one to hit ten times could use the CannonField unchanged.<p>If the game has already been ended, we return immediately.  If a game isgoing on, we stop the shot, set the game over flag and repaint the entirewidget.  <pre>    void CannonField::restartGame()    {        if ( isShooting() )            autoShootTimer-&gt;stop();        gameEnded = FALSE;        <a href="qwidget.html#7569b1">repaint</a>();        emit canShoot( TRUE );    }</pre><p>This slot starts a new game.  If a shot is in the air, we stop shooting.We then reset the <code>gameEnded</code> variable and repaint the widget.<p>moveShot() too emits the new canShoot(TRUE) signal at the same time aseither hit() or miss().<p>Modifications in CannonField::paintEvent():  <pre>    void CannonField::paintEvent( <a href="qpaintevent.html">QPaintEvent</a> *e )    {        <a href="qrect.html">QRect</a> updateR = e-&gt;<a href="qpaintevent.html#2d6e18">rect</a>();        <a href="qpainter.html">QPainter</a> p( this );            if ( gameEnded ) {            p.<a href="qpainter.html#0183e4">setPen</a>( black );            p.<a href="qpainter.html#998df2">setFont</a>( <a href="qfont.html">QFont</a>( "Courier", 48, QFont::Bold ) );            p.<a href="qpainter.html#0f088f">drawText</a>( <a href="qwidget.html#75ae71">rect</a>(), AlignCenter, "Game Over" );        }</pre><p>The paint event has been enhanced to display the text "Game Over" ifthe game is over, i.e.  <code>gameEnded</code> is TRUE.  We don't bother tocheck the update rectangle here, because speed is not critical whenthe game is over.<p>To draw the text, we first set a black pen.  The pen color is usedwhen drawing text.  Next, we choose a 48 point bold font from theCourier family.  Finally, we draw the text centered in the widget'srectangle.  Unfortunately, on some systems (especially X servers withUnicode fonts) it can take a while to load such a large font.  SinceQt caches fonts, you will only notice this the first time the font is

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -