Commit a4dc0fcf authored by Paul Tvete's avatar Paul Tvete

Import tutorial 14 from Qt 1.0

parents
####### This section is automatically generated from
####### /home/hanord/qt/tutorial/Makefile
INCDIR = $(QTDIR)/include
CFLAGS = -O2
LFLAGS = -L$(QTDIR)/lib -lqt
CC = gcc
MOC = moc
####### End of automatically generated section
#
# $Source: /tmp/cvs/qt/tutorial/t14/Makefile,v $
#
####### Files
HEADERS = lcdrange.h cannon.h gamebrd.h
SOURCES = lcdrange.cpp cannon.cpp gamebrd.cpp main.cpp
OBJECTS = lcdrange.o cannon.o gamebrd.o main.o
SRCMETA = mlcdrang.cpp mcannon.cpp mgamebr.cpp
OBJMETA = mlcdrang.o mcannon.o mgamebr.o
TARGET = t14
####### Implicit rules
.SUFFIXES: .cpp
.cpp.o:
$(CC) -c $(CFLAGS) -I$(INCDIR) $<
####### Build rules
all: $(TARGET)
$(TARGET): $(OBJECTS) $(OBJMETA)
$(CC) $(OBJECTS) $(OBJMETA) -o $(TARGET) $(LFLAGS) -lm
depend:
@makedepend -I$(INCDIR) $(SOURCES) 2> /dev/null
showfiles:
@echo $(HEADERS) $(SOURCES) Makefile
clean:
rm -f *.o *.bak *~ *% #*
rm -f $(SRCMETA) $(TARGET)
####### Meta classes
mlcdrang.cpp: lcdrange.h
$(MOC) -o $@ lcdrange.h
mcannon.cpp: cannon.h
$(MOC) -o $@ cannon.h
mgamebr.cpp: gamebrd.h
$(MOC) -o $@ gamebrd.h
# DO NOT DELETE THIS LINE -- make depend depends on it.
/****************************************************************
**
** Implementation CannonField class, Qt tutorial 14
**
****************************************************************/
#include "cannon.h"
#include <qpainter.h>
#include <qpixmap.h>
#include <qdatetm.h>
#include <qfont.h>
#include <math.h>
#include <stdlib.h>
CannonField::CannonField( QWidget *parent, const char *name )
: QWidget( parent, name )
{
ang = 45;
f = 0;
shooting = FALSE;
timerCount = 0;
shoot_ang = 0;
shoot_f = 0;
gameEnded = FALSE;
barrelPressed = FALSE;
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 ( shooting )
return;
timerCount = 0;
shoot_ang = ang;
shoot_f = f;
shooting = TRUE;
startTimer( 50 );
}
void CannonField::newTarget()
{
static bool first_time = TRUE;
if ( first_time ) {
first_time = FALSE;
QTime midnight( 0, 0, 0 );
srand( midnight.secsTo(QTime::currentTime()) );
}
erase( targetRect() );
target = QPoint( 200 + rand() % 190,
10 + rand() % 255 );
repaint( targetRect() );
}
void CannonField::setGameOver()
{
if ( gameEnded )
return;
if ( shooting )
stopShooting();
gameEnded = TRUE;
repaint();
}
void CannonField::restartGame()
{
if ( shooting )
stopShooting();
gameEnded = FALSE;
repaint();
}
void CannonField::timerEvent( QTimerEvent * )
{
erase( shotRect() );
timerCount++;
QRect shotR = shotRect();
if ( shotR.intersects( targetRect() ) ) {
stopShooting();
emit hit();
return;
}
if ( (shotR.x() > width() || shotR.y() > height()) ||
shotR.intersects(barrierRect()) ) {
stopShooting();
emit missed();
return;
}
repaint( shotR, FALSE );
}
void CannonField::paintEvent( QPaintEvent *e )
{
QRect updateR = e->rect();
QPainter p;
p.begin( this );
if ( updateR.intersects( cannonRect() ) )
paintCannon( &p );
if ( updateR.intersects( barrierRect() ) )
paintBarrier( &p );
if ( gameEnded ) {
p.setPen( black );
p.setFont( QFont( "Courier", 48, QFont::Bold ) );
p.drawText( rect(), AlignCenter, "Game Over" );
} else {
if ( shooting && updateR.intersects( shotRect() ) )
paintShot( &p );
if ( updateR.intersects( targetRect() ) )
paintTarget( &p );
}
p.end();
}
void CannonField::mousePressEvent( QMouseEvent *e )
{
if ( e->button() != LeftButton )
return;
if ( barrelHit( e->pos() ) )
barrelPressed = TRUE;
}
void CannonField::mouseMoveEvent( QMouseEvent *e )
{
if ( !barrelPressed )
return;
QPoint 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( QMouseEvent *e )
{
if ( e->button() == LeftButton )
barrelPressed = FALSE;
}
void CannonField::stopShooting()
{
shooting = FALSE;
killTimers();
}
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() );
}
void CannonField::paintBarrier( QPainter *p )
{
p->setBrush( yellow );
p->setPen( black );
p->drawRect( barrierRect() );
}
const QRect barrel_rect(33, -4, 15, 8);
void CannonField::paintCannon( QPainter *p )
{
QRect cr = cannonRect();
QPixmap pix( cr.size() );
QPainter tmp;
pix.fill( this, cr.topLeft() );
tmp.begin( &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( barrel_rect );
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/0.7;
double radians = shoot_ang*3.14159265/180;
double velx = velocity*cos( radians );
double vely = velocity*sin( radians );
double x0 = ( barrel_rect.right() + 5 )*cos(radians);
double y0 = ( barrel_rect.right() + 5 )*sin(radians);
double x = x0 + velx*time;
double y = y0 + vely*time - 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;
}
QRect CannonField::barrierRect() const
{
return QRect( 145, height() - 100, 15, 100 );
}
bool CannonField::barrelHit( const QPoint &p ) const
{
QWMatrix mtx;
mtx.translate( 0, height() - 1 );
mtx.rotate( -ang );
mtx = mtx.invert();
return barrel_rect.contains( mtx.map(p) );
}
/****************************************************************
**
** Definition of CannonField class, Qt tutorial 14
**
****************************************************************/
#ifndef CANNON_H
#define CANNON_H
#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 { return shooting; }
public slots:
void setAngle( int degrees );
void setForce( int newton );
void shoot();
void newTarget();
void setGameOver();
void restartGame();
signals:
void hit();
void missed();
void angleChanged( int );
void forceChanged( int );
protected:
void timerEvent( QTimerEvent * );
void paintEvent( QPaintEvent * );
void mousePressEvent( QMouseEvent * );
void mouseMoveEvent( QMouseEvent * );
void mouseReleaseEvent( QMouseEvent * );
private:
void stopShooting();
void paintShot( QPainter * );
void paintTarget( QPainter * );
void paintBarrier( QPainter * );
void paintCannon( QPainter * );
QRect cannonRect() const;
QRect shotRect() const;
QRect targetRect() const;
QRect barrierRect() const;
bool barrelHit( const QPoint & ) const;
int ang;
int f;
bool shooting;
int timerCount;
float shoot_ang;
float shoot_f;
QPoint target;
bool gameEnded;
bool barrelPressed;
};
#endif // CANNON_H
/****************************************************************
**
** Implementation of GameBoard class, Qt tutorial 14
**
****************************************************************/
#include "gamebrd.h"
#include <qfont.h>
#include <qapp.h>
#include <qlabel.h>
#include <qaccel.h>
#include <qpushbt.h>
#include <qlcdnum.h>
#include "lcdrange.h"
#include "cannon.h"
GameBoard::GameBoard( QWidget *parent, const char *name )
: QWidget( parent, name )
{
setMinimumSize( 500, 355 );
quit = new QPushButton( "Quit", this, "quit" );
quit->setFont( QFont( "Times", 18, QFont::Bold ) );
connect( quit, SIGNAL(clicked()), qApp, SLOT(quit()) );
angle = new LCDRange( "ANGLE", this, "angle" );
angle->setRange( 5, 70 );
force = new LCDRange( "FORCE", this, "force" );
force->setRange( 10, 50 );
frame = new QFrame( this, "cannonFrame" );
frame->setFrameStyle( QFrame::WinPanel | QFrame::Sunken );
cannonField = new CannonField( this, "cannonField" );
cannonField->setBackgroundColor( QColor( 250, 250, 200) );
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()),SLOT(hit()) );
connect( cannonField, SIGNAL(missed()),SLOT(missed()) );
angle->setValue( 60 );
force->setValue( 25 );
shoot = new QPushButton( "Shoot", this, "shoot" );
shoot->setFont( QFont( "Times", 18, QFont::Bold ) );
connect( shoot, SIGNAL(clicked()), SLOT(fire()) );
restart = new QPushButton( "New Game", this, "newgame" );
restart->setFont( QFont( "Times", 18, QFont::Bold ) );
connect( restart, SIGNAL(clicked()), 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" );
QAccel *accel = new QAccel( this );
accel->connectItem( accel->insertItem( Key_Space), this, SLOT(fire()) );
accel->connectItem( accel->insertItem( Key_Q), qApp, SLOT(quit()) );
quit->setGeometry( 10, 10, 75, 30 );
angle->setGeometry( 10, quit->y() + quit->height() + 10, 75, 130 );
force->setGeometry( 10, angle->y() + angle->height() + 10, 75, 130 );
frame->move( angle->x() + angle->width() + 10, angle->y() );
cannonField->move( frame->x() + 2, frame->y() + 2 );
shoot->setGeometry( 10, 315, 75, 30 );
restart->setGeometry( 380, 10, 110, 30 );
hits->setGeometry( 130, 10, 40, 30 );
hitsL->setGeometry( hits->x() + hits->width() + 5, 10, 60, 30 );
shotsLeft->setGeometry( 240, 10, 40, 30 );
shotsLeftL->setGeometry( shotsLeft->x()+shotsLeft->width()+5, 10, 60, 30 );
newGame();
}
void GameBoard::resizeEvent( QResizeEvent * )
{
frame->resize( width() - frame->x() - 10,
height() - frame->y() - 10 );
cannonField->resize( frame->width() - 4, frame->height() - 4 );
}
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();
}
/****************************************************************
**
** Definition of GameBoard class, Qt tutorial 14
**
****************************************************************/
#ifndef GAMEBRD_H
#define GAMEBRD_H
#include <qwidget.h>
class QPushButton;
class LCDRange;
class QLCDNumber;
class QFrame;
class CannonField;
#include "lcdrange.h"
#include "cannon.h"
class GameBoard : public QWidget
{
Q_OBJECT
public:
GameBoard( QWidget *parent=0, const char *name=0 );
protected:
void resizeEvent( QResizeEvent * );
protected slots:
void fire();
void hit();
void missed();
void newGame();
private:
QPushButton *quit;
QPushButton *shoot;
QPushButton *restart;
LCDRange *angle;
LCDRange *force;
QLCDNumber *hits;
QLCDNumber *shotsLeft;
QFrame *frame;
CannonField *cannonField;
};
#endif // GAMEBRD_H
/****************************************************************
**
** Implementation of LCDRange class, Qt tutorial 14
**
****************************************************************/
#include "lcdrange.h"
#include <qscrbar.h>
#include <qlcdnum.h>
#include <qlabel.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()
{
lcd = new QLCDNumber( 2, this, "lcd" );
lcd->move( 0, 0 );
sBar = new QScrollBar( 0, 99, // range
1, 10, // line/page steps
0, // inital value
QScrollBar::Horizontal, // orientation
this, "scrollbar" );
label = new QLabel( this, "label" );
label->setAlignment( AlignCenter );
connect( sBar, SIGNAL(valueChanged(int)), lcd, SLOT(display(int)) );
connect( sBar, SIGNAL(valueChanged(int)), SIGNAL(valueChanged(int)) );
}
int LCDRange::value() const
{
return sBar->value();
}
const char *LCDRange::text() const
{
return label->text();
}
void LCDRange::setValue( int value )
{
sBar->setValue( value );
}
void LCDRange::setRange( int minVal, int maxVal )
{
if ( minVal < 0 || maxVal > 99 || minVal > maxVal ) {
warning( "LCDRange::setRange(%d,%d)\n"
"\tRange must be 0..99\n"
"\tand minVal must not be greater than maxVal",
minVal, maxVal );
return;
}
sBar->setRange( minVal, maxVal );
}
void LCDRange::setText( const char *s )
{
label->setText( s );
}
void LCDRange::resizeEvent( QResizeEvent * )
{
lcd->resize( width(), height() - 41 - 5 );
sBar->setGeometry( 0, lcd->height() + 5, width(), 16 );
label->setGeometry( 0, lcd->height() + 21, width(), 20 );
}
/****************************************************************
**
** Definition of LCDRange class, Qt tutorial 14
**
****************************************************************/
#ifndef LCDRANGE_H
#define LCDRANGE_H
#include <qwidget.h>
class QScrollBar;
class QLCDNumber;
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 );
protected:
void resizeEvent( QResizeEvent * );
private:
void init();
QScrollBar *sBar;
QLCDNumber *lcd;
QLabel *label;
};
#endif // LCDRANGE_H
/****************************************************************
**
** Qt tutorial 14
**
****************************************************************/
#include <qapp.h>
#include "gamebrd.h"
int main( int argc, char **argv )
{
QApplication::setColorMode( QApplication::CustomColors );
QApplication a( argc, argv );
GameBoard gb;
gb.setGeometry( 100, 100, 500, 355 );
a.setMainWidget( &gb );
gb.show();
return a.exec();
}
#############################################################################
# Generated by tmake at 15:26, 1996/10/10
# Project: t14.p
# Template: e:\tmake\template\win\msvc\qtapp.t
#############################################################################
####### Directories
BASEDIR = $(QTDIR)
INCDIR = $(BASEDIR)\include
LIBDIR = $(BASEDIR)\lib
####### Compiler
CFLAGS = -Zi -nologo
LFLAGS = $(LIBDIR)\qt.lib user32.lib gdi32.lib comdlg32.lib libc.lib
CC = cl
MOC = moc
LINK = link /DEBUG /SUBSYSTEM:windows /NODEFAULTLIB:libcd.lib
####### Files
HEADERS = cannon.h \
gamebrd.h \
lcdrange.h
SOURCES = cannon.cpp \
gamebrd.cpp \
lcdrange.cpp \
main.cpp
OBJECTS = cannon.obj \
gamebrd.obj \
lcdrange.obj \