1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta name="robots" content="noindex"><title>Qt tutorial - t13/gamebrd.cpp</title>
</head><body bgcolor="#ffffff">
<p>
<a href=index.html><img width=122 height=65 src=qtlogo.jpg alt="Qt logo" align=left border=0></a>
<center><img src=dochead.gif width=472 height=27></center>
<br clear=all>
<p>
<h1 align=center>Source code for <a href="t13.html">tutorial 13</a>: gamebrd.cpp</h1><br clear="all">
<hr>
<pre>/****************************************************************
**
** Implementation of GameBoard class, Qt tutorial 13
**
****************************************************************/
#include "gamebrd.h"
#include <<a href="qfont-h.html">qfont.h</a>>
#include <<a href="qapplication-h.html">qapplication.h</a>>
#include <<a href="qlabel-h.html">qlabel.h</a>>
#include <<a href="qpushbutton-h.html">qpushbutton.h</a>>
#include <<a href="qlcdnumber-h.html">qlcdnumber.h</a>>
#include "lcdrange.h"
#include "cannon.h"
GameBoard::GameBoard( <a href="qwidget.html">QWidget</a> *parent, const char *name )
: <a href="qwidget.html">QWidget</a>( parent, name )
{
<a href="qwidget.html#c6">setMinimumSize</a>( 500, 355 );
quit = new <a href="qpushbutton.html">QPushButton</a>( "Quit", this, "quit" );
quit->setFont( <a href="qfont.html">QFont</a>( "Times", 18, QFont::Bold ) );
<a href="qobject.html#f2">connect</a>( quit, SIGNAL(clicked()), qApp, SLOT(quit()) );
angle = new LCDRange( "ANGLE", this, "angle" );
angle->setRange( 5, 70 );
force = new LCDRange( "FORCE", this, "force" );
force->setRange( 10, 50 );
cannonField = new CannonField( this, "cannonField" );
cannonField->setBackgroundColor( <a href="qcolor.html">QColor</a>( 250, 250, 200) );
<a href="qobject.html#f2">connect</a>( angle,SIGNAL(valueChanged(int)), cannonField,SLOT(setAngle(int)));
<a href="qobject.html#f2">connect</a>( cannonField,SIGNAL(angleChanged(int)), angle,SLOT(setValue(int)));
<a href="qobject.html#f2">connect</a>( force,SIGNAL(valueChanged(int)), cannonField,SLOT(setForce(int)));
<a href="qobject.html#f2">connect</a>( cannonField,SIGNAL(forceChanged(int)), force,SLOT(setValue(int)));
<a href="qobject.html#f2">connect</a>( cannonField, SIGNAL(<a href=#337>hit</a>()),SLOT(<a href=#337>hit</a>()) );
<a href="qobject.html#f2">connect</a>( cannonField, SIGNAL(<a href=#338>missed</a>()),SLOT(<a href=#338>missed</a>()) );
angle->setValue( 60 );
force->setValue( 25 );
shoot = new <a href="qpushbutton.html">QPushButton</a>( "Shoot", this, "shoot" );
shoot->setFont( <a href="qfont.html">QFont</a>( "Times", 18, QFont::Bold ) );
<a href="qobject.html#f2">connect</a>( shoot, SIGNAL(clicked()), SLOT(<a href=#336>fire</a>()) );
restart = new <a href="qpushbutton.html">QPushButton</a>( "New Game", this, "newgame" );
restart->setFont( <a href="qfont.html">QFont</a>( "Times", 18, QFont::Bold ) );
<a href="qobject.html#f2">connect</a>( restart, SIGNAL(clicked()), SLOT(<a href=#339>newGame</a>()) );
hits = new <a href="qlcdnumber.html">QLCDNumber</a>( 2, this, "hits" );
shotsLeft = new <a href="qlcdnumber.html">QLCDNumber</a>( 2, this, "shotsleft" );
<a href="qlabel.html">QLabel</a> *hitsL = new <a href="qlabel.html">QLabel</a>( "HITS", this, "hitsLabel" );
<a href="qlabel.html">QLabel</a> *shotsLeftL = new <a href="qlabel.html">QLabel</a>( "SHOTS LEFT", this, "shotsleftLabel" );
quit->setGeometry( 10, 10, 75, 30 );
angle->setGeometry( 10, quit->y() + quit->height() + 10, 75, 130 );
force->setGeometry( 10, angle->y() + angle->height() + 10, 75, 130 );
cannonField->move( angle->x() + angle->width() + 10, angle->y() );
shoot->setGeometry( 10, 315, 75, 30 );
restart->setGeometry( 380, 10, 110, 30 );
hits->setGeometry( 130, 10, 40, 30 );
hitsL-><a href="qwidget.html#l5">setGeometry</a>( hits->x() + hits->width() + 5, 10, 60, 30 );
shotsLeft->setGeometry( 240, 10, 40, 30 );
shotsLeftL-><a href="qwidget.html#l5">setGeometry</a>( shotsLeft->x()+shotsLeft->width()+5, 10, 60, 30 );
<a href=#339>newGame</a>();
}
void <a name="335"></a>GameBoard::resizeEvent( <a href="qresizeevent.html">QResizeEvent</a> * )
{
cannonField->resize( <a href="qwidget.html#b9">width</a>() - cannonField->x() - 10,
<a href="qwidget.html#c0">height</a>() - cannonField->y() - 10 );
}
void <a name="336"></a>GameBoard::fire()
{
if ( cannonField->gameOver() || cannonField->isShooting() )
return;
shotsLeft->display( shotsLeft->intValue() - 1 );
cannonField->shoot();
}
void <a name="337"></a>GameBoard::hit()
{
hits->display( hits->intValue() + 1 );
if ( shotsLeft->intValue() == 0 )
cannonField->setGameOver();
else
cannonField->newTarget();
}
void <a name="338"></a>GameBoard::missed()
{
if ( shotsLeft->intValue() == 0 )
cannonField->setGameOver();
}
void <a name="339"></a>GameBoard::newGame()
{
shotsLeft->display( 15 );
hits->display( 0 );
cannonField->restartGame();
cannonField->newTarget();
}
</pre>
<p>
<p><address><hr><div align="center">
<table width="100%" cellspacing="0" border="0"><tr>
<td>Copyright 1999 Troll Tech<td><a href="trademarks.html">Trademarks</a>
<td align="right"><div align="right">Qt version 1.45</div>
</table></div></address></body></html>
|