达内学习心得可以两人一起玩的坦克大战Word下载.docx
《达内学习心得可以两人一起玩的坦克大战Word下载.docx》由会员分享,可在线阅读,更多相关《达内学习心得可以两人一起玩的坦克大战Word下载.docx(26页珍藏版)》请在冰点文库上搜索。
frame.setLocationRelativeTo(null);
//使窗口居中
frame.setVisible(true);
//这句是干啥的?
war.action();
//启动(war)战斗,亲,可以游戏了!
}
}
classWarextendsJPanel{
privatebooleansUp,sDown,sRight,sLeft,sH;
//定义右边坦克按键的开关
privatebooleansW,sD,sS,sA,sL;
//定义左边坦克按键的开关
publicstaticfinalintWIDTH=750;
//定义地图宽度
publicstaticfinalintHEIGHT=530;
//定义地图高度
privateintscore;
//设置分数
privatebooleangameOver;
//gameover=false表示游戏没有结束
privateTimertimer;
//刚学的东西
privateintshootNum,shootNum1;
//可以射击的子弹数,防止作弊
MyTank[]myTank=newMyTank[2];
//定义一个我方坦克对象数组
EnemyTanks[]enemyTank=newEnemyTanks[5];
//初始化敌人坦克的数量
EnemyTanksnewEnemyTank;
//用来产生一辆敌人的坦克,补充死去的T_T
Randomr=newRandom();
/**用于产生一辆新的敌人坦克,返回参数是EnemyTanks类型*/
publicEnemyTanksnextTank(){
intx=r.nextInt
(2)*WIDTH;
//随机x的值,坦克出生在角落
inty=r.nextInt
(2)*HEIGHT;
//随机y的值,坦克出生在角落
intstep=r.nextInt(4)+1;
//速度
intdirect=r.nextInt(4)+1;
//方向
intwho=r.nextInt(3)+1;
//谁?
newEnemyTank=newEnemyTanks(x,y,step,direct,who,true);
returnnewEnemyTank;
//返回一个新坦克
publicvoidaction(){
startGame();
//开始游戏函数
KeyAdapterl=newKeyAdapter(){//键盘监听
publicvoidkeyPressed(KeyEvente){
intkey=e.getKeyCode();
if(key==KeyEvent.VK_Q){
System.exit(0);
//Q关闭进程结束游戏
if(gameOver){
if(key==KeyEvent.VK_Y){
//Y键开始游戏
return;
switch(key){
caseKeyEvent.VK_W:
sW=true;
break;
caseKeyEvent.VK_A:
sA=true;
caseKeyEvent.VK_D:
sD=true;
caseKeyEvent.VK_S:
sS=true;
caseKeyEvent.VK_L:
sL=true;
caseKeyEvent.VK_RIGHT:
sRight=true;
caseKeyEvent.VK_LEFT:
sLeft=true;
caseKeyEvent.VK_DOWN:
sDown=true;
caseKeyEvent.VK_UP:
sUp=true;
caseKeyEvent.VK_H:
sH=true;
repaint();
publicvoidkeyReleased(KeyEvente){
sW=false;
sA=false;
sD=false;
sS=false;
sH=false;
sRight=false;
sLeft=false;
sDown=false;
sUp=false;
sL=false;
};
this.requestFocus();
this.addKeyListener(l);
publicvoidlevel(){//每5分增加一辆敌人的坦克
intlength=score/5+5;
if(length>
enemyTank.length){
enemyTank=Arrays.copyOf(enemyTank,enemyTank.length+1);
//坦克数组扩容
enemyTank[enemyTank.length-1]=nextTank();
//初始化数组最后的一辆坦克
enemyTank[enemyTank.length-1].start();
//启动这个坦克的线程
publicvoidstartGame(){
cleanShoot();
//清空子弹
shootNum=5;
//可以发射的子弹数为5
shootNum1=5;
score=0;
myTank[0]=newMyTank(550,370,4,1,-1,true);
//初始化我的坦克
myTank[1]=newMyTank(100,100,4,1,0,true);
for(inti=0;
i<
enemyTank.length;
i++){//初始化敌人的坦克
if(gameOver){//游戏结束,关闭每个坦克的线程
enemyTank[i].s=false;
enemyTank=newEnemyTanks[5];
enemyTank[i]=nextTank();
gameOver=false;
//游戏没有结束,表示开始
myTank[0].start();
//启动我的坦克线程
myTank[1].start();
i++){//启动敌人坦克线程
enemyTank[i].start();
timer=newTimer();
//匿名内部类Timer
timer.schedule(newTimerTask(){
publicvoidrun(){//重写run()函数
shootAndRun();
},0,50);
//50毫秒执行一次
/**检查游戏是否结束*/
publicvoidcheckGameOver(){//模仿俄罗斯方块里面写的
if(myTank[0].isLive()||myTank[1].isLive()){
gameOver=true;
timer.cancel();
publicvoidshootAndRun(){
if(sW){//如果开关sW=true就执行{}里面的代码
myTank[1].moveUp();
}elseif(sD){
myTank[1].moveRight();
}elseif(sS){
myTank[1].moveDown();
}elseif(sA){
myTank[1].moveLeft();
}if(sH&
&
myTank[1].isLive()){//如果sL=true并且坦克是活的,就执行
if(shootNum1>
=0){//如果允许的子弹数小于于0了,不执行
myTank[1].shoot();
shootNum1--;
//控制子弹数量,射击一次,子弹减少,消失加1
if(sUp){
myTank[0].moveUp();
}elseif(sRight){
myTank[0].moveRight();
}elseif(sLeft){
myTank[0].moveLeft();
}elseif(sDown){
myTank[0].moveDown();
}if(sL&
myTank[0].isLive()){
if(shootNum>
=0){
myTank[0].shoot();
shootNum--;
publicvoidpaint(Graphicsg){
g.setColor(Color.white);
g.fillRect(0,0,this.getWidth(),this.getHeight());
//画背景
paintWall(g);
//画墙
//画我的坦克
paintTank(myTank[1].getX(),myTank[1].getY(),g,myTank[1].getDirect(),myTank[1].getWho(),myTank[1].isLive());
paintTank(myTank[0].getX(),myTank[0].getY(),g,myTank[0].getDirect(),myTank[0].getWho(),myTank[0].isLive());
i++){//画敌人的坦克
paintTank(enemyTank[i].getX(),enemyTank[i].getY(),g,enemyTank[i].getDirect(),enemyTank[i].getWho(),enemyTank[i].isLive());
paintShoot(g);
//画我射击的子弹
paintEemyShoot(g);
//画敌人发出的子弹
paintScore(g);
//画分数,及字符
//paintMap(g);
/*publicvoidpaintMap(Graphicsg){//这是个方法可以生成漂亮的东西
for(intj=0;
j<
3;
j++){
Randomcolor=newRandom();
//通过Random生成随机颜色
intr=color.nextInt(256);
intg1=color.nextInt(256);
intb=color.nextInt(256);
g.setColor(newColor(r,g1,b));
g.fillOval(color.nextInt(750),color.nextInt(530),5,5);
}*/
publicvoidpaintScore(Graphicsg){//画字符相关的
g.setColor(Color.lightGray);
Fontf=getFont();
Fontfont=newFont(f.getName(),Font.BOLD,0x1e);
intx=130;
inty=275;
Stringstr="
SCORE:
"
+this.score;
g.setFont(font);
g.drawString(str,x,y);
str="
TANK:
+enemyTank.length;
x+=170;
x+=140;
str="
[Q]Quit!
;
[Y]Start!
publicvoidpaintWall(Graphicsg){//画中间的柱子
g.setColor(Color.LIGHT_GRAY);
g.fill3DRect(WIDTH/2-45,150,40,HEIGHT-300,false);
g.fill3DRect(130,HEIGHT/2-20,WIDTH-300,40,false);
/**画自己坦克子弹,同时判断子弹有没有击中敌人*/
publicvoidpaintShoot(Graphicsg){
ShootDispeal();
Shoot.myShoot.length;
i+=4){
if(Shoot.myShoot[i]==0&
Shoot.myShoot[i+1]==0){
continue;
g.setColor(Color.RED);
g.fillOval(Shoot.myShoot[i],Shoot.myShoot[i+1],10,10);
intx=Shoot.myShoot[i];
inty=Shoot.myShoot[i+1];
intex=enemyTank[j].getX();
intey=enemyTank[j].getY();
if(x>
ex&
x<
ex+40&
y>
ey&
y<
ey+40){
score+=1;
level();
enemyTank[j].s=false;
//坦克死亡,线程关闭
enemyTank[j]=nextTank();
enemyTank[j].start();
Shoot.myShoot[i]=0;
Shoot.myShoot[i+1]=0;
//子弹消失
Shoot.myShoot[i+2]=0;
Shoot.myShoot[i+3]=0;
shootNum++;
shootNum1++;
Shoot.myShoot[i]+=Shoot.myShoot[i+2];
Shoot.myShoot[i+1]+=Shoot.myShoot[i+3];
/**画敌人发出的子弹,同时判断是否击中了我的坦克*/
publicvoidpaintEemyShoot(Graphicsg){
Shoot.enemyShoot.length;
if(Shoot.enemyShoot[i]==0&
Shoot.enemyShoot[i+1]==0){
g.setColor(Color.blue);
g.fillOval(Shoot.enemyShoot[i],Shoot.enemyShoot[i+1],10,10);
intx=Shoot.enemyShoot[i];
inty=Shoot.enemyShoot[i+1];
intmx=myTank[0].getX();
intmy=myTank[0].getY();
intmx1=myTank[1].getX();
intmy1=myTank[1].getY();
mx&
mx+40&
my&
my+40){
myTank[0].setLive(false);
checkGameOver();
mx1&
mx1+40&
my1&
my1+40){
myTank[1].setLive(false);
Shoot.enemyShoot[i]+=Shoot.enemyShoot[i+2];
//根据步伐,改变子弹的坐标
Shoot.enemyShoot[i+1]+=Shoot.enemyShoot[i+3];
/**画坦克*/
publicvoidpaintTank(intx,inty,Graphicsg,intdirect,intwho,booleanisLive){
Colorcolor=null;
//设置颜色
if(isLive){
if(who==0){//我的坦克
color=Color.green;
}elseif(who==-1){
color=Color.yellow;
}elseif(who==1){//1,2,3敌人的坦克,3种颜色
color=Color.red;
}elseif(who==2){
color=Color.magenta;
}elseif(who==3){
color=Color.CYAN;
}
switch(direct){//根据方向画出不同方向的坦克
case1:
g.setColor(color);
paintUpTank(x,y,g);
case2:
paintRightTank(x,y,g);
case3:
paintDownTank(x,y,g);
case4:
paintLeftTank(x,y,g);
/**纯画图打造坦克*/
publicvoidpaintUpTank(intx,inty,Graphicsg){
g.fill3DRect(x,y,15,50,false);
g.fill3DRect(x+35,y,15,50,false);
g.fill3DRect(x+15,y+10,20,30,false);
g.setColor(Color.black);
g.fill3DRect(x+23,y-10,5,33,false);
g.setColor(Color.yellow);
g.fillOval(x+20,y+18,10,10);
publicvoidpaintLeftTank(intx,inty,Graphicsg){
g.fill3DRect(x,y,50,15,false);
g.fill3DRect(x,y+35,50,15,false);
g.fill3DRect(x+10,y+15,30,20,false);
g.fill3DRect(x-10,y+22,33,5,false);
publicvoidpaintDownTank(intx,inty,Graphicsg){
g.fill3DRect(x+23,y+25,5,33,false);
publicvoidpaintRightTank(intx,int