[190] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
---|
| 2 | <!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/doc/tutorial.doc:2029 -->
|
---|
| 3 | <html>
|
---|
| 4 | <head>
|
---|
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
| 6 | <title>t13/gamebrd.cpp Example File</title>
|
---|
| 7 | <style type="text/css"><!--
|
---|
| 8 | fn { margin-left: 1cm; text-indent: -1cm; }
|
---|
| 9 | a:link { color: #004faf; text-decoration: none }
|
---|
| 10 | a:visited { color: #672967; text-decoration: none }
|
---|
| 11 | body { background: #ffffff; color: black; }
|
---|
| 12 | --></style>
|
---|
| 13 | </head>
|
---|
| 14 | <body>
|
---|
| 15 |
|
---|
| 16 | <table border="0" cellpadding="0" cellspacing="0" width="100%">
|
---|
| 17 | <tr bgcolor="#E5E5E5">
|
---|
| 18 | <td valign=center>
|
---|
| 19 | <a href="index.html">
|
---|
| 20 | <font color="#004faf">Home</font></a>
|
---|
| 21 | | <a href="classes.html">
|
---|
| 22 | <font color="#004faf">All Classes</font></a>
|
---|
| 23 | | <a href="mainclasses.html">
|
---|
| 24 | <font color="#004faf">Main Classes</font></a>
|
---|
| 25 | | <a href="annotated.html">
|
---|
| 26 | <font color="#004faf">Annotated</font></a>
|
---|
| 27 | | <a href="groups.html">
|
---|
| 28 | <font color="#004faf">Grouped Classes</font></a>
|
---|
| 29 | | <a href="functions.html">
|
---|
| 30 | <font color="#004faf">Functions</font></a>
|
---|
| 31 | </td>
|
---|
| 32 | <td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>t13/gamebrd.cpp Example File</h1>
|
---|
| 33 |
|
---|
| 34 |
|
---|
| 35 | <pre>/****************************************************************
|
---|
| 36 | **
|
---|
| 37 | ** Implementation of GameBoard class, Qt tutorial 13
|
---|
| 38 | **
|
---|
| 39 | ****************************************************************/
|
---|
| 40 |
|
---|
| 41 | #include "gamebrd.h"
|
---|
| 42 |
|
---|
| 43 | #include <<a href="qfont-h.html">qfont.h</a>>
|
---|
| 44 | #include <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
| 45 | #include <<a href="qlabel-h.html">qlabel.h</a>>
|
---|
| 46 | #include <<a href="qpushbutton-h.html">qpushbutton.h</a>>
|
---|
| 47 | #include <<a href="qlcdnumber-h.html">qlcdnumber.h</a>>
|
---|
| 48 | #include <<a href="qlayout-h.html">qlayout.h</a>>
|
---|
| 49 |
|
---|
| 50 | #include "lcdrange.h"
|
---|
| 51 | #include "cannon.h"
|
---|
| 52 |
|
---|
| 53 | <a name="f109"></a>GameBoard::GameBoard( <a href="qwidget.html">QWidget</a> *parent, const char *name )
|
---|
| 54 | : <a href="qwidget.html">QWidget</a>( parent, name )
|
---|
| 55 | {
|
---|
| 56 | <a href="qpushbutton.html">QPushButton</a> *quit = new <a href="qpushbutton.html">QPushButton</a>( "&Quit", this, "quit" );
|
---|
| 57 | quit-><a href="qwidget.html#setFont">setFont</a>( QFont( "Times", 18, QFont::Bold ) );
|
---|
| 58 |
|
---|
| 59 | <a href="qobject.html#connect">connect</a>( quit, SIGNAL(<a href="qbutton.html#clicked">clicked</a>()), qApp, SLOT(<a href="qapplication.html#quit">quit</a>()) );
|
---|
| 60 |
|
---|
| 61 | LCDRange *angle = new LCDRange( "ANGLE", this, "angle" );
|
---|
| 62 | angle->setRange( 5, 70 );
|
---|
| 63 |
|
---|
| 64 | LCDRange *force = new LCDRange( "FORCE", this, "force" );
|
---|
| 65 | force->setRange( 10, 50 );
|
---|
| 66 |
|
---|
| 67 | cannonField = new CannonField( this, "cannonField" );
|
---|
| 68 |
|
---|
| 69 | <a href="qobject.html#connect">connect</a>( angle, SIGNAL(valueChanged(int)),
|
---|
| 70 | cannonField, SLOT(setAngle(int)) );
|
---|
| 71 | <a href="qobject.html#connect">connect</a>( cannonField, SIGNAL(angleChanged(int)),
|
---|
| 72 | angle, SLOT(setValue(int)) );
|
---|
| 73 |
|
---|
| 74 | <a href="qobject.html#connect">connect</a>( force, SIGNAL(valueChanged(int)),
|
---|
| 75 | cannonField, SLOT(setForce(int)) );
|
---|
| 76 | <a href="qobject.html#connect">connect</a>( cannonField, SIGNAL(forceChanged(int)),
|
---|
| 77 | force, SLOT(setValue(int)) );
|
---|
| 78 |
|
---|
| 79 | <a href="qobject.html#connect">connect</a>( cannonField, SIGNAL(hit()),
|
---|
| 80 | this, SLOT(hit()) );
|
---|
| 81 | <a href="qobject.html#connect">connect</a>( cannonField, SIGNAL(missed()),
|
---|
| 82 | this, SLOT(missed()) );
|
---|
| 83 |
|
---|
| 84 | <a href="qpushbutton.html">QPushButton</a> *shoot = new <a href="qpushbutton.html">QPushButton</a>( "&Shoot", this, "shoot" );
|
---|
| 85 | shoot-><a href="qwidget.html#setFont">setFont</a>( QFont( "Times", 18, QFont::Bold ) );
|
---|
| 86 |
|
---|
| 87 | <a href="qobject.html#connect">connect</a>( shoot, SIGNAL(<a href="qbutton.html#clicked">clicked</a>()), SLOT(fire()) );
|
---|
| 88 | <a href="qobject.html#connect">connect</a>( cannonField, SIGNAL(canShoot(bool)),
|
---|
| 89 | shoot, SLOT(<a href="qwidget.html#setEnabled">setEnabled</a>(bool)) );
|
---|
| 90 |
|
---|
| 91 | QPushButton *restart
|
---|
| 92 | = new <a href="qpushbutton.html">QPushButton</a>( "&New Game", this, "newgame" );
|
---|
| 93 | restart->setFont( QFont( "Times", 18, QFont::Bold ) );
|
---|
| 94 |
|
---|
| 95 | <a href="qobject.html#connect">connect</a>( restart, SIGNAL(clicked()), this, SLOT(newGame()) );
|
---|
| 96 |
|
---|
| 97 | hits = new <a href="qlcdnumber.html">QLCDNumber</a>( 2, this, "hits" );
|
---|
| 98 | shotsLeft = new <a href="qlcdnumber.html">QLCDNumber</a>( 2, this, "shotsleft" );
|
---|
| 99 | <a href="qlabel.html">QLabel</a> *hitsL = new <a href="qlabel.html">QLabel</a>( "HITS", this, "hitsLabel" );
|
---|
| 100 | QLabel *shotsLeftL
|
---|
| 101 | = new <a href="qlabel.html">QLabel</a>( "SHOTS LEFT", this, "shotsleftLabel" );
|
---|
| 102 |
|
---|
| 103 | <a href="qgridlayout.html">QGridLayout</a> *grid = new <a href="qgridlayout.html">QGridLayout</a>( this, 2, 2, 10 );
|
---|
| 104 | grid-><a href="qgridlayout.html#addWidget">addWidget</a>( quit, 0, 0 );
|
---|
| 105 | grid-><a href="qgridlayout.html#addWidget">addWidget</a>( cannonField, 1, 1 );
|
---|
| 106 | grid-><a href="qgridlayout.html#setColStretch">setColStretch</a>( 1, 10 );
|
---|
| 107 |
|
---|
| 108 | <a href="qvboxlayout.html">QVBoxLayout</a> *leftBox = new <a href="qvboxlayout.html">QVBoxLayout</a>;
|
---|
| 109 | grid-><a href="qgridlayout.html#addLayout">addLayout</a>( leftBox, 1, 0 );
|
---|
| 110 | leftBox-><a href="qboxlayout.html#addWidget">addWidget</a>( angle );
|
---|
| 111 | leftBox-><a href="qboxlayout.html#addWidget">addWidget</a>( force );
|
---|
| 112 |
|
---|
| 113 | <a href="qhboxlayout.html">QHBoxLayout</a> *topBox = new <a href="qhboxlayout.html">QHBoxLayout</a>;
|
---|
| 114 | grid-><a href="qgridlayout.html#addLayout">addLayout</a>( topBox, 0, 1 );
|
---|
| 115 | topBox-><a href="qboxlayout.html#addWidget">addWidget</a>( shoot );
|
---|
| 116 | topBox-><a href="qboxlayout.html#addWidget">addWidget</a>( hits );
|
---|
| 117 | topBox-><a href="qboxlayout.html#addWidget">addWidget</a>( hitsL );
|
---|
| 118 | topBox-><a href="qboxlayout.html#addWidget">addWidget</a>( shotsLeft );
|
---|
| 119 | topBox-><a href="qboxlayout.html#addWidget">addWidget</a>( shotsLeftL );
|
---|
| 120 | topBox-><a href="qboxlayout.html#addStretch">addStretch</a>( 1 );
|
---|
| 121 | topBox-><a href="qboxlayout.html#addWidget">addWidget</a>( restart );
|
---|
| 122 |
|
---|
| 123 | angle->setValue( 60 );
|
---|
| 124 | force->setValue( 25 );
|
---|
| 125 | angle-><a href="qwidget.html#setFocus">setFocus</a>();
|
---|
| 126 |
|
---|
| 127 | newGame();
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 |
|
---|
| 131 | void <a name="f110"></a>GameBoard::fire()
|
---|
| 132 | {
|
---|
| 133 | if ( cannonField->gameOver() || cannonField->isShooting() )
|
---|
| 134 | return;
|
---|
| 135 | shotsLeft-><a href="qlcdnumber.html#display">display</a>( shotsLeft-><a href="qlcdnumber.html#intValue">intValue</a>() - 1 );
|
---|
| 136 | cannonField->shoot();
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 |
|
---|
| 140 | void <a name="f111"></a>GameBoard::hit()
|
---|
| 141 | {
|
---|
| 142 | hits-><a href="qlcdnumber.html#display">display</a>( hits-><a href="qlcdnumber.html#intValue">intValue</a>() + 1 );
|
---|
| 143 | if ( shotsLeft-><a href="qlcdnumber.html#intValue">intValue</a>() == 0 )
|
---|
| 144 | cannonField->setGameOver();
|
---|
| 145 | else
|
---|
| 146 | cannonField->newTarget();
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 |
|
---|
| 150 | void <a name="f112"></a>GameBoard::missed()
|
---|
| 151 | {
|
---|
| 152 | if ( shotsLeft-><a href="qlcdnumber.html#intValue">intValue</a>() == 0 )
|
---|
| 153 | cannonField->setGameOver();
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 |
|
---|
| 157 | void <a name="f113"></a>GameBoard::newGame()
|
---|
| 158 | {
|
---|
| 159 | shotsLeft-><a href="qlcdnumber.html#display">display</a>( 15 );
|
---|
| 160 | hits-><a href="qlcdnumber.html#display">display</a>( 0 );
|
---|
| 161 | cannonField->restartGame();
|
---|
| 162 | cannonField->newTarget();
|
---|
| 163 | }
|
---|
| 164 | </pre><!-- eof -->
|
---|
| 165 | <p><address><hr><div align=center>
|
---|
| 166 | <table width=100% cellspacing=0 border=0><tr>
|
---|
| 167 | <td>Copyright © 2007
|
---|
| 168 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
| 169 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
| 170 | </table></div></address></body>
|
---|
| 171 | </html>
|
---|