summaryrefslogtreecommitdiffstats
path: root/tutorial/t14
diff options
context:
space:
mode:
Diffstat (limited to 'tutorial/t14')
-rw-r--r--tutorial/t14/cannon.cpp292
-rw-r--r--tutorial/t14/cannon.h79
-rw-r--r--tutorial/t14/gamebrd.cpp143
-rw-r--r--tutorial/t14/gamebrd.h40
-rw-r--r--tutorial/t14/lcdrange.cpp88
-rw-r--r--tutorial/t14/lcdrange.h42
-rw-r--r--tutorial/t14/main.cpp22
-rw-r--r--tutorial/t14/t14.pro12
8 files changed, 718 insertions, 0 deletions
diff --git a/tutorial/t14/cannon.cpp b/tutorial/t14/cannon.cpp
new file mode 100644
index 000000000..bf9d7e49d
--- /dev/null
+++ b/tutorial/t14/cannon.cpp
@@ -0,0 +1,292 @@
+/****************************************************************
+**
+** Implementation CannonField class, TQt tutorial 14
+**
+****************************************************************/
+
+#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;
+ barrelPressed = 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() ||
+ shotR.intersects(barrierRect()) ) {
+ autoShootTimer->stop();
+ emit missed();
+ emit canShoot( TRUE );
+ } else {
+ r = r.unite( TQRegion( shotR ) );
+ }
+
+ repaint( r );
+}
+
+
+void CannonField::mousePressEvent( TQMouseEvent *e )
+{
+ if ( e->button() != LeftButton )
+ return;
+ if ( barrelHit( e->pos() ) )
+ barrelPressed = TRUE;
+}
+
+
+void CannonField::mouseMoveEvent( TQMouseEvent *e )
+{
+ if ( !barrelPressed )
+ return;
+ TQPoint pnt = e->pos();
+ if ( pnt.x() <= 0 )
+ pnt.setX( 1 );
+ if ( pnt.y() >= height() )
+ pnt.setY( height() - 1 );
+ double rad = atan(((double)rect().bottom()-pnt.y())/pnt.x());
+ setAngle( qRound ( rad*180/3.14159265 ) );
+}
+
+
+void CannonField::mouseReleaseEvent( TQMouseEvent *e )
+{
+ if ( e->button() == LeftButton )
+ barrelPressed = FALSE;
+}
+
+
+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 ( updateR.intersects( barrierRect() ) )
+ paintBarrier( &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() );
+}
+
+void CannonField::paintBarrier( TQPainter *p )
+{
+ p->setBrush( yellow );
+ p->setPen( black );
+ p->drawRect( barrierRect() );
+}
+
+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;
+}
+
+
+TQRect CannonField::barrierRect() const
+{
+ return TQRect( 145, height() - 100, 15, 100 );
+}
+
+
+bool CannonField::barrelHit( const TQPoint &p ) const
+{
+ TQWMatrix mtx;
+ mtx.translate( 0, height() - 1 );
+ mtx.rotate( -ang );
+ mtx = mtx.invert();
+ return barrelRect.contains( mtx.map(p) );
+}
+
+
+bool CannonField::isShooting() const
+{
+ return autoShootTimer->isActive();
+}
+
+
+TQSize CannonField::sizeHint() const
+{
+ return TQSize( 400, 300 );
+}
+
+
+TQSizePolicy CannonField::sizePolicy() const
+{
+ return TQSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Expanding );
+}
diff --git a/tutorial/t14/cannon.h b/tutorial/t14/cannon.h
new file mode 100644
index 000000000..21c4aecc2
--- /dev/null
+++ b/tutorial/t14/cannon.h
@@ -0,0 +1,79 @@
+/****************************************************************
+**
+** Definition of CannonField class, TQt tutorial 14
+**
+****************************************************************/
+
+#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;
+ TQSize sizeHint() 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 * );
+ void mousePressEvent( TQMouseEvent * );
+ void mouseMoveEvent( TQMouseEvent * );
+ void mouseReleaseEvent( TQMouseEvent * );
+
+private:
+ void paintShot( TQPainter * );
+ void paintTarget( TQPainter * );
+ void paintBarrier( TQPainter * );
+ void paintCannon( TQPainter * );
+ TQRect cannonRect() const;
+ TQRect shotRect() const;
+ TQRect targetRect() const;
+ TQRect barrierRect() const;
+ bool barrelHit( const TQPoint & ) const;
+
+ int ang;
+ int f;
+
+ int timerCount;
+ TQTimer * autoShootTimer;
+ float shoot_ang;
+ float shoot_f;
+
+ TQPoint target;
+
+ bool gameEnded;
+ bool barrelPressed;
+};
+
+
+#endif // CANNON_H
diff --git a/tutorial/t14/gamebrd.cpp b/tutorial/t14/gamebrd.cpp
new file mode 100644
index 000000000..2570eb423
--- /dev/null
+++ b/tutorial/t14/gamebrd.cpp
@@ -0,0 +1,143 @@
+/****************************************************************
+**
+** Implementation of GameBoard class, TQt tutorial 14
+**
+****************************************************************/
+
+#include "gamebrd.h"
+
+#include <qfont.h>
+#include <qapplication.h>
+#include <qlabel.h>
+#include <qaccel.h>
+#include <qpushbutton.h>
+#include <qlcdnumber.h>
+#include <qlayout.h>
+#include <qvbox.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 );
+
+ TQVBox *box = new TQVBox( this, "cannonFrame" );
+ box->setFrameStyle( TQFrame::WinPanel | TQFrame::Sunken );
+
+ cannonField = new CannonField( box, "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" );
+
+ TQAccel *accel = new TQAccel( this );
+ accel->connectItem( accel->insertItem( Key_Enter ),
+ this, SLOT(fire()) );
+ accel->connectItem( accel->insertItem( Key_Return ),
+ this, SLOT(fire()) );
+ accel->connectItem( accel->insertItem( CTRL+Key_Q ),
+ qApp, SLOT(tquit()) );
+
+ TQGridLayout *grid = new TQGridLayout( this, 2, 2, 10 );
+ grid->addWidget( tquit, 0, 0 );
+ grid->addWidget( box, 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/t14/gamebrd.h b/tutorial/t14/gamebrd.h
new file mode 100644
index 000000000..4a4306c97
--- /dev/null
+++ b/tutorial/t14/gamebrd.h
@@ -0,0 +1,40 @@
+/****************************************************************
+**
+** Definition of GameBoard class, TQt tutorial 14
+**
+****************************************************************/
+
+#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/t14/lcdrange.cpp b/tutorial/t14/lcdrange.cpp
new file mode 100644
index 000000000..aff272af6
--- /dev/null
+++ b/tutorial/t14/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/t14/lcdrange.h b/tutorial/t14/lcdrange.h
new file mode 100644
index 000000000..874dd169a
--- /dev/null
+++ b/tutorial/t14/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/t14/main.cpp b/tutorial/t14/main.cpp
new file mode 100644
index 000000000..5f2b439ea
--- /dev/null
+++ b/tutorial/t14/main.cpp
@@ -0,0 +1,22 @@
+/****************************************************************
+**
+** TQt tutorial 14
+**
+****************************************************************/
+
+#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/t14/t14.pro b/tutorial/t14/t14.pro
new file mode 100644
index 000000000..665b7844f
--- /dev/null
+++ b/tutorial/t14/t14.pro
@@ -0,0 +1,12 @@
+TEMPLATE = app
+CONFIG += qt warn_on release
+HEADERS = cannon.h \
+ gamebrd.h \
+ lcdrange.h
+SOURCES = cannon.cpp \
+ gamebrd.cpp \
+ lcdrange.cpp \
+ main.cpp
+TARGET = t14
+REQUIRES=full-config
+unix:LIBS += -lm