#ifndef TANK_H#define TANK_H#include"wanwu.h"#include"main.h"#include"gamemap.h"class Tank : public Wanwu{protected: int steps[8]={4,8,8,16,16,32,32,64}; float lifes[8]={200,400,600,800,900,1100,1300,1500}; float wulis[8]={20,40,80,160,160,320,320,640}; float fashus[8]={20,40,80,160,160,320,320,640}; float hujias[8]={50,70,90,110,130,150,170,190}; float mokangs[8]={50,70,90,110,130,150,170,190}; int gongjijianges[8]={8,7,6,5,4,3,2,2}; int group;public: Tank(); Tank(int iIndex,int jIndex,Dir dir=UP,int style=0,int group=1); virtual void CalculateSphere(); void Display(QPainter &paint); void Move(); void startmove(){ismove=true;} void stopmove(){ismove=false;} void setdir(Dir dir){m_dir=dir;}private: int style; bool ismove; int gongjijiange;};#endif // TANK_H
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
tank.cpp
#include "tank.h"Tank::Tank(){ this->m_pos.setX(10*CELLWIDTH+CELLWIDTH/2); this->m_pos.setY(8*CELLHEIGHT+CELLHEIGHT/2); this->wuli=200; this->group=0; m_step=16; gongjijiange=3; this->style=0; ismove=false; m_dir=UP; m_bDisappear=false; life=1000; CalculateSphere();}Tank::Tank(int iIndex,int jIndex,Dir dir,int style,int group){ this->m_pos.setX(jIndex*CELLWIDTH+CELLWIDTH/2); this->m_pos.setY(iIndex*CELLHEIGHT+CELLHEIGHT/2); this->m_dir=dir;z this->style=style; this->group=group; wuli=wulis[style]; fashu=fashus[style]; hujia=hujias[style]; mokang=mokangs[style]; m_step=steps[style]; gongjijiange=gongjijianges[style]; m_bDisappear=false; life=lifes[style]; CalculateSphere();}void Tank::Display(QPainter &paint){ QRect xm_rectSphere=m_rectSphere; if(m_bDisappear)return; switch(m_dir){ case UP: paint.drawImage(m_rectSphere,*glo.tankimage,QRect(2*PICTANKWIDTH,style*2*PICTANKHEIGHT,PICTANKWIDTH,PICTANKHEIGHT)); paint.drawImage(xm_rectSphere,*glo.tankimage,QRect(2*PICTANKWIDTH,(style*2+1)*PICTANKHEIGHT,PICTANKWIDTH,PICTANKHEIGHT)); break; case DOWN: paint.drawImage(m_rectSphere,*glo.tankimage,QRect(0*PICTANKWIDTH,style*2*PICTANKHEIGHT,PICTANKWIDTH,PICTANKHEIGHT)); paint.drawImage(xm_rectSphere,*glo.tankimage,QRect(0*PICTANKWIDTH,(style*2+1)*PICTANKHEIGHT,PICTANKWIDTH,PICTANKHEIGHT)); break; case LEFT: paint.drawImage(m_rectSphere,*glo.tankimage,QRect(1*PICTANKWIDTH,style*2*PICTANKHEIGHT,PICTANKWIDTH,PICTANKHEIGHT)); paint.drawImage(xm_rectSphere,*glo.tankimage,QRect(1*PICTANKWIDTH,(style*2+1)*PICTANKHEIGHT,PICTANKWIDTH,PICTANKHEIGHT)); break; case RIGHT: paint.drawImage(m_rectSphere,*glo.tankimage,QRect(3*PICTANKWIDTH,style*2*PICTANKHEIGHT,PICTANKWIDTH,PICTANKHEIGHT)); paint.drawImage(xm_rectSphere,*glo.tankimage,QRect(3*PICTANKWIDTH,(style*2+1)*PICTANKHEIGHT,PICTANKWIDTH,PICTANKHEIGHT)); break; }}void Tank::Move(){ if(ismove==true){ switch(m_dir){ case UP: m_pos.setY(m_pos.y()-m_step); break; case DOWN: m_pos.setY(m_pos.y()+m_step); break; case LEFT: m_pos.setX(m_pos.x()-m_step); break; case RIGHT: m_pos.setX(m_pos.x()+m_step); break; } CalculateSphere(); qDebug("move on"); }qDebug("move off");}void Tank::CalculateSphere(){this->m_rectSphere.setRect(m_pos.x()-TANKWIDTH/2,m_pos.y()-TANKHEIGHT/2,TANKWIDTH,TANKHEIGHT);}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
main.h结构体Glo中加入选项Tank *player.
class QImage;class GameMap;class Tank;typedef struct{unsigned int framei;QImage *blockimage;GameMap *gamemap;Tank *player;}Glo;
mainwindow.h里加入
#include"tank.h"
mainwindow.cpp构造函数里加入
glo.player=new Tank();
mainwindow.cpp paintEvent函数变更如下
void MainWindow::paintEvent(QPaintEvent *event){ Q_UNUSED(event);setFixedSize(WIDTH,HEIGHT);paint.begin(this);glo.gamemap->Display(paint);glo.player->Display(paint);paint.end();}
mainwindow.cpp keyPressEvent变更如下
void MainWindow::keyPressEvent(QKeyEvent *event){ qDebug("key:--------------------------%d ",event->key()); if(event->key()==Qt::Key_M) { gamestatus=mapedit; }else if(event->key()==Qt::Key_G){ gamestatus=gameing; } if(gamestatus==mapedit){ if(event->key()==Qt::Key_S) { glo.gamemap->savemap("1.dat"); } else if(event->key()==Qt::Key_L) { glo.gamemap->loadmap("1.dat"); } }else if(gamestatus==gameing){ if(event->key()==Qt::Key_S) { glo.player->setdir(DOWN);glo.player->startmove(); } else if(event->key()==Qt::Key_A) { glo.player->setdir(LEFT);glo.player->startmove(); } else if(event->key()==Qt::Key_W) { glo.player->setdir(UP);glo.player->startmove(); } else if(event->key()==Qt::Key_D) { glo.player->setdir(RIGHT);glo.player->startmove(); }else if(event->key()==Qt::Key_J){ glo.player->startfire(); } }update();}