[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:2378 -->
|
---|
| 3 | <html>
|
---|
| 4 | <head>
|
---|
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
| 6 | <title>t14/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>t14/gamebrd.cpp Example File</h1>
|
---|
| 33 |
|
---|
| 34 |
|
---|
| 35 | <pre>/****************************************************************
|
---|
| 36 | **
|
---|
| 37 | ** Implementation of GameBoard class, Qt tutorial 14
|
---|
| 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="qaccel-h.html">qaccel.h</a>>
|
---|
| 47 | #include <<a href="qpushbutton-h.html">qpushbutton.h</a>>
|
---|
| 48 | #include <<a href="qlcdnumber-h.html">qlcdnumber.h</a>>
|
---|
| 49 | #include <<a href="qlayout-h.html">qlayout.h</a>>
|
---|
| 50 | #include <<a href="qvbox-h.html">qvbox.h</a>>
|
---|
| 51 |
|
---|
| 52 | #include "lcdrange.h"
|
---|
| 53 | #include "cannon.h"
|
---|
| 54 |
|
---|
| 55 | <a name="f139"></a>GameBoard::GameBoard( <a href="qwidget.html">QWidget</a> *parent, const char *name )
|
---|
| 56 | : <a href="qwidget.html">QWidget</a>( parent, name )
|
---|
| 57 | {
|
---|
| 58 | <a href="qpushbutton.html">QPushButton</a> *quit = new <a href="qpushbutton.html">QPushButton</a>( "&Quit", this, "quit" );
|
---|
| 59 | quit-><a href="qwidget.html#setFont">setFont</a>( QFont( "Times", 18, QFont::Bold ) );
|
---|
| 60 |
|
---|
| 61 | <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>()) );
|
---|
| 62 |
|
---|
| 63 | LCDRange *angle = new LCDRange( "ANGLE", this, "angle" );
|
---|
| 64 | angle->setRange( 5, 70 );
|
---|
| 65 |
|
---|
| 66 | LCDRange *force = new LCDRange( "FORCE", this, "force" );
|
---|
| 67 | force->setRange( 10, 50 );
|
---|
| 68 |
|
---|
| 69 | <a href="qvbox.html">QVBox</a> *box = new <a href="qvbox.html">QVBox</a>( this, "cannonFrame" );
|
---|
| 70 | box-><a href="qframe.html#setFrameStyle">setFrameStyle</a>( QFrame::WinPanel | QFrame::Sunken );
|
---|
| 71 |
|
---|
| 72 | cannonField = new CannonField( box, "cannonField" );
|
---|
| 73 |
|
---|
| 74 | <a href="qobject.html#connect">connect</a>( angle, SIGNAL(valueChanged(int)),
|
---|
| 75 | cannonField, SLOT(setAngle(int)) );
|
---|
| 76 | <a href="qobject.html#connect">connect</a>( cannonField, SIGNAL(angleChanged(int)),
|
---|
| 77 | angle, SLOT(setValue(int)) );
|
---|
| 78 |
|
---|
| 79 | <a href="qobject.html#connect">connect</a>( force, SIGNAL(valueChanged(int)),
|
---|
| 80 | cannonField, SLOT(setForce(int)) );
|
---|
| 81 | <a href="qobject.html#connect">connect</a>( cannonField, SIGNAL(forceChanged(int)),
|
---|
| 82 | force, SLOT(setValue(int)) );
|
---|
| 83 |
|
---|
| 84 | <a href="qobject.html#connect">connect</a>( cannonField, SIGNAL(hit()),
|
---|
| 85 | this, SLOT(hit()) );
|
---|
| 86 | <a href="qobject.html#connect">connect</a>( cannonField, SIGNAL(missed()),
|
---|
| 87 | this, SLOT(missed()) );
|
---|
| 88 |
|
---|
| 89 | <a href="qpushbutton.html">QPushButton</a> *shoot = new <a href="qpushbutton.html">QPushButton</a>( "&Shoot", this, "shoot" );
|
---|
| 90 | shoot-><a href="qwidget.html#setFont">setFont</a>( QFont( "Times", 18, QFont::Bold ) );
|
---|
| 91 |
|
---|
| 92 | <a href="qobject.html#connect">connect</a>( shoot, SIGNAL(<a href="qbutton.html#clicked">clicked</a>()), SLOT(fire()) );
|
---|
| 93 |
|
---|
| 94 | <a href="qobject.html#connect">connect</a>( cannonField, SIGNAL(canShoot(bool)),
|
---|
| 95 | shoot, SLOT(<a href="qwidget.html#setEnabled">setEnabled</a>(bool)) );
|
---|
| 96 |
|
---|
| 97 | QPushButton *restart
|
---|
| 98 | = new <a href="qpushbutton.html">QPushButton</a>( "&New Game", this, "newgame" );
|
---|
| 99 | restart->setFont( QFont( "Times", 18, QFont::Bold ) );
|
---|
| 100 |
|
---|
| 101 | <a href="qobject.html#connect">connect</a>( restart, SIGNAL(clicked()), this, SLOT(newGame()) );
|
---|
| 102 |
|
---|
| 103 | hits = new <a href="qlcdnumber.html">QLCDNumber</a>( 2, this, "hits" );
|
---|
| 104 | shotsLeft = new <a href="qlcdnumber.html">QLCDNumber</a>( 2, this, "shotsleft" );
|
---|
| 105 | <a href="qlabel.html">QLabel</a> *hitsL = new <a href="qlabel.html">QLabel</a>( "HITS", this, "hitsLabel" );
|
---|
| 106 | QLabel *shotsLeftL
|
---|
| 107 | = new <a href="qlabel.html">QLabel</a>( "SHOTS LEFT", this, "shotsleftLabel" );
|
---|
| 108 |
|
---|
| 109 | <a href="qaccel.html">QAccel</a> *accel = new <a href="qaccel.html">QAccel</a>( this );
|
---|
| 110 | accel-><a href="qaccel.html#connectItem">connectItem</a>( accel-><a href="qaccel.html#insertItem">insertItem</a>( Key_Enter ),
|
---|
| 111 | this, SLOT(fire()) );
|
---|
| 112 | accel-><a href="qaccel.html#connectItem">connectItem</a>( accel-><a href="qaccel.html#insertItem">insertItem</a>( Key_Return ),
|
---|
| 113 | this, SLOT(fire()) );
|
---|
| 114 | accel-><a href="qaccel.html#connectItem">connectItem</a>( accel-><a href="qaccel.html#insertItem">insertItem</a>( CTRL+Key_Q ),
|
---|
| 115 | qApp, SLOT(<a href="qapplication.html#quit">quit</a>()) );
|
---|
| 116 |
|
---|
| 117 | <a href="qgridlayout.html">QGridLayout</a> *grid = new <a href="qgridlayout.html">QGridLayout</a>( this, 2, 2, 10 );
|
---|
| 118 | grid-><a href="qgridlayout.html#addWidget">addWidget</a>( quit, 0, 0 );
|
---|
| 119 | grid-><a href="qgridlayout.html#addWidget">addWidget</a>( box, 1, 1 );
|
---|
| 120 | grid-><a href="qgridlayout.html#setColStretch">setColStretch</a>( 1, 10 );
|
---|
| 121 |
|
---|
| 122 | <a href="qvboxlayout.html">QVBoxLayout</a> *leftBox = new <a href="qvboxlayout.html">QVBoxLayout</a>;
|
---|
| 123 | grid-><a href="qgridlayout.html#addLayout">addLayout</a>( leftBox, 1, 0 );
|
---|
| 124 | leftBox-><a href="qboxlayout.html#addWidget">addWidget</a>( angle );
|
---|
| 125 | leftBox-><a href="qboxlayout.html#addWidget">addWidget</a>( force );
|
---|
| 126 |
|
---|
| 127 | <a href="qhboxlayout.html">QHBoxLayout</a> *topBox = new <a href="qhboxlayout.html">QHBoxLayout</a>;
|
---|
| 128 | grid-><a href="qgridlayout.html#addLayout">addLayout</a>( topBox, 0, 1 );
|
---|
| 129 | topBox-><a href="qboxlayout.html#addWidget">addWidget</a>( shoot );
|
---|
| 130 | topBox-><a href="qboxlayout.html#addWidget">addWidget</a>( hits );
|
---|
| 131 | topBox-><a href="qboxlayout.html#addWidget">addWidget</a>( hitsL );
|
---|
| 132 | topBox-><a href="qboxlayout.html#addWidget">addWidget</a>( shotsLeft );
|
---|
| 133 | topBox-><a href="qboxlayout.html#addWidget">addWidget</a>( shotsLeftL );
|
---|
| 134 | topBox-><a href="qboxlayout.html#addStretch">addStretch</a>( 1 );
|
---|
| 135 | topBox-><a href="qboxlayout.html#addWidget">addWidget</a>( restart );
|
---|
| 136 |
|
---|
| 137 | angle->setValue( 60 );
|
---|
| 138 | force->setValue( 25 );
|
---|
| 139 | angle-><a href="qwidget.html#setFocus">setFocus</a>();
|
---|
| 140 |
|
---|
| 141 | newGame();
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 |
|
---|
| 145 | void <a name="f140"></a>GameBoard::fire()
|
---|
| 146 | {
|
---|
| 147 | if ( cannonField->gameOver() || cannonField->isShooting() )
|
---|
| 148 | return;
|
---|
| 149 | shotsLeft-><a href="qlcdnumber.html#display">display</a>( shotsLeft-><a href="qlcdnumber.html#intValue">intValue</a>() - 1 );
|
---|
| 150 | cannonField->shoot();
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 |
|
---|
| 154 | void <a name="f141"></a>GameBoard::hit()
|
---|
| 155 | {
|
---|
| 156 | hits-><a href="qlcdnumber.html#display">display</a>( hits-><a href="qlcdnumber.html#intValue">intValue</a>() + 1 );
|
---|
| 157 | if ( shotsLeft-><a href="qlcdnumber.html#intValue">intValue</a>() == 0 )
|
---|
| 158 | cannonField->setGameOver();
|
---|
| 159 | else
|
---|
| 160 | cannonField->newTarget();
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 |
|
---|
| 164 | void <a name="f142"></a>GameBoard::missed()
|
---|
| 165 | {
|
---|
| 166 | if ( shotsLeft-><a href="qlcdnumber.html#intValue">intValue</a>() == 0 )
|
---|
| 167 | cannonField->setGameOver();
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 |
|
---|
| 171 | void <a name="f143"></a>GameBoard::newGame()
|
---|
| 172 | {
|
---|
| 173 | shotsLeft-><a href="qlcdnumber.html#display">display</a>( 15 );
|
---|
| 174 | hits-><a href="qlcdnumber.html#display">display</a>( 0 );
|
---|
| 175 | cannonField->restartGame();
|
---|
| 176 | cannonField->newTarget();
|
---|
| 177 | }
|
---|
| 178 | </pre><!-- eof -->
|
---|
| 179 | <p><address><hr><div align=center>
|
---|
| 180 | <table width=100% cellspacing=0 border=0><tr>
|
---|
| 181 | <td>Copyright © 2007
|
---|
| 182 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
| 183 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
| 184 | </table></div></address></body>
|
---|
| 185 | </html>
|
---|