写这个游戏已经有一段时间了,一直在实现各种新功能,从最开始的地图上只有坦克,发子弹还是一个大问题到现在可以两个人一起玩,还是花了不少心思的,现在坦克的速度更快,电脑坦克也不会撞墙.虽然游戏性没有经典坦克大战那么强,但是还是可以用来休闲娱乐一下,这个用了很多最近学到的新知识,模仿俄罗斯方块,还有一些小技巧,比如可以同时按触发多个按键事件,对子弹的处理等.
左边的坦克用W D S A控制移动,H发射子弹,每次最多出现5颗子弹,右边的坦克用上下左右箭头控制移动,L键发射子弹,互不干扰.Q键可以直接退出游戏,游戏结束后按Y键可以继续游戏.为了结构清晰游戏分为Mywar Shoot Tanks 3个类.
(第二次用插入代码,之前一直都不懂这功能,我奥特蛋了 (*^__^*) 嘻嘻……)
/********************MyWar类***********************/
package tank; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.util.Arrays; import java.util.Random; import java.util.Timer; import java.util.TimerTask; import javax.swing.JFrame; import javax.swing.JPanel; public class MyWar{ public static void main(String[] args) { JFrame frame = new JFrame("坦克大战");//新建一个窗口 War war = new War();//创建一个War类的对象 frame.add(war);//把war添加到窗口中 frame.setSize(750,530);//窗口宽高 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭时结束进程 frame.setLocationRelativeTo(null);//使窗口居中 frame.setVisible(true);//这句是干啥的? war.action();//启动(war)战斗,亲,可以游戏了! } } class War extends JPanel{ private boolean sUp,sDown,sRight,sLeft,sH;//定义右边坦克按键的开关 private boolean sW,sD,sS,sA,sL;//定义左边坦克按键的开关 public static final int WIDTH=750;//定义地图宽度 public static final int HEIGHT=530;//定义地图高度 private int score;//设置分数 private boolean gameOver;//gameover=false表示游戏没有结束 private Timer timer;//刚学的东西 private int shootNum,shootNum1;//可以射击的子弹数,防止作弊 MyTank[] myTank=new MyTank[2];//定义一个我方坦克对象数组 EnemyTanks[] enemyTank=new EnemyTanks[5];//初始化敌人坦克的数量 EnemyTanks newEnemyTank;//用来产生一辆敌人的坦克,补充死去的T_T Random r = new Random(); /**用于产生一辆新的敌人坦克,返回参数是EnemyTanks类型*/ public EnemyTanks nextTank(){ int x = r.nextInt(2)*WIDTH;//随机x的值,坦克出生在角落 int y=r.nextInt(2)*HEIGHT;//随机y的值,坦克出生在角落 int step=r.nextInt(4)+1;//速度 int direct=r.nextInt(4)+1;//方向 int who = r.nextInt(3)+1;//谁? newEnemyTank=new EnemyTanks(x,y,step,direct,who,true); return newEnemyTank;//返回一个新坦克 } public void action(){ startGame();//开始游戏函数 KeyAdapter l = new KeyAdapter(){//键盘监听 public void keyPressed(KeyEvent e){ int key = e.getKeyCode(); if(key==KeyEvent.VK_Q){ System.exit(0);//Q关闭进程结束游戏 } if(gameOver){ if(key==KeyEvent.VK_Y){ startGame();//Y键开始游戏 } return; } switch(key){ case KeyEvent.VK_W:sW=true;break; case KeyEvent.VK_A:sA=true;break; case KeyEvent.VK_D:sD=true;break; case KeyEvent.VK_S:sS=true;break; case KeyEvent.VK_L:sL=true;break; case KeyEvent.VK_RIGHT:sRight=true;break; case KeyEvent.VK_LEFT:sLeft=true;break; case KeyEvent.VK_DOWN:sDown=true;break; case KeyEvent.VK_UP:sUp=true;break; case KeyEvent.VK_H:sH=true;break; } repaint(); } public void keyReleased(KeyEvent e) { int key = e.getKeyCode(); switch(key){ case KeyEvent.VK_W:sW=false;break; case KeyEvent.VK_A:sA=false;break; case KeyEvent.VK_D:sD=false;break; case KeyEvent.VK_S:sS=false;break; case KeyEvent.VK_H:sH=false;break; case KeyEvent.VK_RIGHT:sRight=false;break; case KeyEvent.VK_LEFT:sLeft=false;break; case KeyEvent.VK_DOWN:sDown=false;break; case KeyEvent.VK_UP:sUp=false;break; case KeyEvent.VK_L:sL=false;break; } } }; this.requestFocus(); this.addKeyListener(l); } public void level(){//每5分增加一辆敌人的坦克 int length = score/5+5; if(length>enemyTank.length){ enemyTank=Arrays.copyOf(enemyTank,enemyTank.length+1);//坦克数组扩容 enemyTank[enemyTank.length-1]=nextTank();//初始化数组最后的一辆坦克 enemyTank[enemyTank.length-1].start();//启动这个坦克的线程 } } public void startGame(){ cleanShoot();//清空子弹 shootNum=5;//可以发射的子弹数为5 shootNum1=5; score=0; myTank[0]= new MyTank(550,370,4,1,-1,true);//初始化我的坦克 myTank[1]= new MyTank(100,100,4,1,0,true); for (int i = 0; i < enemyTank.length; i++) {//初始化敌人的坦克 if(gameOver){//游戏结束,关闭每个坦克的线程 enemyTank[i].s=false; } } enemyTank=new EnemyTanks[5]; for (int i = 0; i < enemyTank.length; i++) {//初始化敌人的坦克 enemyTank[i]=nextTank(); } gameOver=false;//游戏没有结束,表示开始 myTank[0].start();//启动我的坦克线程 myTank[1].start(); for (int i = 0; i < enemyTank.length; i++) {//启动敌人坦克线程 enemyTank[i].start(); } timer = new Timer();//匿名内部类Timer timer.schedule(new TimerTask(){ public void run(){//重写run()函数 repaint(); shootAndRun(); } }, 0,50);//50毫秒执行一次 } /**检查游戏是否结束*/ public void checkGameOver(){//模仿俄罗斯方块里面写的 if(myTank[0].isLive()||myTank[1].isLive()){ return; } gameOver = true; timer.cancel(); repaint(); } public void shootAndRun(){ if(sW){//如果开关sW=true就执行{}里面的代码 myTank[1].moveUp(); }else if(sD){ myTank[1].moveRight(); }else if(sS){ myTank[1].moveDown(); }else if(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(); }else if(sRight){ myTank[0].moveRight(); }else if(sLeft){ myTank[0].moveLeft(); }else if(sDown){ myTank[0].moveDown(); }if(sL&&myTank[0].isLive()){ if(shootNum>=0){ myTank[0].shoot(); shootNum--;//控制子弹数量,射击一次,子弹减少,消失加1 } } } public void paint(Graphics g){ 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()); for (int i = 0; i < enemyTank.length; 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); } /* public void paintMap(Graphics g){//这是个方法可以生成漂亮的东西 for (int j = 0; j < 3; j++) { Random color=new Random(); //通过Random生成随机颜色 int r = color.nextInt(256); int g1 = color.nextInt(256); int b = color.nextInt(256); g.setColor(new Color(r, g1, b)); g.fillOval(color.nextInt(750), color.nextInt(530), 5, 5); } }*/ public void paintScore(Graphics g){//画字符相关的 g.setColor(Color.lightGray); Font f = getFont(); Font font = new Font(f.getName(),Font.BOLD,0x1e); int x = 130; int y = 275; String str = "SCORE:"+this.score; g.setFont(font); g.drawString(str, x, y); str="TANK:"+enemyTank.length ; x+=170; g.drawString(str, x, y); x+=140; str = "[Q]Quit!"; if(gameOver){ str = "[Y]Start!"; } g.drawString(str, x, y); } public void paintWall(Graphics g){//画中间的柱子 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); } /**画自己坦克子弹,同时判断子弹有没有击中敌人*/ public void paintShoot(Graphics g){ ShootDispeal(); for (int i = 0; i < 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); int x = Shoot.myShoot[i]; int y = Shoot.myShoot[i+1]; for (int j = 0; j < enemyTank.length; j++) { int ex = enemyTank[j].getX(); int ey = 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]; } } /**画敌人发出的子弹,同时判断是否击中了我的坦克*/ public void paintEemyShoot(Graphics g){ ShootDispeal(); for (int i = 0; i < Shoot.enemyShoot.length; i+=4) { if(Shoot.enemyShoot[i]==0&&Shoot.enemyShoot[i+1]==0){ continue; } g.setColor(Color.blue); g.fillOval(Shoot.enemyShoot[i], Shoot.enemyShoot[i+1], 10, 10); int x = Shoot.enemyShoot[i]; int y = Shoot.enemyShoot[i+1]; int mx = myTank[0].getX(); int my = myTank[0].getY(); int mx1 = myTank[1].getX(); int my1 = myTank[1].getY(); if(x>mx&&x<mx+40&&y>my&&y<my+40){ myTank[0].setLive(false); checkGameOver(); } if(x>mx1&&x<mx1+40&&y>my1&&y<my1+40){ myTank[1].setLive(false); checkGameOver(); } Shoot.enemyShoot[i]+=Shoot.enemyShoot[i+2];//根据步伐,改变子弹的坐标 Shoot.enemyShoot[i+1]+=Shoot.enemyShoot[i+3]; } } /**画坦克*/ public void paintTank(int x,int y,Graphics g,int direct,int who,boolean isLive){ Color color = null;//设置颜色 if(isLive){ if(who==0){//我的坦克 color=Color.green; }else if(who==-1){ color=Color.yellow; }else if(who==1){//1,2,3敌人的坦克,3种颜色 color=Color.red; }else if(who==2){ color=Color.magenta; }else if(who==3){ color=Color.CYAN; } switch(direct){//根据方向画出不同方向的坦克 case 1:g.setColor(color);paintUpTank(x,y,g);break; case 2:g.setColor(color);paintRightTank(x,y,g);break; case 3:g.setColor(color);paintDownTank(x,y,g);break; case 4:g.setColor(color);paintLeftTank(x,y,g);break; } } } /**纯画图打造坦克*/ public void paintUpTank(int x,int y,Graphics g){ 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); } public void paintLeftTank(int x,int y,Graphics g){ 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.setColor(Color.black); g.fill3DRect(x-10, y+22, 33, 5, false); g.setColor(Color.yellow); g.fillOval(x+20, y+18, 10, 10); } public void paintDownTank(int x,int y,Graphics g){ 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+25, 5, 33, false); g.setColor(Color.yellow); g.fillOval(x+20, y+18, 10, 10); } public void paintRightTank(int x,int y,Graphics g){ 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.setColor(Color.black); g.fill3DRect(x+23, y+22, 33, 5, false); g.setColor(Color.yellow); g.fillOval(x+20, y+18, 10, 10); } /**重新开始游戏的时候会清空子弹数组里面的数据*/ public void cleanShoot(){ for (int i = 0; i < Shoot.enemyShoot.length; i++) { Shoot.enemyShoot[i]=0; } for (int i = 0; i < Shoot.myShoot.length; i++) { Shoot.myShoot[i]=0; } } /**子弹消失了*/ public void ShootDispeal(){ for (int i = 0; i < Shoot.myShoot.length; i+=4) {//撞到边缘 if(Shoot.myShoot[i]<0||Shoot.myShoot[i]>WIDTH||Shoot.myShoot[i+1]<0||Shoot.myShoot[i+1]>HEIGHT){ Shoot.myShoot[i]=0;Shoot.myShoot[i+1]=0; Shoot.myShoot[i+2]=0;Shoot.myShoot[i+3]=0; shootNum++; shootNum1++; } int x=Shoot.myShoot[i]; int y=Shoot.myShoot[i+1]; //撞到柱子 if((x>330&&x<360&&y>150&&y<380)||(x>130&&x<570&&y>240&&y<280)){ Shoot.myShoot[i]=0;Shoot.myShoot[i+1]=0; Shoot.myShoot[i+2]=0;Shoot.myShoot[i+3]=0; shootNum++; shootNum1++; } } for (int i = 0; i < Shoot.enemyShoot.length; i+=4) {//撞到边缘 if(Shoot.enemyShoot[i]<0||Shoot.enemyShoot[i]>WIDTH||Shoot.enemyShoot[i+1]<0||Shoot.enemyShoot[i+1]>HEIGHT){ Shoot.enemyShoot[i]=0;Shoot.enemyShoot[i+1]=0; Shoot.enemyShoot[i+2]=0;Shoot.enemyShoot[i+3]=0; } int x=Shoot.enemyShoot[i]; int y=Shoot.enemyShoot[i+1]; //撞到柱子 if((x>330&&x<360&&y>150&&y<380)||(x>130&&x<570&&y>240&&y<280)){ Shoot.enemyShoot[i]=0;Shoot.enemyShoot[i+1]=0; Shoot.enemyShoot[i+2]=0;Shoot.enemyShoot[i+3]=0; } } } }
/********************Tanks类***********************/
package tank; import java.util.Random; /**坦克父类,继承了线程*/ public class Tanks extends Thread{ private int x;//坦克坐标x private int y;//坦克坐标y private int step;//坦克的速度 private int direct;//方向,1表示向上,2表示向右,3表示向下,4表示向左 private int who;//坦克标识,0和-1表示自己的坦克,1,2,3表示敌人的坦克,颜色随机. private boolean isLive = true;//判断坦克是否死亡 Shoot shoot;//shoot坦克的射击 public Tanks(int x, int y, int step, int direct,int who ,boolean isLive) { super(); this.x = x; this.y = y; this.step = step+2; this.direct = direct; this.who = who; this.isLive = isLive; } public boolean canMove(){//boolean返回值类型.判断坦克是否能移动,比如撞墙了. if(x<0){ x=0; return false; }else if(x>690){ x=690; return false; }else if(y<0){ y=0; return false; }else if(y>450){ y=450; return false; }else if(x>270&&x<370&&y>100&&y<380){//撞到竖着的柱子 if(x-270<20){ x=270; }else{ x=370; } return false; }else if(x>85&&x<570&&y>195&&y<290){//撞到横着的柱子 if(y-195<10){ y=190; }else{ y=290; } return false; } return true; } public void moveUp(){//坦克向上移动一步 if(canMove()){ y-=step; direct=1; } } public void moveDown(){//坦克向下移动一步 if(canMove()){ y+=step; direct=3; } } public void moveLeft(){//坦克向左移动一步 if(canMove()){ x-=step; direct=4; } } public void moveRight(){//坦克向右移动一步 if(canMove()){ x+=step; direct=2; } } public void shoot(){//每次射击调用此函数 shoot = new Shoot(x,y,direct,step,who); } public String toString() {//用于调试 return "Tanks [direct=" + direct + ", isLive=" + isLive + ", step=" + step + ", x=" + x + ", y=" + y + "]"; } /**以下是get() set()函数*/ public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public int getStep() { return step; } public void setStep(int step) { this.step = step; } public int getDirect() { return direct; } public void setDirect(int direct) { this.direct = direct; } public boolean isLive() { return isLive; } public void setLive(boolean isLive) { this.isLive = isLive; } public void setWho(int who) { this.who = who; } public int getWho() { return who; } } class MyTank extends Tanks{//我的坦克继承了Tanks public MyTank(int x, int y, int step, int direct, int who ,boolean isLive) { super(x, y, step, direct, who, isLive); } } class EnemyTanks extends Tanks{//敌人的坦克继承了tanks,也继承了父类的线程 private int time = 500;//线程占用时间500毫秒 boolean s=true;//定义一个boolean的开关,坦克死亡,关闭开关,线程死亡. public EnemyTanks(int x, int y, int step, int direct, int who, boolean isLive) { super(x, y, step, direct, who,isLive); } public void move(){//敌人的坦克自己移动 Random r = new Random(); int random = r.nextInt(50); int r1=r.nextInt(4); if(!(r1==0)){//如果r1=0,就射击一次 shoot(); } for (int i = 0; i < random; i++) {//random表示坦克的一次随机移动的距离 try { Thread.sleep(50);//线程sleep,移动使移动看起来自然一点 } catch (InterruptedException e) { e.printStackTrace(); } if(r1==0){//根据r1的值判断,此次移动往那个方向 if(this.getY()==0||this.getY()==290){ r1=3; }else{ moveUp(); } }else if(r1==1){ if(this.getX()==690||this.getX()==270){ r1=2; }else{ moveRight(); } }else if(r1==2){ if(this.getX()==0||this.getX()==370){ r1=1; }else{ moveLeft(); } }else if(r1==3){ if(this.getY()==450||this.getY()==190){ r1=0; }else{ moveDown(); } } } } public void run(){//继承线程功能,必须实现run()函数 while(s){//s=true表示坦克没有死亡.s=false跳出死循环,坦克死亡 move(); try { Thread.sleep(time);//每次线程占用时间500毫秒 } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
/********************Shoot类***********************/
package tank; /**Shoot类,处理坦克的射击*/ public class Shoot { static int[] myShoot = new int[40];//我的坦克子弹放在数组里面 static int[] enemyShoot = new int[100];//所有敌人共享这个数组 private int direct;//坦克的方向 private int who;//是敌人的坦克?还是自己的坦克 private int x1;//根据情况调整后子弹x的坐标 private int y1;//根据情况调整后子弹y的坐标 private int stepx;//子弹在x轴上移动的速度 private int stepy;//子弹在y轴上移动的速度 private int speed;//坦克的速度 static int indexMy=0;//我的子弹索引,数组满了之后indexMy=0;每次加4 static int indexEnemy=0;//敌人子弹索引,数组满了之后indexEnemy=0;每次加4 public Shoot(int x,int y,int direct,int step,int who){ speed=step+8;//根据坦克的速度,设定子弹的速度 this.direct=direct; this.who=who; if(direct==1){//if else用来调整子弹射击出去的位置 x1=x+20; y1=y; stepx=0; stepy=-speed; }else if(direct==2){ x1=x+40; y1=y+20; stepx=speed; stepy=0; }else if(direct==3){ x1=x+20; y1=y+40; stepx=0; stepy=speed; }else if(direct==4){ x1=x; y1=y+20; stepx=-speed; stepy=0; } if(indexEnemy==enemyShoot.length-4){//表示数组满了 indexEnemy=0; }if(indexMy==myShoot.length-4){//表示数组满了 indexMy=0; } if(who==1||who==2||who==3){//敌人的坦克 enemyShoot[indexEnemy]=x1;//子弹的坐标x enemyShoot[indexEnemy+1]=y1;//子弹的坐标y enemyShoot[indexEnemy+2]=stepx;//子弹在x轴移动的步伐 enemyShoot[indexEnemy+3]=stepy;//在t轴移动的步伐 indexEnemy+=4; }else if(who==0||who==-1){//我的坦克 myShoot[indexMy]=x1; myShoot[indexMy+1]=y1; myShoot[indexMy+2]=stepx; myShoot[indexMy+3]=stepy; indexMy+=4; } } }