summaryrefslogtreecommitdiffstats
path: root/tutorial/t13
diff options
context:
space:
mode:
Diffstat (limited to 'tutorial/t13')
-rw-r--r--tutorial/t13/cannon.cpp231
-rw-r--r--tutorial/t13/cannon.h71
-rw-r--r--tutorial/t13/gamebrd.cpp129
-rw-r--r--tutorial/t13/gamebrd.h40
-rw-r--r--tutorial/t13/lcdrange.cpp88
-rw-r--r--tutorial/t13/lcdrange.h42
-rw-r--r--tutorial/t13/main.cpp22
-rw-r--r--tutorial/t13/t13.pro13
8 files changed, 636 insertions, 0 deletions
diff --git a/tutorial/t13/cannon.cpp b/tutorial/t13/cannon.cpp
new file mode 100644
index 000000000..b04403107
--- /dev/null
+++ b/tutorial/t13/cannon.cpp
@@ -0,0 +1,231 @@
+/****************************************************************
+**
+** Implementation CannonField class, TQt 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( TQWidget *parent, const char *name )
+ : TQWidget( parent, name )
+{
+ ang = 45;
+ f = 0;
+ timerCount = 0;
+ autoShootTimer = new TQTimer( this, "movement handler" );
+ connect( autoShootTimer, SIGNAL(timeout()),
+ this, SLOT(moveShot()) );
+ shoot_ang = 0;
+ shoot_f = 0;
+ target = TQPoint( 0, 0 );
+ gameEnded = FALSE;
+ setPalette( TQPalette( TQColor( 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;
+ TQTime midnight( 0, 0, 0 );
+ srand( midnight.secsTo(TQTime::currentTime()) );
+ }
+ TQRegion r( targetRect() );
+ target = TQPoint( 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()
+{
+ TQRegion r( shotRect() );
+ timerCount++;
+
+ TQRect 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( TQRegion( shotR ) );
+ }
+
+ repaint( r );
+}
+
+
+void CannonField::paintEvent( TQPaintEvent *e )
+{
+ TQRect updateR = e->rect();
+ TQPainter p( this );
+
+ if ( gameEnded ) {
+ p.setPen( black );
+ p.setFont( TQFont( "Courier", 48, TQFont::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( TQPainter *p )
+{
+ p->setBrush( black );
+ p->setPen( NoPen );
+ p->drawRect( shotRect() );
+}
+
+
+void CannonField::paintTarget( TQPainter *p )
+{
+ p->setBrush( red );
+ p->setPen( black );
+ p->drawRect( targetRect() );
+}
+
+
+const TQRect barrelRect(33, -4, 15, 8);
+
+void CannonField::paintCannon( TQPainter *p )
+{
+ TQRect cr = cannonRect();
+ TQPixmap pix( cr.size() );
+ pix.fill( this, cr.topLeft() );
+
+ TQPainter tmp( &pix );
+ tmp.setBrush( blue );
+ tmp.setPen( NoPen );
+
+ tmp.translate( 0, pix.height() - 1 );
+ tmp.drawPie( TQRect( -35,-35, 70, 70 ), 0, 90*16 );
+ tmp.rotate( -ang );
+ tmp.drawRect( barrelRect );
+ tmp.end();
+
+ p->drawPixmap( cr.topLeft(), pix );
+}
+
+
+TQRect CannonField::cannonRect() const
+{
+ TQRect r( 0, 0, 50, 50 );
+ r.moveBottomLeft( rect().bottomLeft() );
+ return r;
+}
+
+
+TQRect 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;
+
+ TQRect r = TQRect( 0, 0, 6, 6 );
+ r.moveCenter( TQPoint( qRound(x), height() - 1 - qRound(y) ) );
+ return r;
+}
+
+
+TQRect CannonField::targetRect() const
+{
+ TQRect r( 0, 0, 20, 10 );
+ r.moveCenter( TQPoint(target.x(),height() - 1 - target.y()) );
+ return r;
+}
+
+
+bool CannonField::isShooting() const
+{
+ return autoShootTimer->isActive();
+}
+
+
+TQSizePolicy CannonField::sizePolicy() const
+{
+ return TQSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Expanding );
+}
diff --git a/tutorial/t13/cannon.h b/tutorial/t13/cannon.h
new file mode 100644
index 000000000..9d3910ea2
--- /dev/null
+++ b/tutorial/t13/cannon.h
@@ -0,0 +1,71 @@
+/****************************************************************
+**
+** Definition of CannonField class, TQt tutorial 13
+**
+****************************************************************/
+
+#ifndef CANNON_H
+#define CANNON_H
+
+class TQTimer;
+
+
+#include <qwidget.h>
+
+
+class CannonField : public TQWidget
+{
+ Q_OBJECT
+public:
+ CannonField( TQWidget *parent=0, const char *name=0 );
+
+ int angle() const { return ang; }
+ int force() const { return f; }
+ bool gameOver() const { return gameEnded; }
+ bool isShooting() const;
+ TQSizePolicy 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( TQPaintEvent * );
+
+private:
+ void paintShot( TQPainter * );
+ void paintTarget( TQPainter * );
+ void paintCannon( TQPainter * );
+ TQRect cannonRect() const;
+ TQRect shotRect() const;
+ TQRect targetRect() const;
+
+ int ang;
+ int f;
+
+ int timerCount;
+ TQTimer * autoShootTimer;
+ float shoot_ang;
+ float shoot_f;
+
+ TQPoint target;
+
+ bool gameEnded;
+};
+
+
+#endif // CANNON_H
diff --git a/tutorial/t13/gamebrd.cpp b/tutorial/t13/gamebrd.cpp
new file mode 100644
index 000000000..cf15f9ef2
--- /dev/null
+++ b/tutorial/t13/gamebrd.cpp
@@ -0,0 +1,129 @@
+/****************************************************************
+**
+** Implementation of GameBoard class, TQt 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( TQWidget *parent, const char *name )
+ : TQWidget( parent, name )
+{
+ TQPushButton *tquit = new TQPushButton( "&Quit", this, "tquit" );
+ tquit->setFont( TQFont( "Times", 18, TQFont::Bold ) );
+
+ connect( tquit, SIGNAL(clicked()), qApp, SLOT(tquit()) );
+
+ 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()) );
+
+ TQPushButton *shoot = new TQPushButton( "&Shoot", this, "shoot" );
+ shoot->setFont( TQFont( "Times", 18, TQFont::Bold ) );
+
+ connect( shoot, SIGNAL(clicked()), SLOT(fire()) );
+ connect( cannonField, SIGNAL(canShoot(bool)),
+ shoot, SLOT(setEnabled(bool)) );
+
+ TQPushButton *restart
+ = new TQPushButton( "&New Game", this, "newgame" );
+ restart->setFont( TQFont( "Times", 18, TQFont::Bold ) );
+
+ connect( restart, SIGNAL(clicked()), this, SLOT(newGame()) );
+
+ hits = new TQLCDNumber( 2, this, "hits" );
+ shotsLeft = new TQLCDNumber( 2, this, "shotsleft" );
+ TQLabel *hitsL = new TQLabel( "HITS", this, "hitsLabel" );
+ TQLabel *shotsLeftL
+ = new TQLabel( "SHOTS LEFT", this, "shotsleftLabel" );
+
+ TQGridLayout *grid = new TQGridLayout( this, 2, 2, 10 );
+ grid->addWidget( tquit, 0, 0 );
+ grid->addWidget( cannonField, 1, 1 );
+ grid->setColStretch( 1, 10 );
+
+ TQVBoxLayout *leftBox = new TQVBoxLayout;
+ grid->addLayout( leftBox, 1, 0 );
+ leftBox->addWidget( angle );
+ leftBox->addWidget( force );
+
+ TQHBoxLayout *topBox = new TQHBoxLayout;
+ 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 000000000..2d3cafee5
--- /dev/null
+++ b/tutorial/t13/gamebrd.h
@@ -0,0 +1,40 @@
+/****************************************************************
+**
+** Definition of GameBoard class, TQt tutorial 13
+**
+****************************************************************/
+
+#ifndef GAMEBRD_H
+#define GAMEBRD_H
+
+#include <qwidget.h>
+
+class TQPushButton;
+class LCDRange;
+class TQLCDNumber;
+class CannonField;
+
+#include "lcdrange.h"
+#include "cannon.h"
+
+
+class GameBoard : public TQWidget
+{
+ Q_OBJECT
+public:
+ GameBoard( TQWidget *parent=0, const char *name=0 );
+
+protected slots:
+ void fire();
+ void hit();
+ void missed();
+ void newGame();
+
+private:
+ TQLCDNumber *hits;
+ TQLCDNumber *shotsLeft;
+ CannonField *cannonField;
+};
+
+
+#endif // GAMEBRD_H
diff --git a/tutorial/t13/lcdrange.cpp b/tutorial/t13/lcdrange.cpp
new file mode 100644
index 000000000..aff272af6
--- /dev/null
+++ b/tutorial/t13/lcdrange.cpp
@@ -0,0 +1,88 @@
+/****************************************************************
+**
+** Implementation of LCDRange class, TQt tutorial 12
+**
+****************************************************************/
+
+#include "lcdrange.h"
+
+#include <qslider.h>
+#include <qlcdnumber.h>
+#include <qlabel.h>
+#include <qlayout.h>
+
+
+LCDRange::LCDRange( TQWidget *parent, const char *name )
+ : TQWidget( parent, name )
+{
+ init();
+}
+
+
+LCDRange::LCDRange( const char *s, TQWidget *parent, const char *name )
+ : TQWidget( parent, name )
+{
+ init();
+ setText( s );
+}
+
+
+void LCDRange::init()
+{
+ TQLCDNumber *lcd = new TQLCDNumber( 2, this, "lcd" );
+ slider = new TQSlider( Horizontal, this, "slider" );
+ slider->setRange( 0, 99 );
+ slider->setValue( 0 );
+
+ label = new TQLabel( " ", this, "label" );
+ label->setAlignment( AlignCenter );
+
+ connect( slider, SIGNAL(valueChanged(int)),
+ lcd, SLOT(display(int)) );
+ connect( slider, SIGNAL(valueChanged(int)),
+ SIGNAL(valueChanged(int)) );
+
+ setFocusProxy( slider );
+
+ TQVBoxLayout * l = new TQVBoxLayout( 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 000000000..874dd169a
--- /dev/null
+++ b/tutorial/t13/lcdrange.h
@@ -0,0 +1,42 @@
+/****************************************************************
+**
+** Definition of LCDRange class, TQt tutorial 12
+**
+****************************************************************/
+
+#ifndef LCDRANGE_H
+#define LCDRANGE_H
+
+#include <qwidget.h>
+
+class TQSlider;
+class TQLabel;
+
+
+class LCDRange : public TQWidget
+{
+ Q_OBJECT
+public:
+ LCDRange( TQWidget *parent=0, const char *name=0 );
+ LCDRange( const char *s, TQWidget *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();
+
+ TQSlider *slider;
+ TQLabel *label;
+};
+
+
+#endif // LCDRANGE_H
diff --git a/tutorial/t13/main.cpp b/tutorial/t13/main.cpp
new file mode 100644
index 000000000..f7922ece8
--- /dev/null
+++ b/tutorial/t13/main.cpp
@@ -0,0 +1,22 @@
+/****************************************************************
+**
+** TQt tutorial 13
+**
+****************************************************************/
+
+#include <qapplication.h>
+
+#include "gamebrd.h"
+
+
+int main( int argc, char **argv )
+{
+ TQApplication::setColorSpec( TQApplication::CustomColor );
+ TQApplication 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 000000000..a670da605
--- /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
+