✅ 操作成功!

代码坦克

发布时间:2023-06-11 作者:admin 来源:文学

代码坦克

代码坦克

-

2023年3月5日发(作者:整合营销什么意思)

JAVA实现经典游戏坦克⼤战的⽰例代码

⽬录

前⾔

主要设计

功能截图

代码实现

总结

前⾔

⼩时候⼤家都玩过坦克⼤战吧,熟悉的旋律和丰富的关卡陪伴了我们⼀整个寒暑假,还记得传说中的经典坦克⼤战吗?那些

怀旧的记忆,伴随着我们⼀起⾛过来的经典坦克⼤战,刚开始那战战兢兢,屡屡被敌⼈坦克击毁的情景历历在⽬。现在好了,

再也不⽤担⼼敌⼈坦克了,可以横冲直撞,横扫敌⼈坦克了。快哉

《坦克⼤战》游戏以坦克战⽃为主题,⽤java语⾔实现,采⽤了swing技术进⾏了界⾯化处理,设计思路⽤了⾯向对象思想。

主要需求

可以⽣成不同的地图,消灭地图上的所有坦克为胜利;可以设置关卡地图,不断增加难度。

主要设计

1、要有难度关卡:第⼀关,第⼆关,第三关,第四关,第五关;第⼀关地图最简单,第五关地图最难;

2、坦克要有⾎条,打多次才会死

3、地图要我有我⽅坦克四辆(相当于4条命)和多辆敌⽅坦克

4、我⽅坦克的炮弹数量是固定的,设置为500

5、地图右边显⽰基本信息

6、地图上要在砖块,铁块,河流

功能截图

游戏开始页⾯

代码实现

窗⼝布局

publicclassGameFrameextendsJFrame{

/**

*serialVersionUID

*/

privatestaticfinallongserialVersionUID=-63603304L;

publicGameFrame(){

super();

e(800,700);

le("坦克⼤战");

aultCloseOperation(_ON_CLOSE);

izable(false);

nImage(Img[0]);

//显⽰器屏幕⼤⼩

DimensionscreenSizeInfo=aultToolkit().getScreenSize();

intleftTopX=((int)th()-th())/2;

intleftTopY=((int)ght()-ght())/2;

//设置显⽰的位置在屏幕中间

ation(leftTopX,leftTopY);

}

}

地图渲染核⼼算法

@Service

