diff options
author | Timothy Pearson <kb9vqf@pearsoncomputing.net> | 2011-07-10 15:24:15 -0500 |
---|---|---|
committer | Timothy Pearson <kb9vqf@pearsoncomputing.net> | 2011-07-10 15:24:15 -0500 |
commit | bd0f3345a938b35ce6a12f6150373b0955b8dd12 (patch) | |
tree | 7a520322212d48ebcb9fbe1087e7fca28b76185c /tutorial/t13 | |
download | qt3-bd0f3345a938b35ce6a12f6150373b0955b8dd12.tar.gz qt3-bd0f3345a938b35ce6a12f6150373b0955b8dd12.zip |
Add Qt3 development HEAD version
Diffstat (limited to 'tutorial/t13')
-rw-r--r-- | tutorial/t13/cannon.cpp | 231 | ||||
-rw-r--r-- | tutorial/t13/cannon.h | 71 | ||||
-rw-r--r-- | tutorial/t13/gamebrd.cpp | 129 | ||||
-rw-r--r-- | tutorial/t13/gamebrd.h | 40 | ||||
-rw-r--r-- | tutorial/t13/lcdrange.cpp | 88 | ||||
-rw-r--r-- | tutorial/t13/lcdrange.h | 42 | ||||
-rw-r--r-- | tutorial/t13/main.cpp | 22 | ||||
-rw-r--r-- | tutorial/t13/t13.pro | 13 |
8 files changed, 636 insertions, 0 deletions
diff --git a/tutorial/t13/cannon.cpp b/tutorial/t13/cannon.cpp new file mode 100644 index 0000000..ac8f3d1 --- /dev/null +++ b/tutorial/t13/cannon.cpp @@ -0,0 +1,231 @@ +/**************************************************************** +** +** Implementation CannonField class, Qt tutorial 13 +** +****************************************************************/ + +#include "cannon.h" +#include <qtimer.h> +#include <qpainter.h> +#include <qpixmap.h> +#include <qdatetime.h> + +#include <math.h> +#include <stdlib.h> + + +CannonField::CannonField( QWidget *parent, const char *name ) + : QWidget( parent, name ) +{ + ang = 45; + f = 0; + timerCount = 0; + autoShootTimer = new QTimer( this, "movement handler" ); + connect( autoShootTimer, SIGNAL(timeout()), + this, SLOT(moveShot()) ); + shoot_ang = 0; + shoot_f = 0; + target = QPoint( 0, 0 ); + gameEnded = FALSE; + setPalette( QPalette( QColor( 250, 250, 200) ) ); + newTarget(); +} + + +void CannonField::setAngle( int degrees ) +{ + if ( degrees < 5 ) + degrees = 5; + if ( degrees > 70 ) + degrees = 70; + if ( ang == degrees ) + return; + ang = degrees; + repaint( cannonRect(), FALSE ); + emit angleChanged( ang ); +} + + +void CannonField::setForce( int newton ) +{ + if ( newton < 0 ) + newton = 0; + if ( f == newton ) + return; + f = newton; + emit forceChanged( f ); +} + + +void CannonField::shoot() +{ + if ( isShooting() ) + return; + timerCount = 0; + shoot_ang = ang; + shoot_f = f; + autoShootTimer->start( 50 ); + emit canShoot( FALSE ); +} + + +void CannonField::newTarget() +{ + static bool first_time = TRUE; + if ( first_time ) { + first_time = FALSE; + QTime midnight( 0, 0, 0 ); + srand( midnight.secsTo(QTime::currentTime()) ); + } + QRegion r( targetRect() ); + target = QPoint( 200 + rand() % 190, + 10 + rand() % 255 ); + repaint( r.unite( targetRect() ) ); +} + +void CannonField::setGameOver() +{ + if ( gameEnded ) + return; + if ( isShooting() ) + autoShootTimer->stop(); + gameEnded = TRUE; + repaint(); +} + +void CannonField::restartGame() +{ + if ( isShooting() ) + autoShootTimer->stop(); + gameEnded = FALSE; + repaint(); + emit canShoot( TRUE ); +} + +void CannonField::moveShot() +{ + QRegion r( shotRect() ); + timerCount++; + + QRect shotR = shotRect(); + + if ( shotR.intersects( targetRect() ) ) { + autoShootTimer->stop(); + emit hit(); + emit canShoot( TRUE ); + } else if ( shotR.x() > width() || shotR.y() > height() ) { + autoShootTimer->stop(); + emit missed(); + emit canShoot( TRUE ); + } else { + r = r.unite( QRegion( shotR ) ); + } + + repaint( r ); +} + + +void CannonField::paintEvent( QPaintEvent *e ) +{ + QRect updateR = e->rect(); + QPainter p( this ); + + if ( gameEnded ) { + p.setPen( black ); + p.setFont( QFont( "Courier", 48, QFont::Bold ) ); + p.drawText( rect(), AlignCenter, "Game Over" ); + } + if ( updateR.intersects( cannonRect() ) ) + paintCannon( &p ); + if ( isShooting() && updateR.intersects( shotRect() ) ) + paintShot( &p ); + if ( !gameEnded && updateR.intersects( targetRect() ) ) + paintTarget( &p ); +} + + +void CannonField::paintShot( QPainter *p ) +{ + p->setBrush( black ); + p->setPen( NoPen ); + p->drawRect( shotRect() ); +} + + +void CannonField::paintTarget( QPainter *p ) +{ + p->setBrush( red ); + p->setPen( black ); + p->drawRect( targetRect() ); +} + + +const QRect barrelRect(33, -4, 15, 8); + +void CannonField::paintCannon( QPainter *p ) +{ + QRect cr = cannonRect(); + QPixmap pix( cr.size() ); + pix.fill( this, cr.topLeft() ); + + QPainter tmp( &pix ); + tmp.setBrush( blue ); + tmp.setPen( NoPen ); + + tmp.translate( 0, pix.height() - 1 ); + tmp.drawPie( QRect( -35,-35, 70, 70 ), 0, 90*16 ); + tmp.rotate( -ang ); + tmp.drawRect( barrelRect ); + tmp.end(); + + p->drawPixmap( cr.topLeft(), pix ); +} + + +QRect CannonField::cannonRect() const +{ + QRect r( 0, 0, 50, 50 ); + r.moveBottomLeft( rect().bottomLeft() ); + return r; +} + + +QRect CannonField::shotRect() const +{ + const double gravity = 4; + + double time = timerCount / 4.0; + double velocity = shoot_f; + double radians = shoot_ang*3.14159265/180; + + double velx = velocity*cos( radians ); + double vely = velocity*sin( radians ); + double x0 = ( barrelRect.right() + 5 )*cos(radians); + double y0 = ( barrelRect.right() + 5 )*sin(radians); + double x = x0 + velx*time; + double y = y0 + vely*time - 0.5*gravity*time*time; + + QRect r = QRect( 0, 0, 6, 6 ); + r.moveCenter( QPoint( qRound(x), height() - 1 - qRound(y) ) ); + return r; +} + + +QRect CannonField::targetRect() const +{ + QRect r( 0, 0, 20, 10 ); + r.moveCenter( QPoint(target.x(),height() - 1 - target.y()) ); + return r; +} + + +bool CannonField::isShooting() const +{ + return autoShootTimer->isActive(); +} + + +QSizePolicy CannonField::sizePolicy() const +{ + return QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ); +} diff --git a/tutorial/t13/cannon.h b/tutorial/t13/cannon.h new file mode 100644 index 0000000..ff1ff82 --- /dev/null +++ b/tutorial/t13/cannon.h @@ -0,0 +1,71 @@ +/**************************************************************** +** +** Definition of CannonField class, Qt tutorial 13 +** +****************************************************************/ + +#ifndef CANNON_H +#define CANNON_H + +class QTimer; + + +#include <qwidget.h> + + +class CannonField : public QWidget +{ + Q_OBJECT +public: + CannonField( QWidget *parent=0, const char *name=0 ); + + int angle() const { return ang; } + int force() const { return f; } + bool gameOver() const { return gameEnded; } + bool isShooting() const; + QSizePolicy sizePolicy() const; + +public slots: + void setAngle( int degrees ); + void setForce( int newton ); + void shoot(); + void newTarget(); + void setGameOver(); + void restartGame(); + +private slots: + void moveShot(); + +signals: + void hit(); + void missed(); + void angleChanged( int ); + void forceChanged( int ); + void canShoot( bool ); + +protected: + void paintEvent( QPaintEvent * ); + +private: + void paintShot( QPainter * ); + void paintTarget( QPainter * ); + void paintCannon( QPainter * ); + QRect cannonRect() const; + QRect shotRect() const; + QRect targetRect() const; + + int ang; + int f; + + int timerCount; + QTimer * autoShootTimer; + float shoot_ang; + float shoot_f; + + QPoint target; + + bool gameEnded; +}; + + +#endif // CANNON_H diff --git a/tutorial/t13/gamebrd.cpp b/tutorial/t13/gamebrd.cpp new file mode 100644 index 0000000..ffb3228 --- /dev/null +++ b/tutorial/t13/gamebrd.cpp @@ -0,0 +1,129 @@ +/**************************************************************** +** +** Implementation of GameBoard class, Qt tutorial 13 +** +****************************************************************/ + +#include "gamebrd.h" + +#include <qfont.h> +#include <qapplication.h> +#include <qlabel.h> +#include <qpushbutton.h> +#include <qlcdnumber.h> +#include <qlayout.h> + +#include "lcdrange.h" +#include "cannon.h" + +GameBoard::GameBoard( QWidget *parent, const char *name ) + : QWidget( parent, name ) +{ + QPushButton *quit = new QPushButton( "&Quit", this, "quit" ); + quit->setFont( QFont( "Times", 18, QFont::Bold ) ); + + connect( quit, SIGNAL(clicked()), qApp, SLOT(quit()) ); + + LCDRange *angle = new LCDRange( "ANGLE", this, "angle" ); + angle->setRange( 5, 70 ); + + LCDRange *force = new LCDRange( "FORCE", this, "force" ); + force->setRange( 10, 50 ); + + cannonField = new CannonField( this, "cannonField" ); + + connect( angle, SIGNAL(valueChanged(int)), + cannonField, SLOT(setAngle(int)) ); + connect( cannonField, SIGNAL(angleChanged(int)), + angle, SLOT(setValue(int)) ); + + connect( force, SIGNAL(valueChanged(int)), + cannonField, SLOT(setForce(int)) ); + connect( cannonField, SIGNAL(forceChanged(int)), + force, SLOT(setValue(int)) ); + + connect( cannonField, SIGNAL(hit()), + this, SLOT(hit()) ); + connect( cannonField, SIGNAL(missed()), + this, SLOT(missed()) ); + + QPushButton *shoot = new QPushButton( "&Shoot", this, "shoot" ); + shoot->setFont( QFont( "Times", 18, QFont::Bold ) ); + + connect( shoot, SIGNAL(clicked()), SLOT(fire()) ); + connect( cannonField, SIGNAL(canShoot(bool)), + shoot, SLOT(setEnabled(bool)) ); + + QPushButton *restart + = new QPushButton( "&New Game", this, "newgame" ); + restart->setFont( QFont( "Times", 18, QFont::Bold ) ); + + connect( restart, SIGNAL(clicked()), this, SLOT(newGame()) ); + + hits = new QLCDNumber( 2, this, "hits" ); + shotsLeft = new QLCDNumber( 2, this, "shotsleft" ); + QLabel *hitsL = new QLabel( "HITS", this, "hitsLabel" ); + QLabel *shotsLeftL + = new QLabel( "SHOTS LEFT", this, "shotsleftLabel" ); + + QGridLayout *grid = new QGridLayout( this, 2, 2, 10 ); + grid->addWidget( quit, 0, 0 ); + grid->addWidget( cannonField, 1, 1 ); + grid->setColStretch( 1, 10 ); + + QVBoxLayout *leftBox = new QVBoxLayout; + grid->addLayout( leftBox, 1, 0 ); + leftBox->addWidget( angle ); + leftBox->addWidget( force ); + + QHBoxLayout *topBox = new QHBoxLayout; + grid->addLayout( topBox, 0, 1 ); + topBox->addWidget( shoot ); + topBox->addWidget( hits ); + topBox->addWidget( hitsL ); + topBox->addWidget( shotsLeft ); + topBox->addWidget( shotsLeftL ); + topBox->addStretch( 1 ); + topBox->addWidget( restart ); + + angle->setValue( 60 ); + force->setValue( 25 ); + angle->setFocus(); + + newGame(); +} + + +void GameBoard::fire() +{ + if ( cannonField->gameOver() || cannonField->isShooting() ) + return; + shotsLeft->display( shotsLeft->intValue() - 1 ); + cannonField->shoot(); +} + + +void GameBoard::hit() +{ + hits->display( hits->intValue() + 1 ); + if ( shotsLeft->intValue() == 0 ) + cannonField->setGameOver(); + else + cannonField->newTarget(); +} + + +void GameBoard::missed() +{ + if ( shotsLeft->intValue() == 0 ) + cannonField->setGameOver(); +} + + +void GameBoard::newGame() +{ + shotsLeft->display( 15 ); + hits->display( 0 ); + cannonField->restartGame(); + cannonField->newTarget(); +} diff --git a/tutorial/t13/gamebrd.h b/tutorial/t13/gamebrd.h new file mode 100644 index 0000000..1c629a4 --- /dev/null +++ b/tutorial/t13/gamebrd.h @@ -0,0 +1,40 @@ +/**************************************************************** +** +** Definition of GameBoard class, Qt tutorial 13 +** +****************************************************************/ + +#ifndef GAMEBRD_H +#define GAMEBRD_H + +#include <qwidget.h> + +class QPushButton; +class LCDRange; +class QLCDNumber; +class CannonField; + +#include "lcdrange.h" +#include "cannon.h" + + +class GameBoard : public QWidget +{ + Q_OBJECT +public: + GameBoard( QWidget *parent=0, const char *name=0 ); + +protected slots: + void fire(); + void hit(); + void missed(); + void newGame(); + +private: + QLCDNumber *hits; + QLCDNumber *shotsLeft; + CannonField *cannonField; +}; + + +#endif // GAMEBRD_H diff --git a/tutorial/t13/lcdrange.cpp b/tutorial/t13/lcdrange.cpp new file mode 100644 index 0000000..3c91b27 --- /dev/null +++ b/tutorial/t13/lcdrange.cpp @@ -0,0 +1,88 @@ +/**************************************************************** +** +** Implementation of LCDRange class, Qt tutorial 12 +** +****************************************************************/ + +#include "lcdrange.h" + +#include <qslider.h> +#include <qlcdnumber.h> +#include <qlabel.h> +#include <qlayout.h> + + +LCDRange::LCDRange( QWidget *parent, const char *name ) + : QWidget( parent, name ) +{ + init(); +} + + +LCDRange::LCDRange( const char *s, QWidget *parent, const char *name ) + : QWidget( parent, name ) +{ + init(); + setText( s ); +} + + +void LCDRange::init() +{ + QLCDNumber *lcd = new QLCDNumber( 2, this, "lcd" ); + slider = new QSlider( Horizontal, this, "slider" ); + slider->setRange( 0, 99 ); + slider->setValue( 0 ); + + label = new QLabel( " ", this, "label" ); + label->setAlignment( AlignCenter ); + + connect( slider, SIGNAL(valueChanged(int)), + lcd, SLOT(display(int)) ); + connect( slider, SIGNAL(valueChanged(int)), + SIGNAL(valueChanged(int)) ); + + setFocusProxy( slider ); + + QVBoxLayout * l = new QVBoxLayout( this ); + l->addWidget( lcd, 1 ); + l->addWidget( slider ); + l->addWidget( label ); +} + + +int LCDRange::value() const +{ + return slider->value(); +} + + +const char *LCDRange::text() const +{ + return label->text(); +} + + +void LCDRange::setValue( int value ) +{ + slider->setValue( value ); +} + + +void LCDRange::setRange( int minVal, int maxVal ) +{ + if ( minVal < 0 || maxVal > 99 || minVal > maxVal ) { + qWarning( "LCDRange::setRange(%d,%d)\n" + "\tRange must be 0..99\n" + "\tand minVal must not be greater than maxVal", + minVal, maxVal ); + return; + } + slider->setRange( minVal, maxVal ); +} + + +void LCDRange::setText( const char *s ) +{ + label->setText( s ); +} diff --git a/tutorial/t13/lcdrange.h b/tutorial/t13/lcdrange.h new file mode 100644 index 0000000..2df2686 --- /dev/null +++ b/tutorial/t13/lcdrange.h @@ -0,0 +1,42 @@ +/**************************************************************** +** +** Definition of LCDRange class, Qt tutorial 12 +** +****************************************************************/ + +#ifndef LCDRANGE_H +#define LCDRANGE_H + +#include <qwidget.h> + +class QSlider; +class QLabel; + + +class LCDRange : public QWidget +{ + Q_OBJECT +public: + LCDRange( QWidget *parent=0, const char *name=0 ); + LCDRange( const char *s, QWidget *parent=0, const char *name=0 ); + + int value() const; + const char *text() const; + +public slots: + void setValue( int ); + void setRange( int minVal, int maxVal ); + void setText( const char * ); + +signals: + void valueChanged( int ); + +private: + void init(); + + QSlider *slider; + QLabel *label; +}; + + +#endif // LCDRANGE_H diff --git a/tutorial/t13/main.cpp b/tutorial/t13/main.cpp new file mode 100644 index 0000000..63f4ea7 --- /dev/null +++ b/tutorial/t13/main.cpp @@ -0,0 +1,22 @@ +/**************************************************************** +** +** Qt tutorial 13 +** +****************************************************************/ + +#include <qapplication.h> + +#include "gamebrd.h" + + +int main( int argc, char **argv ) +{ + QApplication::setColorSpec( QApplication::CustomColor ); + QApplication a( argc, argv ); + + GameBoard gb; + gb.setGeometry( 100, 100, 500, 355 ); + a.setMainWidget( &gb ); + gb.show(); + return a.exec(); +} diff --git a/tutorial/t13/t13.pro b/tutorial/t13/t13.pro new file mode 100644 index 0000000..a670da6 --- /dev/null +++ b/tutorial/t13/t13.pro @@ -0,0 +1,13 @@ +TEMPLATE = app +CONFIG += qt warn_on release +HEADERS = cannon.h \ + gamebrd.h \ + lcdrange.h +SOURCES = cannon.cpp \ + gamebrd.cpp \ + lcdrange.cpp \ + main.cpp +TARGET = t13 +REQUIRES=full-config +unix:LIBS += -lm + |