标题:
用嵌入式ok6410实现坦克大战源代码
[打印本页]
作者:
christian-gao
时间:
2017-7-20 10:36
标题:
用嵌入式ok6410实现坦克大战源代码
这是我上学期的课程设计,用嵌入式开发板ok6410实现的坦克大战,花费了一周的时间完成,有兴趣的朋友可以看看。
0.png
(61.52 KB, 下载次数: 69)
下载附件
2017-7-20 16:05 上传
所有资料51hei提供下载:
tank.tar.gz
(4.92 MB, 下载次数: 8)
2017-7-20 10:35 上传
点击文件名下载附件
下载积分: 黑币 -5
ok6410源程序如下:
#include "pub.h"
#include "tank.h"
#include <QtGui>
//#include <QKeyEvent>
#include "missile.h"
#include "tankclient.h"
#include "explode.h"
#include "wall.h"
#include <QKeyEvent>
QList<QImage> Tank::tankImgs;
Tank::Tank(int x,int y,int w,int h,TankClient* tc,bool good,int liveValue)
{
this->x=x;
this->y=y;
this->w=w;
this->h=h;
BU=BD=BL=BR=false;
this->tc=tc;
dir=STOP;
ptDir=U;
this->good=good;
live=true;
step=0;
xtemp=ytemp=0;
count=8;
this->liveValue=liveValue;
}
void Tank::drawTank(QPainter &p)
{
if(!live)
{
tc->etanks.removeOne(this);
return ;
}
if(dir!=STOP)
ptDir=dir;
switch(ptDir)
{
case U: p.drawImage(x,y,tankImgs[0]);break;
case D: p.drawImage(x,y,tankImgs[1]);break;
case L: p.drawImage(x,y,tankImgs[2]);break;
case R: p.drawImage(x,y,tankImgs[3]);break;
case LU: p.drawImage(x,y,tankImgs[4]);break;
case LD: p.drawImage(x,y,tankImgs[5]);break;
case RU: p.drawImage(x,y,tankImgs[6]);break;
case RD: p.drawImage(x,y,tankImgs[7]);break;
default:break;
}
p.setPen(Qt::yellow);
p.setBrush(Qt::black);
p.drawRect(x,y,25,5);
p.setBrush(Qt::red);
p.drawRect(x,y,liveValue/4,5);//画坦克生命值
moveTank();
}
void Tank::keyPress(int key)
{
switch(key)
{
case Qt::Key_Up: BU=true;break;
case Qt::Key_Down: BD=true;break;
case Qt::Key_Left: BL=true;break;
case Qt::Key_Right: BR=true;break;
case Qt::Key_Return: fire();break;
case Qt::Key_K:
{
if(count>0)superfire();
count--;
if(count<=0)count=0;
break;
}
case Qt::Key_F2:
{
tc->step=0;
tc->start=!tc->start;
if(!(tc->start))//暂停/继续
{
tc->button[1]->setText(QObject::tr("continue"));
}
break;
}
case Qt::Key_F1:
{
//重启游戏
while(tc->missile.size())
tc->missile.removeOne(tc->missile[0]);
while(tc->etanks.size())
tc->etanks.removeOne(tc->etanks[0]);
while(tc->myWall.size())
tc->myWall.removeOne(tc->myWall[0]);
tc->AllObject();
tc->start=true;
tc->gameover=false;
break;
}
case Qt::Key_Escape:exit(0);break;//退出游戏
default:break;
}
if(BU&&!BD&&!BL&&!BR)dir=U;
else if(!BU&&BD&&!BL&&!BR)dir=D;
else if(!BU&&!BD&&BL&&!BR)dir=L;
else if(!BU&&!BD&&!BL&&BR)dir=R;
else if(BU&&!BD&&BL&&!BR)dir=LU;
else if(!BU&&BD&&BL&&!BR)dir=LD;
else if(BU&&!BD&&!BL&&BR)dir=RU;
else if(!BU&&BD&&!BL&&BR)dir=RD;
else dir=STOP;
}
void Tank::keyRelease(int key)
{
switch(key)
{
case Qt::Key_Up: BU=false;break;
case Qt::Key_Down: BD=false;break;
case Qt::Key_Left: BL=false;break;
case Qt::Key_Right: BR=false;break;
default:break;
}
if(BU&&!BD&&!BL&&!BR)dir=U;
else if(!BU&&BD&&!BL&&!BR)dir=D;
else if(!BU&&!BD&&BL&&!BR)dir=L;
else if(!BU&&!BD&&!BL&&BR)dir=R;
else if(BU&&!BD&&BL&&!BR)dir=LU;
else if(!BU&&BD&&BL&&!BR)dir=LD;
else if(BU&&!BD&&!BL&&BR)dir=RU;
else if(!BU&&BD&&!BL&&BR)dir=RD;
else dir=STOP;
}
void Tank::moveTank()
{
xtemp=x;
ytemp=y;
switch(dir)
{
case U: y-=5;break;
case D: y+=5;break;
case L: x-=5;break;
case R: x+=5;break;
case LU: x-=5;y-=5;break;
case LD: x-=5;y+=5;break;
case RU: x+=5;y-=5;break;
case RD: x+=5;y+=5;break;
default:break;
}
if(x<=0)x=0;
if(y<=0)y=0;
if(x>=TankClient::GAME_WIDTH-w)x=TankClient::GAME_WIDTH-w;
if(y>=TankClient::GAME_HEIGHT-h)y=TankClient::GAME_HEIGHT-h;
if(!good)
{
if(qrand()%20>18)fire();
if(step==0)
{
dir=Dir(qrand()%9);
step=qrand()%15+10;
}
step--;
}
}
void Tank::fire()
{
int mw=10;
int mh=10;
int mx=x+w/2-mw/2;
int my=y+h/2-mh/2;
Missile* m=new Missile(mx,my,mw,mh,ptDir,tc,good);
tc->missile.push_back(m);
}
void Tank::fire(Dir tdir)
{
int mw=10;
int mh=10;
int mx=x+w/2-mw/2;
int my=y+h/2-mh/2;
Missile* m=new Missile(mx,my,mw,mh,tdir,tc,good);
tc->missile.push_back(m);
}
void Tank::superfire()//朝八个方向开火
{
for(int i=0;i<8;i++)
fire(Dir(i));
}
bool Tank::TankHitWall(Wall* w) //检测坦克和墙壁是否碰撞
{
if(live&&getRect().intersects(w->getRect()))
{
x=xtemp;//回到上一次的位置
y=ytemp;
return true;
}
return false;
}
void Tank::TankHitWalls(QList<Wall*> ws)
{
for(int i=0;i<ws.size();i++)
{
if(TankHitWall(ws[i]))
return;
}
}
void Tank::init()
{
tankImgs.push_back(QImage(":/images/etU.gif"));
tankImgs.push_back(QImage(":/images/etD.gif"));
tankImgs.push_back(QImage(":/images/etL.gif"));
tankImgs.push_back(QImage(":/images/etR.gif"));
tankImgs.push_back(QImage(":/images/etLU.gif"));
tankImgs.push_back(QImage(":/images/etLD.gif"));
tankImgs.push_back(QImage(":/images/etRU.gif"));
tankImgs.push_back(QImage(":/images/etRD.gif"));
……………………
…………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1