1 | /****************************************************************
|
---|
2 | **
|
---|
3 | ** Implementation of GameBoard class, Qt tutorial 14
|
---|
4 | **
|
---|
5 | ****************************************************************/
|
---|
6 |
|
---|
7 | #include "gamebrd.h"
|
---|
8 |
|
---|
9 | #include <qfont.h>
|
---|
10 | #include <qapplication.h>
|
---|
11 | #include <qlabel.h>
|
---|
12 | #include <qaccel.h>
|
---|
13 | #include <qpushbutton.h>
|
---|
14 | #include <qlcdnumber.h>
|
---|
15 | #include <qlayout.h>
|
---|
16 | #include <qvbox.h>
|
---|
17 |
|
---|
18 | #include "lcdrange.h"
|
---|
19 | #include "cannon.h"
|
---|
20 |
|
---|
21 | GameBoard::GameBoard( QWidget *parent, const char *name )
|
---|
22 | : QWidget( parent, name )
|
---|
23 | {
|
---|
24 | QPushButton *quit = new QPushButton( "&Quit", this, "quit" );
|
---|
25 | quit->setFont( QFont( "Times", 18, QFont::Bold ) );
|
---|
26 |
|
---|
27 | connect( quit, SIGNAL(clicked()), qApp, SLOT(quit()) );
|
---|
28 |
|
---|
29 | LCDRange *angle = new LCDRange( "ANGLE", this, "angle" );
|
---|
30 | angle->setRange( 5, 70 );
|
---|
31 |
|
---|
32 | LCDRange *force = new LCDRange( "FORCE", this, "force" );
|
---|
33 | force->setRange( 10, 50 );
|
---|
34 |
|
---|
35 | QVBox *box = new QVBox( this, "cannonFrame" );
|
---|
36 | box->setFrameStyle( QFrame::WinPanel | QFrame::Sunken );
|
---|
37 |
|
---|
38 | cannonField = new CannonField( box, "cannonField" );
|
---|
39 |
|
---|
40 | connect( angle, SIGNAL(valueChanged(int)),
|
---|
41 | cannonField, SLOT(setAngle(int)) );
|
---|
42 | connect( cannonField, SIGNAL(angleChanged(int)),
|
---|
43 | angle, SLOT(setValue(int)) );
|
---|
44 |
|
---|
45 | connect( force, SIGNAL(valueChanged(int)),
|
---|
46 | cannonField, SLOT(setForce(int)) );
|
---|
47 | connect( cannonField, SIGNAL(forceChanged(int)),
|
---|
48 | force, SLOT(setValue(int)) );
|
---|
49 |
|
---|
50 | connect( cannonField, SIGNAL(hit()),
|
---|
51 | this, SLOT(hit()) );
|
---|
52 | connect( cannonField, SIGNAL(missed()),
|
---|
53 | this, SLOT(missed()) );
|
---|
54 |
|
---|
55 | QPushButton *shoot = new QPushButton( "&Shoot", this, "shoot" );
|
---|
56 | shoot->setFont( QFont( "Times", 18, QFont::Bold ) );
|
---|
57 |
|
---|
58 | connect( shoot, SIGNAL(clicked()), SLOT(fire()) );
|
---|
59 |
|
---|
60 | connect( cannonField, SIGNAL(canShoot(bool)),
|
---|
61 | shoot, SLOT(setEnabled(bool)) );
|
---|
62 |
|
---|
63 | QPushButton *restart
|
---|
64 | = new QPushButton( "&New Game", this, "newgame" );
|
---|
65 | restart->setFont( QFont( "Times", 18, QFont::Bold ) );
|
---|
66 |
|
---|
67 | connect( restart, SIGNAL(clicked()), this, SLOT(newGame()) );
|
---|
68 |
|
---|
69 | hits = new QLCDNumber( 2, this, "hits" );
|
---|
70 | shotsLeft = new QLCDNumber( 2, this, "shotsleft" );
|
---|
71 | QLabel *hitsL = new QLabel( "HITS", this, "hitsLabel" );
|
---|
72 | QLabel *shotsLeftL
|
---|
73 | = new QLabel( "SHOTS LEFT", this, "shotsleftLabel" );
|
---|
74 |
|
---|
75 | QAccel *accel = new QAccel( this );
|
---|
76 | accel->connectItem( accel->insertItem( Key_Enter ),
|
---|
77 | this, SLOT(fire()) );
|
---|
78 | accel->connectItem( accel->insertItem( Key_Return ),
|
---|
79 | this, SLOT(fire()) );
|
---|
80 | accel->connectItem( accel->insertItem( CTRL+Key_Q ),
|
---|
81 | qApp, SLOT(quit()) );
|
---|
82 |
|
---|
83 | QGridLayout *grid = new QGridLayout( this, 2, 2, 10 );
|
---|
84 | grid->addWidget( quit, 0, 0 );
|
---|
85 | grid->addWidget( box, 1, 1 );
|
---|
86 | grid->setColStretch( 1, 10 );
|
---|
87 |
|
---|
88 | QVBoxLayout *leftBox = new QVBoxLayout;
|
---|
89 | grid->addLayout( leftBox, 1, 0 );
|
---|
90 | leftBox->addWidget( angle );
|
---|
91 | leftBox->addWidget( force );
|
---|
92 |
|
---|
93 | QHBoxLayout *topBox = new QHBoxLayout;
|
---|
94 | grid->addLayout( topBox, 0, 1 );
|
---|
95 | topBox->addWidget( shoot );
|
---|
96 | topBox->addWidget( hits );
|
---|
97 | topBox->addWidget( hitsL );
|
---|
98 | topBox->addWidget( shotsLeft );
|
---|
99 | topBox->addWidget( shotsLeftL );
|
---|
100 | topBox->addStretch( 1 );
|
---|
101 | topBox->addWidget( restart );
|
---|
102 |
|
---|
103 | angle->setValue( 60 );
|
---|
104 | force->setValue( 25 );
|
---|
105 | angle->setFocus();
|
---|
106 |
|
---|
107 | newGame();
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|
111 | void GameBoard::fire()
|
---|
112 | {
|
---|
113 | if ( cannonField->gameOver() || cannonField->isShooting() )
|
---|
114 | return;
|
---|
115 | shotsLeft->display( shotsLeft->intValue() - 1 );
|
---|
116 | cannonField->shoot();
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 | void GameBoard::hit()
|
---|
121 | {
|
---|
122 | hits->display( hits->intValue() + 1 );
|
---|
123 | if ( shotsLeft->intValue() == 0 )
|
---|
124 | cannonField->setGameOver();
|
---|
125 | else
|
---|
126 | cannonField->newTarget();
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 | void GameBoard::missed()
|
---|
131 | {
|
---|
132 | if ( shotsLeft->intValue() == 0 )
|
---|
133 | cannonField->setGameOver();
|
---|
134 | }
|
---|
135 |
|
---|
136 |
|
---|
137 | void GameBoard::newGame()
|
---|
138 | {
|
---|
139 | shotsLeft->display( 15 );
|
---|
140 | hits->display( 0 );
|
---|
141 | cannonField->restartGame();
|
---|
142 | cannonField->newTarget();
|
---|
143 | }
|
---|