publicclassPaintService{

@Autowired

privateGameContextcontext;

privateBrickrightBrick=newBrick(700,50);

privateIronrightIron=newIron(700,50);

privateWaterrightWater=newWater(700,50);

/**

*画出东西(包括坦克、障碍物。。)

*

*@paramgGraphics

*@paramstuff东西对象

*@parampanel被画的那个⾯板

*/

publicvoiddrawStuff(Graphicsg,Stuffstuff,JPanelpanel){

switch(e()){

//枚举的switch,有意思,不需要+

caseTANK:

Tanktank=(Tank)stuff;

switch(ect()){//判断所朝的⽅向

caseNORTH:

rth(g,tank,panel);

break;

caseSOUTH:

uth(g,tank,panel);

break;

caseWEST:

st(g,tank,panel);

break;

caseEAST:

st(g,tank,panel);

break;

}

break;

caseBRICK:

//or(newColor(216,90,49));

//3DRect(()-20,()-20,40,40,false);

age(mg[()],

()-10,()-10,20,20,panel);

break;

caseIRON:

//or(newColor(225,225,225));

//3DRect(()-20,

//()-20,40,40,false);

age(mg[()],()-10,

()-10,20,20,panel);

break;

caseWATER:

//or(newColor(65,64,253));

//ct(()-20,

//()-20,40,40);

age(mg[()],

()-10,()-10,20,20,panel);

break;

}

}

/**

*画出爆炸

*

*@paramgGraphics

*@parambombs炸弹对象容器

*@parampanel被画的那个⾯板

*/

publicvoiddrawBomb(Graphicsg,Vectorbombs,JPanelpanel){

for(inti=0;i<();i++){

intl=(i).getL();

Bombb=(i);//从炸弹容器中取出⼀颗炸弹

if(eTime()>24){//⽣命值21-25

age([0],()-l/2,()

-l/2,l,l,panel);

}elseif(eTime()>18){//⽣命值16-20

age([1],()-l/2,()

-l/2,l,l,panel);

}elseif(eTime()>12){//⽣命值11-15

age([2],()-l/2,()

-l/2,l,l,panel);

}elseif(eTime()>6){//⽣命值6-10

age([3],()-l/2,()

-l/2,l,l,panel);

}else{//⽣命值低于6

age([4],()-l/2,()

-l/2,l,l,panel);

}

wn();//⽣命随时间衰减

if(eTime()==0){//该炸弹死亡

e(false);

}

}

}

/**

*画出敌⼈坦克和⼦弹

*

*@paramgGraphics

*@paramenemies敌⼈坦克容量

*@parampanel被画的⾯板

*/

publicvoiddrawEnemyTank(Graphicsg,Vectorenemies,JPanelpanel){

for(inti=0;i<();i++){

uff(g,(i),panel);//画出敌⼈的坦克

for(intj=0;j<(i).getBullets().size();j++){

if((i).getBullets().get(j)!=null){

Bulleteb=(i).getBullets().get(j);

age(,()-2,

()-2,4,4,panel);

}

}

}

}

/**

*画出我的坦克和⼦弹

*

*@paramgGraphics

*@parammyTanks我的坦克容量

*@parampanel被画的那个⾯板

*/

publicvoiddrawMyTank(Graphicsg,VectormyTanks,JPanelpanel){

for(intm=0;m<();m++){

MyTankmyTank=(m);//取出我的坦克

uff(g,myTank,panel);//画出我的坦克

for(inti=0;i

if(lets().get(i)!=null){

Bulletb=lets().get(i);

age(,()-2,

()-2,4,4,panel);

}

}

}

}

/**

*画出地图

*

*@paramgGraphics

*@parammap地图对象

*@parampanel被画的那个⾯板

*/

publicvoiddrawMap(Graphicsg,Mapmap,JPanelpanel){

Vectorbricks=cks();

Vectorirons=ns();

Vectorwaters=ers();

for(inti=0;i<();i++){

uff(g,(i),panel);

}

for(inti=0;i<();i++){

uff(g,(i),panel);

}

for(inti=0;i<();i++){

uff(g,(i),panel);

}

}

/**

*画出⼀个⾯朝北的坦克

*

*@paramgGraphics

*@paramtank东西对象

*@parampanel被画的那个⾯板

*/

publicvoiddrawNorth(Graphicsg,Tanktank,JPanelpanel){

//intx=();

//inty=();//0.设置画笔颜⾊

//or();

////1.画出左边的矩形

//3DRect(x-20,y-20,10,

//40,false);

//3DRect(x+10,y-20,10,40,false);//2.画出右边矩形

////3.更换画笔颜⾊

//or(or());//4.画出轮⼦条纹

//for(int

//i=0;

//i<20-1;

//i++){

//ne(x-20,y-20+(i+1)*2,x-10-1,y-20+(i+1)*2);

//ne(x+10,y-20+(i+1)*2,x+20-1,y-20+(i+1)*2);

//}//5.画出中间1矩形

//3DRect(x-15,y-14,30,28,false);//6.更换画笔颜⾊

//or();//7.画出中间2矩形

//3DRect(x-10,y-9,20,

//18,false);//8.画出中间3矩形

//3DRect(x-3,y-5,6,10,false);//9.画直线

//ne(x-15,y-14,x-10,y-9);

//ne(x+15,y-14,x+10,y-9);

//ne(x-15,y+14,x-10,y+9);

//ne(x+15,y+14,x+10,y+9);

//or(or());//10.画矩形

//3DRect(x-3,y-12,6,3,

//false);

//3DRect(x-2,y-20,4,2,false);

//3DRect(x-1,y-20,

//2,11,false);

Imageimage;

if(kType()==){

or();

image=Img[()];//初始化图⽚

}else{

image=ankImg[()];

or();

}

age(image,()-20,()-20,40,40,panel);

ct(()-20,()-30,od()*4,5);

}

/**

*画出⼀个⽅向朝南的坦克

*

*@paramgGraphics

*@paramtank东西对象

*@parampanel被画的那个⾯板

*/

publicvoiddrawSouth(Graphicsg,Tanktank,JPanelpanel){

//intx=();

//inty=();

//or();

//3DRect(x-20,y-20,10,40,false);

//3DRect(x+10,y-20,10,

//40,false);

//or(or());

//for(inti=0;i<20-1;i++){

//ne(x-20,y-20+(i+1)*2,x-10-1,y-20+(i+1)*2);

//ne(x+10,

//y-20+(i+1)*2,x+20-1,y-20+(i+1)*2);

//}

//3DRect(x-15,y-14,30,

//28,false);

//or();

//3DRect(x-10,y-9,20,

//18,false);

//3DRect(x-3,y-5,6,10,false);

//ne(x-15,

//y-14,x-10,y-9);

//ne(x+15,y-14,x+10,y-9);

//ne(x-15,

//y+14,x-10,y+9);

//ne(x+15,y+14,x+10,y+9);

//or(or());

//3DRect(x-3,y+9,6,3,false);

//3DRect(x-1,y+9,2,11,false);

//3DRect(x-2,y+18,4,2,

//false);

Imageimage;

if(kType()==){

or();

image=Img[()];//初始化图⽚

}else{

image=ankImg[()];

or();

}

age(image,()-20,()-20,40,40,panel);

ct(()-20,()-30,od()*4,5);

}

/**

*画出⼀个⽅向朝西的坦克

*

*@paramgGraphics

*@paramtank东西对象

*@parampanel被画的那个⾯板

*/

publicvoiddrawWest(Graphicsg,Tanktank,JPanelpanel){

//intx=();

//inty=();

//or();

//3DRect(x-20,y-20,40,10,false);

//3DRect(x-20,y+10,40,

//10,false);

//or(or());

//for(inti=0;i<20-1;i++){

//ne(x-20+(i+1)*2,y-20,x-20+(i+1)*2,y-10-1);

//ne(x-20+(i+1)*2,y-20+30,x-20+(i+1)*2,y-10-1+30);

//}

//3DRect(x-14,y-15,28,30,false);

//or();

//3DRect(x-9,y-10,18,20,false);

//3DRect(x-5,y-3,10,

//6,false);

//ne(x-15,y-14,x-10,y-9);

//ne(x+15,y-14,

//x+10,y-9);

//ne(x-15,y+14,x-10,y+9);

//ne(x+15,y+14,

//x+10,y+9);

//or(or());

//3DRect(x-12,y-3,3,

//6,false);

//3DRect(x-20,y-1,11,2,false);

//3DRect(x-20,

//y-2,2,4,false);

Imageimage;

if(kType()==){

image=Img[()];//初始化图⽚

or();

}else{

image=ankImg[()];

or();

}

age(image,()-20,()-20,40,40,panel);

ct(()-20,()-30,od()*4,5);

}

/**

*画出⼀个⽅向朝东的坦克

*

*@paramgGraphics

*@paramtank东西对象

*@parampanel被画的那个⾯板

*/

publicvoiddrawEast(Graphicsg,Tanktank,JPanelpanel){

//intx=();

//inty=();

//or();

//3DRect(x-20,y-20,40,10,false);

//3DRect(x-20,y+10,40,

//10,false);

//or(or());

//for(inti=0;i<20-1;i++){

//ne(x-20+(i+1)*2,y-20,x-20+(i+1)*2,y-10-1);

//ne(x-20+(i+1)*2,y-20+30,x-20+(i+1)*2,y-10-1+30);

//}

//3DRect(x-14,y-15,28,30,false);

//or();

//3DRect(x-9,y-10,18,20,false);

//3DRect(x-5,y-3,10,

//6,false);

//ne(x-15,y-14,x-10,y-9);

//ne(x+15,y-14,

//x+10,y-9);

//ne(x-15,y+14,x-10,y+9);

//ne(x+15,y+14,

//x+10,y+9);

//or(or());

//3DRect(x+9,y-3,3,6,

//false);

//3DRect(x+9,y-1,11,2,false);

//3DRect(x+18,y-2,

//2,4,false);

Imageimage;

if(kType()==){

image=Img[()];//初始化图⽚

or();

}else{

image=ankImg[()];

or();

}

age(image,()-20,()-20,40,40,panel);

ct(()-20,()-30,od()*4,5);

}

/**

*画出游戏右边的那个⾯板

*

*@paramgGraphics

*@paramtgp游戏主要⾯板对象

*/

publicvoiddrawRight(Graphicsg,GamePaneltgp,RealTimeGameDatadata){

if(MakingFlag().equals()){

ring("当前选中画笔(可按C键切换)",620,20);

if(rentStuff()==){

drawStuff(g,rightIron,tgp);

}elseif(rentStuff()==){

drawStuff(g,rightBrick,tgp);

}elseif(rentStuff()==){

drawStuff(g,rightWater,tgp);

}else{

ring("橡⽪擦",680,50);

}

}else{

for(inti=0;i

if(i>=4){

age(ankImg[()],

402+50*i,100,40,40,tgp);

}else{

age(ankImg[()],

602+50*i,20,40,40,tgp);

}

}

for(intj=0;j

age(Img[()],602+50*j,

400,40,40,tgp);

}

ring("我的坦克⼦弹数⽬:"+ulletNum(),620,500);

}

}

publicvoidrePaintPanel(GamePanelpanel,Graphicsg){

RealTimeGameDatadata=eData();

if(t()){

or();

ct(0,0,_PANEL_WIDTH,_PANEL_HEIGHT);

ct(280,600,40,40);

p(g,(),panel);

Tank(g,anks(),panel);//画出我的坦克(包括⼦弹)

emyTank(g,mies(),panel);//画出敌⼈坦克(包括⼦弹)

mb(g,bs(),panel);//画出爆炸

ght(g,panel,data);

if(ankNum()==0){//如果我的坦克数量为0

age(er,250,(),100,

100,panel);

}

if(myTankNum()==0){//如果敌⼈坦克的数量为0

age(n,250,(),100,

100,panel);

}

if(()==250){

ct(0,0,800,600);

or();

if(ankNum()==0){

ring("失败了",300,220);

}else{

ring("挑战成功,请稍等...",300,220);

}

ring(

("敌⼈坦克死亡数量:"+(myTankNum())),

300,260);

ring("我的坦克死亡总数量:"+illed(),300,

280);

ring(

"我的炮弹消耗总数量:"

+(_TANK_INIT_BULLET_NUM-data

.getMyBulletNum()),300,300);

ring("敌⼈坦克剩余数量:"+myTankNum(),300,

320);

ring("我的坦克剩余总数量:"+ankNum(),300,

340);

ring("我的炮弹剩余总数量:"+ulletNum(),300,

360);

}

}else{

age(mage,0,0,800,700,panel);

//age(,0,(),panel);

if(Smile()){

//age(le1,(),45,

//panel);

nSmile(false);

}else{

//age(le2,(),45,

//panel);

nSmile(true);

}

}

}

}

