1
/****************************************************************************
3
** Copyright (C) 2005-2005 Trolltech AS. All rights reserved.
5
** This file is part of the example classes of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
29
/****************************************************************
31
** Implementation of GameBoard class, Qt tutorial 14
33
****************************************************************/
35
#include <QApplication>
37
#include <QGridLayout>
38
#include <QHBoxLayout>
41
#include <QPushButton>
43
#include <QVBoxLayout>
46
#include "cannonfield.h"
47
#include "gameboard.h"
50
GameBoard::GameBoard(QWidget *parent)
53
QPushButton *quit = new QPushButton("&Quit");
54
quit->setFont(QFont("Times", 18, QFont::Bold));
56
connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
58
LCDRange *angle = new LCDRange("ANGLE");
59
angle->setRange(5, 70);
61
LCDRange *force = new LCDRange("FORCE");
62
force->setRange(10, 50);
64
QFrame *cannonBox = new QFrame;
65
cannonBox->setFrameStyle(QFrame::WinPanel | QFrame::Sunken);
67
cannonField = new CannonField;
69
connect(angle, SIGNAL(valueChanged(int)),
70
cannonField, SLOT(setAngle(int)));
71
connect(cannonField, SIGNAL(angleChanged(int)),
72
angle, SLOT(setValue(int)));
74
connect(force, SIGNAL(valueChanged(int)),
75
cannonField, SLOT(setForce(int)));
76
connect(cannonField, SIGNAL(forceChanged(int)),
77
force, SLOT(setValue(int)));
79
connect(cannonField, SIGNAL(hit()),
81
connect(cannonField, SIGNAL(missed()),
82
this, SLOT(missed()));
84
QPushButton *shoot = new QPushButton("&Shoot");
85
shoot->setFont(QFont("Times", 18, QFont::Bold));
87
connect(shoot, SIGNAL(clicked()),
89
connect(cannonField, SIGNAL(canShoot(bool)),
90
shoot, SLOT(setEnabled(bool)));
92
QPushButton *restart = new QPushButton("&New Game");
93
restart->setFont(QFont("Times", 18, QFont::Bold));
95
connect(restart, SIGNAL(clicked()), this, SLOT(newGame()));
97
hits = new QLCDNumber(2);
98
shotsLeft = new QLCDNumber(2);
99
QLabel *hitsLabel = new QLabel("HITS");
100
QLabel *shotsLeftLabel = new QLabel("SHOTS LEFT");
102
(void) new QShortcut(Qt::Key_Enter, this, SLOT(fire()));
103
(void) new QShortcut(Qt::Key_Return, this, SLOT(fire()));
104
(void) new QShortcut(Qt::CTRL + Qt::Key_Q, this, SLOT(close()));
106
QHBoxLayout *topLayout = new QHBoxLayout;
107
topLayout->addWidget(shoot);
108
topLayout->addWidget(hits);
109
topLayout->addWidget(hitsLabel);
110
topLayout->addWidget(shotsLeft);
111
topLayout->addWidget(shotsLeftLabel);
112
topLayout->addStretch(1);
113
topLayout->addWidget(restart);
115
QVBoxLayout *leftLayout = new QVBoxLayout;
116
leftLayout->addWidget(angle);
117
leftLayout->addWidget(force);
119
QVBoxLayout *cannonLayout = new QVBoxLayout;
120
cannonLayout->addWidget(cannonField);
121
cannonBox->setLayout(cannonLayout);
123
QGridLayout *gridLayout = new QGridLayout;
124
gridLayout->addWidget(quit, 0, 0);
125
gridLayout->addLayout(topLayout, 0, 1);
126
gridLayout->addLayout(leftLayout, 1, 0);
127
gridLayout->addWidget(cannonBox, 1, 1, 2, 1);
128
gridLayout->setColumnStretch(1, 10);
129
setLayout(gridLayout);
138
void GameBoard::fire()
140
if (cannonField->gameOver() || cannonField->isShooting())
142
shotsLeft->display(shotsLeft->intValue() - 1);
143
cannonField->shoot();
146
void GameBoard::hit()
148
hits->display(hits->intValue() + 1);
149
if (shotsLeft->intValue() == 0)
150
cannonField->setGameOver();
152
cannonField->newTarget();
155
void GameBoard::missed()
157
if (shotsLeft->intValue() == 0)
158
cannonField->setGameOver();
161
void GameBoard::newGame()
163
shotsLeft->display(15);
165
cannonField->restartGame();
166
cannonField->newTarget();