#include "tank.h"
#include "ui_tank.h"
#include "QDebug"
Tank::Tank(QWidget *parent) :
QWidget(parent),
ui(new Ui::Tank)
{
ui->setupUi(this);
setAttribute(Qt::WA_DeleteOnClose);
setFixedSize(WIDTH,HEIGHT);
setWindowTitle("坦克大战");
setWindowIcon(QIcon((rootdir+"pic//icon.png").c_str()));
setStyleSheet("background-color:black;");
campRect.setRect(12*BASESIZE,24*BASESIZE,SIZE,SIZE); //司令部的位置
//加载图像
bg_gray.load((rootdir+"pic\\bg_gray.gif").c_str());
bg_gray=resizePic(bg_gray,SIZE,SIZE);
grass.load((rootdir+"pic\\forest.gif").c_str());
grass = resizePic(grass,BASESIZE,BASESIZE);
brick.load((rootdir+"pic\\wall.gif").c_str());
brick = resizePic(brick,BASESIZE,BASESIZE);
iron.load((rootdir+"pic\\stone.gif").c_str());
iron = resizePic(iron,BASESIZE,BASESIZE);
water.load((rootdir+"pic\\river-0.gif").c_str());
water = resizePic(water,BASESIZE,BASESIZE);
ice.load((rootdir+"pic\\ice.gif").c_str());
ice = resizePic(ice,BASESIZE,BASESIZE);
timer1 = new QTimer(this);
timer2 = new QTimer(this);
timer3 = new QTimer(this);
timer4 = new QTimer(this);
timer5 = new QTimer(this);
connect(timer1,&QTimer::timeout,this,&Tank::play);
connect(timer2,&QTimer::timeout,this,&Tank::refresh);
connect(timer3,&QTimer::timeout,this,&Tank::enemyMove);
connect(timer4,&QTimer::timeout,this,&Tank::bulletMove);
connect(timer5,&QTimer::timeout,this,&Tank::enemyShot);
gate =2;
initMap();
}
void Tank::keyPressEvent(QKeyEvent *event)
{
if(start>0)
{
return;
}
if(event->key()==Qt::Key::Key_W)
{
role1.setDir(Direct::up);
}
else if(event->key()==Qt::Key::Key_S)
{
role1.setDir(Direct::down);
}
else if(event->key()==Qt::Key::Key_A)
{
role1.setDir(Direct::left1);
}
else if(event->key()==Qt::Key::Key_D)
{
role1.setDir(Direct::right1);
}
else if(event->key()==Qt::Key::Key_J)
{
if(!role1.bullet.getActive())
{
qDebug() << "玩家发射子弹";
role1.shot();
QSound::play((rootdir+"\\sound\\shoot.wav").c_str());
}
}
}
void Tank::keyReleaseEvent(QKeyEvent *event)
{
if(event->key()==Qt::Key::Key_J)
{
return;
}
else
{
role1.ismove=false;
}
}
void Tank::play()
{
//玩家移动
if(role1.ismove)
{
role1.move();
QSound::play((rootdir+"\\sound\\move.wav").c_str());
}
}
void Tank::enemyMove()
{
//敌方坦克移动
static int d;
srand((unsigned)time(NULL));
for (auto& enemy:enemies)//使用引用类型 ,不然不是同一个对象
{
//随机方向移动
d = rand()%8;
if(d==0)
{
enemy.setDir(Direct::left1);
}
else if(d == 1)
{
enemy.setDir(Direct::up);
}
else if(d == 2)
{
enemy.setDir(Direct::right1);
}
else if(d == 3 || d== 4 || d == 5 || d == 6 || d == 7)
{
enemy.setDir(Direct::down);
}
enemy.move();
}
}
void Tank::enemyShot()
{
for(auto& enemy: enemies)
{
if(!enemy.bullet.getActive())
{
enemy.shot();
}
}
}
void Tank::bulletMove()
{
//玩家子弹移动
if(role1.bullet.getActive())
{
role1.bullet.move();
}
//敌人子弹移动
for(auto& enemy:enemies)
{
if(enemy.bullet.getActive())
{
enemy.bullet.move();
}
}
}
void Tank::refresh()
{
collisionCheck();
if(enemyNum<=0)
{
nextGate();
}
update();
}
void Tank::nextGate()
{
if(gate==35)
{
QMessageBox msg(QMessageBox::Icon(QMessageBox::Icon::Information),"Victory","恭喜你通过全部关卡");
msg.exec();
exit(1);
}
gate++;
initMap();
}
void Tank::collisionCheck()
{
//玩家子弹和敌方子弹碰撞检测
for(auto& enemy:enemies)
{
if(true==role1.bullet.rect.intersects(enemy.bullet.rect))
{
role1.bullet.setActive(false);
enemy.bullet.setActive(false);
break;
}
else if(true==campRect.intersects(enemy.bullet.rect)||true==campRect.intersects(role1.bullet.rect))
{
QSound::play((rootdir+"\\sound\\player_bomb.wav").c_str());
camp.load((rootdir+"pic\\camp1.gif").c_str());
camp = resizePic(camp,SIZE,SIZE);
update();
gameOver();
}
}
//玩家子弹和敌方坦克碰撞检测
for(auto enemy=enemies.begin();enemy!=enemies.end();enemy++)
{
if(true==role1.bullet.rect.intersects(enemy->rect))
{
QSound::play((rootdir+"\\sound\\enemy-bomb.wav").c_str());
enemies.erase(enemy);//需要重载 == 操作符
enemyNum--;
if(enemyNum<=0)
{
return;
}
createEnemy();
role1.bullet.setActive(false);
break;
}
}
//玩家和敌方子弹碰撞检测
for(auto& enemy:enemies)
{
if(true==role1.rect.intersects(enemy.bullet.rect))
{
enemy.bullet.setActive(false);
createPlayer();
QSound::play((rootdir+"\\sound\\player_bomb.wav").c_str());
life--;
if(life<=0)
{
gameOver();
}
break;
}
}
}
void Tank::initMap()
{
//加载地图
loadMap();
camp.load((rootdir+"pic\\camp0.gif").c_str());
camp = resizePic(camp,SIZE,SIZE);
QSound::play((rootdir+"sound\\start.wav").c_str());
//创建敌人
cursor=0;
enemyNum = 10;
for(int i=0;i<4;i++)
{
createEnemy();
}
//创建玩家
createPlayer();
life = 3;
// 开始游戏
start = 10;
timer1->start(100);//玩家移动速度
timer2->start(30);//检查碰撞 游戏是否结束
timer3->start(120); // 敌坦克移动
timer4->start(60); // 子弹移动
timer5->start(1500);// 敌坦克自动发射子弹
}
void Tank:: createEnemy()
{
if(enemyNum<4)
return;
Enemy enemy;
enemy.rect.setRect(cursor*SIZE,0,SIZE,SIZE);
enemies.push_back(enemy);
cursor+=6;
cursor%=18;
}
void Tank::createPlayer()
{
role1.bullet.setActive(false);
role1.setDir(Direct::up);
role1.ismove=false;
role1.rect.setRect(9*BASESIZE,24*BASESIZE,SIZE,SIZE);
}
void Tank::loadMap()
{
std::ifstream file;
try
{
file.open((QCoreApplication::applicationDirPath()+"\\res\\map.dat").toStdString().c_str(),std::ios::in|std::ios::binary);
if(!file)
{
throw "无法加载地图,请确保可执行文件所在目录的res目录下有map.dat文件,并且该游戏不放在中文目录下";
}
}
catch(const char *err)
{
QMessageBox msg(QMessageBox::Icon(QMessageBox::Icon::Critical),"ERROR",err);
msg.exec();
exit(-1);
}
file.seekg(sizeof(map)*(gate-1));
file.read(*map,sizeof(map));
file.close();
}
void Tank::drawStart(){
for(int i=0;i<15;i++)
{
for(int j=0;j<15;j++)
{
paint.drawPixmap(i*SIZE,j*SIZE,bg_gray);
}
}
paint.setFont(QFont("宋体",24));
paint.drawText(12*BASESIZE+10,13*BASESIZE+10,"第"+QString::number(gate)+"关");
}
void Tank::paintEvent(QPaintEvent *)
{
paint.begin(this);
//画关卡过渡界面
if(0<start--)
{
drawStart();
paint.end();