坦克核⼼算法

@Service

publicclassTankEventService{

/**

*判断坦克是否与另⼀个事物重叠

*

*@paramstuff东西对象

*@paramlength两者之间的最短距离

*@return是否重叠

*/

publicbooleanisTankOverlap(Tanktank,Stuffstuff,intlength){

booleanb=false;

intx=();

inty=();

if(ect()==){

(()-ed());

if((()-y)

&&(()-x)

b=true;

(()+ed());

}else{

(()+ed());

}

}

if(ect()==){

(()+ed());

if((()-y)

&&(()-x)

b=true;

}

(()-ed());

}

if(ect()==){

(()+ed());

if((()-y)

&&(()-x)

b=true;

}

(()-ed());

}

if(ect()==){

(()-ed());

if((()-y)

&&(()-x)

b=true;

}

(()+ed());

}

returnb;

}

/**

*判断是否重叠

*

*@paramenemies敌⼈坦克容量

*@return是否重叠

*/

publicbooleanisMyTankOverlap(MyTanktank,Vectorenemies){

for(inti=0;i<();i++){

if(isTankOverlap(tank,(i),40))

returntrue;

}

returnfalse;

}

/**

*判断⾃⼰跟别的坦克是否重叠

*

*@paramenemies敌⼈坦克容量

*@parammyTanks我的坦克容量

*@return是否重叠

*/

publicbooleanisEnemyTankOverlap(EnemyTankenemy,Vectorenemies,VectormyTanks){

for(inti=0;i<();i++){

if(enemy!=(i)){

if(isTankOverlap(enemy,(i),40)){

rlapNo(true);

returntrue;

}

}

}

for(intj=0;j<();j++){

if(isTankOverlap(enemy,(j),40)){

rlapYes(true);

returntrue;

}

}

rlapNo(false);

rlapYes(false);

returnfalse;

}

/**

*每隔36毫秒⼀直向西⾛

*/

publicvoidenemyGoWest(EnemyTankenemy){

for(;;){

illis(36);

if(!lapNo()&&!lapYes()){

();

}

if(ankLocation()!=){

ect(ankDirect());

break;

}

}

}

/**

*每隔36毫秒⼀直向东⾛

*/

publicvoidenemyGoEast(EnemyTankenemy){

for(;;){

illis(36);

if(!lapNo()&&!lapYes()){

();

}

if(ankLocation()!=){

ect(ankDirect());

break;

}

}

}

/**

*每隔36毫秒⼀直向北⾛

*/

publicvoidenemyGoNorth(EnemyTankenemy){

for(;;){

illis(36);

if(!lapNo()&&!lapYes()){

h();

}

if(ankLocation()!=){

ect(ankDirect());

break;

}

}

}

/**

*每隔36毫秒⼀直向南⾛

*/

publicvoidenemyGoSouth(EnemyTankenemy){

for(;;){

illis(36);

if(!lapNo()&&!lapYes()){

h();

}

if(ankLocation()!=){

ect(ankDirect());

break;

}

}

}

/**

*从指定的三个⽅向中随机选择⼀个

*

*@paramdirect1⽅向1

*@paramdirect2⽅向2

*@paramdirect3⽅向3

*/

publicDirectionEnumenemyGetRandomDirect(DirectionEnumdirect1,DirectionEnumdirect2,DirectionEnumdirect3){

intrandom=(int)(()*3);

DirectionEnumreturnDirect=D;

switch(random){

case0:

returnDirect=direct1;

break;

case1:

returnDirect=direct2;

break;

case2:

returnDirect=direct3;

break;

}

returnreturnDirect;

}

/**

*让敌⼈坦克能够发现我的坦克并开炮

*

*@parammyTank我的坦克

*@parammap地图对象

*/

publicvoidenemyFindAndKill(EnemyTankenemy,MyTankmyTank,Mapmap){

intmyX=();

intmyY=();

intenX=();

intenY=();

if((myX-enX)<20&&myY<=580){

if(enY

ints=0;

for(intt=0;t

Ironiron=ns().get(t);

if((())enY

&&()

s=1;

break;

}

}

if(s==0){

t(true);

ankLocation();

}

}else{

ints=0;

for(intt=0;t

Ironiron=ns().get(t);

if((())<=10&&()

&&()>myY){

s=1;

break;

}

}

if(s==0){

t(true);

ankLocation();

}

}

}elseif((myY-enY)<20&&myY<=580){

if(enX>myX){

ints=0;

for(intt=0;t

Ironiron=ns().get(t);

if((())<=10&&()

&&()>myX){

s=1;

break;

}

}

if(s==0){

t(true);

ankLocation();

}

}else{

ints=0;

for(intt=0;t

Ironiron=ns().get(t);

if((())enX

&&()

s=1;

break;

}

}

if(s==0){

t(true);

ankLocation();

}

}

}else{

t(false);

ankLocation(D);

}

}

}

总结

通过此次的《坦克⼤战》游戏实现,让我对swing的相关知识有了进⼀步的了解,对java这门语⾔也有了⽐以前更深刻的认

识。

java的⼀些基本语法,⽐如数据类型、运算符、程序流程控制和数组等,理解更加透彻。java最核⼼的核⼼就是⾯向对象思

想,对于这⼀个概念,终于悟到了⼀些。

以上就是JAVA实现经典游戏坦克⼤战的⽰例代码的详细内容,更多关于JAVA坦克⼤战的资料请关注其它相关⽂章!

👁️ 阅读量:0