#include "Tank.h"
{
tankDirection=dRight;
tankUp
= new QPixmap(":/CannonBattle/Resources/tank up.jpg");
tankDown
= new QPixmap(":/CannonBattle/Resources/tank down.jpg");
tankRight
=new QPixmap(":/CannonBattle/Resources/tank right.jpg");
tankLeft
= new QPixmap(":/CannonBattle/Resources/tank left.jpg");
if(tankUp->isNull() && tankDown->isNull() && tankRight->isNull()){
emit error
(QString("tank image load fails"));
}
/*
label.setText("dongzhe adfasd fasdfsdafsadfasdfasd");
//label.setMovie(movie);
mo=this->scene()->addWidget(&label);
mo->setPos(50,50);
*/
}
Tank::~Tank()
{
}
if(tankDirection==dRight){
return tankRight;
}else if(tankDirection== dLeft){
return tankLeft;
}else if(tankDirection== dUp){
return tankUp;
}else{
return tankDown;
}
}
QRectF Tank
::boundingRect() const{
//increase the boudingRect to get rid of marks
return QRectF(getImage
()->rect
().
x(),getImage
()->rect
().
y(),getImage
()->rect
().
width(),getImage
()->rect
().
height()+2);
//return getImage()->rect();
}
painter
->drawPixmap
(QPoint(0,
0),
*getImage
());
//painter->drawImage(QPoint(0,0),QImage(":/CannonBattle/Resources/explose.gif"));
}
int Tank::type() const {
return TANK_TYPE;
}
[/QTCLASS]
here is move tank
[QTCLASS]
void CannonField::tankMove(){
#if _DEBUG
int a;
int b;
timeTemp.start();
//timeTemp =QTime::currentTime();
#endif
tankCurrentY = mainTank->pos().y();
tankCurrentX= mainTank->pos().x();
if(mainTank->tankDirection==mainTank->dUp){
mainTank->setPos(tankCurrentX, tankCurrentY-tankSpeed);
}
if(mainTank->tankDirection==mainTank->dDown){
mainTank->setPos(tankCurrentX, tankCurrentY+tankSpeed);
}
if(mainTank->tankDirection==mainTank->dLeft){
mainTank->setPos(tankCurrentX-tankSpeed,tankCurrentY);
}
if(mainTank->tankDirection==mainTank->dRight){
mainTank->setPos(tankCurrentX+tankSpeed,tankCurrentY);
}
tankCurrentY = mainTank->pos().y();
tankCurrentX= mainTank->pos().x();
if (tankCurrentX <0 || tankCurrentY<0 || tankCurrentX > (scene.width()-mainTank->boundingRect().width()) || tankCurrentY > (scene.height()-mainTank->boundingRect().height())) {
mainTank->setPos(oldPoint);
}
QList<QGraphicsItem *> collisions = mainTank->collidingItems(Qt::IntersectsItemBoundingRect);
if(!collisions.isEmpty()){
QListIterator<QGraphicsItem *> it(collisions);
while(it.hasNext()){
temp= it.next();
if(temp->type()==TARGET_TYPE){
mainTank->setPos(oldPoint);
}
}
}
//graphicsView.centerOn(mainTank);
graphicsView.ensureVisible(mainTank,graphicsView.rect().width()/3,graphicsView.rect().height()/3);
#if _DEBUG
//for(int i=0;i<1000000;i++)
//{i++;}
//b=QTime::currentTime().msecsTo(timeTemp);
a= timeTemp.elapsed();
qDebug("%d\n",a);
#endif
}
#include "Tank.h"
Tank::Tank(QObject *parent,QGraphicsScene *scene)
: QObject(parent), QGraphicsItem(0,scene)
{
tankDirection=dRight;
tankUp = new QPixmap(":/CannonBattle/Resources/tank up.jpg");
tankDown= new QPixmap(":/CannonBattle/Resources/tank down.jpg");
tankRight =new QPixmap(":/CannonBattle/Resources/tank right.jpg");
tankLeft = new QPixmap(":/CannonBattle/Resources/tank left.jpg");
if(tankUp->isNull() && tankDown->isNull() && tankRight->isNull()){
emit error(QString("tank image load fails"));
}
/*
label.setText("dongzhe adfasd fasdfsdafsadfasdfasd");
//label.setMovie(movie);
mo=this->scene()->addWidget(&label);
mo->setPos(50,50);
*/
this->setCacheMode(QGraphicsItem::DeviceCoordinateCache);
}
Tank::~Tank()
{
}
QPixmap* Tank::getImage() const{
if(tankDirection==dRight){
return tankRight;
}else if(tankDirection== dLeft){
return tankLeft;
}else if(tankDirection== dUp){
return tankUp;
}else{
return tankDown;
}
}
QRectF Tank::boundingRect() const{
//increase the boudingRect to get rid of marks
return QRectF(getImage()->rect().x(),getImage()->rect().y(),getImage()->rect().width(),getImage()->rect().height()+2);
//return getImage()->rect();
}
void Tank::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
painter->drawPixmap(QPoint(0,0),*getImage());
//painter->drawImage(QPoint(0,0),QImage(":/CannonBattle/Resources/explose.gif"));
}
int Tank::type() const {
return TANK_TYPE;
}
[/QTCLASS]
here is move tank
[QTCLASS]
void CannonField::tankMove(){
#if _DEBUG
int a;
int b;
timeTemp.start();
//timeTemp =QTime::currentTime();
#endif
QPointF oldPoint(mainTank->pos());
tankCurrentY = mainTank->pos().y();
tankCurrentX= mainTank->pos().x();
if(mainTank->tankDirection==mainTank->dUp){
mainTank->setPos(tankCurrentX, tankCurrentY-tankSpeed);
}
if(mainTank->tankDirection==mainTank->dDown){
mainTank->setPos(tankCurrentX, tankCurrentY+tankSpeed);
}
if(mainTank->tankDirection==mainTank->dLeft){
mainTank->setPos(tankCurrentX-tankSpeed,tankCurrentY);
}
if(mainTank->tankDirection==mainTank->dRight){
mainTank->setPos(tankCurrentX+tankSpeed,tankCurrentY);
}
tankCurrentY = mainTank->pos().y();
tankCurrentX= mainTank->pos().x();
if (tankCurrentX <0 || tankCurrentY<0 || tankCurrentX > (scene.width()-mainTank->boundingRect().width()) || tankCurrentY > (scene.height()-mainTank->boundingRect().height())) {
mainTank->setPos(oldPoint);
}
QList<QGraphicsItem *> collisions = mainTank->collidingItems(Qt::IntersectsItemBoundingRect);
if(!collisions.isEmpty()){
QGraphicsItem *temp;
QListIterator<QGraphicsItem *> it(collisions);
while(it.hasNext()){
temp= it.next();
if(temp->type()==TARGET_TYPE){
mainTank->setPos(oldPoint);
}
}
}
//graphicsView.centerOn(mainTank);
graphicsView.ensureVisible(mainTank,graphicsView.rect().width()/3,graphicsView.rect().height()/3);
#if _DEBUG
//for(int i=0;i<1000000;i++)
//{i++;}
//b=QTime::currentTime().msecsTo(timeTemp);
a= timeTemp.elapsed();
qDebug("%d\n",a);
#endif
}
To copy to clipboard, switch view to plain text mode
Bookmarks