感謝樓上的大大提供的訊息
我現在是卡在方程式寫不出來
反彈的方程式應該是將原方程式的斜率乗上一個負號吧
可是我方程式是由時間驅動的,乘上負號後球就會直接消失耶= =
請問各位大大我該如何修改我的程式呢?
以下是我的程式碼
#include "bump.h"
#include <qpainter.h>
#include <qpixmap.h>
#include <qtimer.h>
BumpField::BumpField( QWidget *parent, const char *name )
: QWidget( parent, name )
{
v = 1;
m = 0;
timerCount = 0;
autoShootTimer = new QTimer( this, "movement handler" );
connect( autoShootTimer, SIGNAL(timeout()), this, SLOT(moveShot()) );
barrelPressed = FALSE;
setPalette(QPalette( QColor( 250, 250, 250)) );
}
void BumpField::setMove( int value )
{
if ( value < 0 )
value = 0;
if ( value > 720 )
value = 720;
if ( m == value )
return;
m = value;
repaint();
}
void BumpField::moveShot()
{
QRegion r( shotRect() );
timerCount++;
QRect shotR = shotRect();
if ( shotR. intersects(QRect( 0, 10, 790, 1 )) )
v = -1;
else
r = r.unite( QRegion( shotR ) );
repaint( r );
}
void BumpField::paintEvent( QPaintEvent *e )
{
QRect updateR = e->rect();
QPainter p( this );
paintCannon( &p );
paintWallright( &p );
paintWallleft( &p );
paintWallup( &p );
paintShoot( &p );
}
void BumpField::paintWallright( QPainter *p )
{
p->setBrush( black );
p->setPen( NoPen );
p->drawRect( QRect( -1, 0, 1, 520 ) );
}
void BumpField::paintWallleft( QPainter *p )
{
p->setBrush( black );
p->setPen( NoPen );
p->drawRect( QRect( 770, 0, 1, 520 ) );
}
void BumpField::paintWallup( QPainter *p )
{
p->setBrush( black );
p->setPen( NoPen );
p->drawRect( QRect( 0, 10, 790, 1 ) );
}
void BumpField::paintShoot( QPainter *p )
{
p->setBrush( black );
p->setPen( NoPen );
p->drawRect( shotRect() );
}
void BumpField::paintCannon( QPainter *p )
{
QPainter b( this );
b.setBrush( blue );
b.setPen( NoPen );
b.translate( m, rect().bottom() );
b.drawRect( QRect( 0, -10, 60, 8 ) );
}
void BumpField::mousePressEvent( QMouseEvent *e )
{
if ( e->button() != LeftButton )
return;
if ( barrelHit(e->pos()) )
barrelPressed = TRUE;
autoShootTimer->start( 5 );
}
void BumpField::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 );
setMove( pnt.x() );
}
void BumpField::mouseReleaseEvent( QMouseEvent *e )
{
if (e->button() == LeftButton)
barrelPressed = FALSE;
}
bool BumpField::barrelHit( const QPoint &p ) const
{
QWMatrix mtx;
mtx.translate( m, height() - 1 );
mtx = mtx.invert();
return QRect( 0, -10, 60, 8 ).contains( mtx.map(p) );
}
QRect BumpField::shotRect() const
{
double x = timerCount;
double y = v * x ; /* 此段該如何修改呢? */
QRect r = QRect( 0, 0, 6, 6 );
r.moveCenter( QPoint( qRound(x), height() - 1 -qRound(y) ) );
return r;
}
QSizePolicy BumpField::sizePolicy() const
{
return QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
}