//存储数据

package testdata;
public class Node {private int coordX;//x坐标位置private int coordY;//y坐标位置private int dir;//方向private int speed;//速度private int atk;//攻击力private int boold;//血量private boolean isattack;//是否可以攻击private String sign;public String getSign() {return sign;}public void setSign(String sign) {this.sign = sign;}public boolean isIsattack() {return isattack;}public void setIsattack(boolean isattack) {this.isattack = isattack;}public int getDir() {return dir;}public void setDir(int dir) {this.dir = dir;}private Node next;//下一个public Node(int boold) {this.boold = boold;}public Node(int coordX, int coordY) {this.coordX = coordX;this.coordY = coordY;}/** 敌法坦克*/public Node(int coordX, int coordY, int dir, int speed, int atk, int boold) {this.coordX = coordX;this.coordY = coordY;this.dir = dir;this.speed = speed;this.atk = atk;this.boold = boold;}public Node(int coordX, int coordY, int speed, int atk, int boold, boolean isattack, Node next) {this.coordX = coordX;this.coordY = coordY;this.speed = speed;this.atk = atk;this.boold = boold;this.isattack = isattack;this.next = next;}//土砖public Node(int coordX, int coordY, int boold) {this.coordX = coordX;this.coordY = coordY;this.boold = boold;}//石砖public Node(int coordX, int coordY, int boold, boolean isattack) {this.coordX = coordX;this.coordY = coordY;this.boold = boold;this.isattack = isattack;}//子弹---public Node(int coordX, int coordY, int speed, int atk,int dir) {this.coordX = coordX;this.coordY = coordY;this.speed = speed;this.atk = atk;this.dir=dir;}public void setSpeed(int speed) {this.speed = speed;}public void setAtk(int atk) {this.atk = atk;}public void setBoold(int boold) {this.boold = boold;}public int getSpeed() {return speed;}public int getAtk() {return atk;}public int getBoold() {return boold;}public int getCoordX() {return coordX;}public int getCoordY() {return coordY;}public void setCoordX(int coordX) {this.coordX = coordX;}public void setCoordY(int coordY) {this.coordY = coordY;}public Node getNext(){return next;}public void setNext(Node next){this.next=next;}public void display(){System.out.println("[ "+coordX+", "+coordY+" ]");}
}

//链表

package testdata;    public class SLinkList {private Node head;//表头private int length;//表的长度public SLinkList(){head=null;}/** 表头添加结点*/public void addHead(Node newNode){if(head == null){head=newNode;} else {newNode.setNext(head);head=newNode;}length++;}/** 删除表头结点*/public void delHead(){if(head==null){return ;}Node curNode = head;head=curNode.getNext();length--;}/** 表尾添加结点*/public void addTail(Node newNode){//坐标位置,已经有的长度if(head==null){head=newNode;}else{int count=1;Node tailNode = head;while(count<length){tailNode=tailNode.getNext();count++;}Node curNode = tailNode.getNext();newNode.setNext(curNode);tailNode.setNext(newNode);}length++;}/** 删除表尾结点*/public void delTail() {//坐标位置,已经有的长度if (head == null) {return ;}int count = 1;Node tailNode = head;while (count < length - 1) {tailNode = tailNode.getNext();count++;}Node curNode = tailNode.getNext();tailNode.setNext(curNode.getNext());length--;}/** 指定位置删除*/public void delIndex(int index) {if(index > length || index < 1) {System.out.println("结点删除的位置不存在,可删除的位置为1到"+length);}else if(index == 1) {     //删除表头结点Node curNode = head;head = curNode.getNext();length--;return ;}else{         //删除链表中间或尾部结点Node preNode = head;int count = 1;while(count < index-1) {preNode = preNode.getNext();count++;}Node curNode = preNode.getNext();preNode.setNext(curNode.getNext());length--;return ;}}/** 显示链表中的结点数据*/public void display(){if(head==null){System.out.println("没有数据");return;}int count=1;Node node = head;while(count<=length){node.display();node = node.getNext();count++;}}/** 更改是否可攻击*/public void setarg(int index,boolean isattack){int count=1;Node node = head;if(head==null){return;}else {while(count<index){node = node.getNext();count++;}node.setIsattack(isattack);}}/** 更改所有的参数*/public void setarg(int index,int x,int y,int speed,int atk,int blood,int dir){int count=1;Node node = head;if(head==null){return;}else {while(count<index){node = node.getNext();count++;}node.setCoordX(x);node.setCoordY(y);node.setSpeed(speed);node.setAtk(atk);node.setBoold(blood);node.setDir(dir);}}/**更改单个参数*/public void setarg(int index,int number,String str){int count=1;Node node = head;if(head==null){return;}else {while(count<index){node = node.getNext();count++;}if(str.equals("coordX"))node.setCoordX(number);else if(str.equals("coordY"))node.setCoordY(number);else if(str.equals("speed"))node.setSpeed(number);else if(str.equals("atk"))node.setAtk(number);else if(str.equals("blood"))node.setBoold(number);else if(str.equals("dir"))node.setDir(number);}}/** 更改位置*/public void setarg(int index,int x,int y){int count=1;Node node = head;if(head==null){return;}else {while(count<index){node = node.getNext();count++;}node.setCoordX(x);node.setCoordY(y);}}/** 更改子弹参数*/public void setarg(int index,int x,int y,int speed,int atk,int dir){int count=1;Node node = head;if(head==null){return;}else {while(count<index){node = node.getNext();count++;}node.setCoordX(x);node.setCoordY(y);node.setSpeed(speed);node.setAtk(atk);node.setDir(dir);}}/** 更改老鹰参数*/public void setarg(int index ,int blood){int count=1;Node node = head;if(head==null){return;}else {while(count<index){node = node.getNext();count++;}node .setBoold(blood);node.setIsattack(true);}}/** 更改土砖参数*/public void setarg(int index,int x,int y,int blood){int count=1;Node node = head;if(head==null){return;}else {while(count<index){node = node.getNext();count++;}node.setCoordX(x);node.setCoordY(y);node.setBoold(blood);node.setIsattack(true);}}/** 更改石砖参数*/public void setarg(int index,int x,int y,boolean isattack){int count=1;Node node = head;if(head==null){return;}else {while(count<index){node = node.getNext();count++;}node.setCoordX(x);node.setCoordY(y);node.setIsattack(false);}}/** 获得想要的参数*/public int getarg(int index,String str) {int count = 1;Node node = head;if (head == null) {return 0;}while (count < index) {node = node.getNext();count++;}if(str.equals("coordX")){return node.getCoordX();}else if(str.equals("coordY"))return node.getCoordY();else if(str.equals("speed"))return node.getSpeed();else if(str.equals("atk"))return node.getAtk();else if(str.equals("blood"))return node.getBoold();elsereturn node.getDir();}public String getSign(int index){int count = 1;Node node = head;if (head == null) {return null;}while (count < index) {node = node.getNext();count++;}return node.getSign();}/** 返回表的长度*/public int getLength(){return length;}
}

//地图

package tank_game;
import testdata.SLinkList;import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;public class drawMap extends SLinkList implements KeyListener {ImageIcon tuzhuan = new ImageIcon("E:\\张wp.c\\Eclipse Java\\tank\\.con\\res\\tuzhuan.png");ImageIcon shizhuan = new ImageIcon("E:\\张wp.c\\Eclipse Java\\tank\\.con\\res\\shizhuan.png");ImageIcon laoying = new ImageIcon("E:\\张wp.c\\Eclipse Java\\tank\\.con\\res\\laoying.png");ImageIcon explode2=new ImageIcon("E:\\张wp.c\\Eclipse Java\\tank\\.con\\res\\explode2.png");ImageIcon explode1 = new ImageIcon("E:\\张wp.c\\Eclipse Java\\tank\\.con\\res\\explode1.png");ImageIcon cao = new ImageIcon("E:\\张wp.c\\Eclipse Java\\tank\\.con\\res\\cao.png");ImageIcon shui = new ImageIcon("E:\\张wp.c\\Eclipse Java\\tank\\.con\\res\\shui.png");ImageIcon gameover = new ImageIcon("E:\\张wp.c\\Eclipse Java\\tank\\.con\\res\\gameover.png");public drawMap(){/*    if(type == 1 ){//老鹰setarg(375,700,1);}else if(type == 2){//土砖}else if(type == 3){//石砖}else if(type ==4 ){//大爆炸}*/}public void drawmap(int type,JFrame frame, Graphics g,int x,int y){if(type == 1){//画老鹰laoying.paintIcon(frame,g,x,y);}else if(type ==2 ){//画土砖tuzhuan.paintIcon(frame,g,x,y);}else if(type == 3) {//画石砖shizhuan.paintIcon(frame,g,x,y);}else if(type ==4 ){//画大爆炸explode2.paintIcon(frame,g,x,y);}else if(type == 5){//画小爆炸explode1.paintIcon(frame,g,x,y);}else if(type == 6){//画草丛cao.paintIcon(frame,g,x,y);}else if(type == 7){//画水shui.paintIcon(frame,g,x,y);}else if(type == 8) {//游戏结束gameover.paintIcon(frame,g,x,y);}}@Overridepublic void keyTyped(KeyEvent e) {}@Overridepublic void keyPressed(KeyEvent e) {}@Overridepublic void keyReleased(KeyEvent e) {}
}

//己方
//子弹

package tank_game;import testdata.SLinkList;import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;public class myButtet extends SLinkList implements KeyListener {ImageIcon bullet = new ImageIcon("E:\\张wp.c\\Eclipse Java\\tank\\.con\\res\\playerBullet.png");public myButtet(){ }@Overridepublic void keyTyped(KeyEvent e) { }@Overridepublic void keyPressed(KeyEvent e) { }@Overridepublic void keyReleased(KeyEvent e) { }/** 画子弹*/public void drawBullet(Frame frame,Graphics g, int x, int y){bullet.paintIcon(frame,g,x,y);}
}

//坦克
package tank_game;

import testdata.SLinkList;import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;public class myTank extends SLinkList implements KeyListener {int score;int life;ImageIcon up = new ImageIcon("E:\\张wp.c\\Eclipse Java\\tank\\.con\\res\\playerUp.png");ImageIcon down = new ImageIcon("E:\\张wp.c\\Eclipse Java\\tank\\.con\\res\\playerDown.png");ImageIcon right = new ImageIcon("E:\\张wp.c\\Eclipse Java\\tank\\.con\\res\\playerRight.png");ImageIcon left = new ImageIcon("E:\\张wp.c\\Eclipse Java\\tank\\.con\\res\\playerLeft.png");ImageIcon boom1 = new ImageIcon("E:\\张wp.c\\Eclipse Java\\tank\\.con\\res\\explode1.png");ImageIcon boom2 = new ImageIcon("E:\\张wp.c\\Eclipse Java\\tank\\.con\\res\\explode2.png");public int getScore() {return score;}public void setScore(int score) {this.score = score;}public int getLife() {return life;}public void setLife(int life) {this.life = life;}public myTank(){score = 0;life = 5;}public void drawTank(JFrame frame,Graphics g,int x,int y,int dir){//设置坦克位置switch(dir){case 1://上up.paintIcon(frame,g,x,y);break;case 2://下down.paintIcon(frame,g,x,y);break;case 3://左left.paintIcon(frame,g,x,y);break;case 4://右right.paintIcon(frame,g,x,y);break;}}public void drawBom1(Frame frame,Graphics g,int x,int y){boom1.paintIcon(frame,g,x,y);}public void drawBom2(Frame frame,Graphics g,int x,int y){boom2.paintIcon(frame,g,x,y);}@Overridepublic void keyTyped(KeyEvent e) {}@Overridepublic void keyPressed(KeyEvent e) {}@Overridepublic void keyReleased(KeyEvent e) {}
}

//敌方
//子弹
package tank_game;
import testdata.SLinkList;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;public class enemyButtet extends SLinkList implements KeyListener {ImageIcon bullet = new ImageIcon("E:\\张wp.c\\Eclipse Java\\tank\\.con\\res\\enemyBullet.png");public enemyButtet(){}@Overridepublic void keyTyped(KeyEvent e) { }@Overridepublic void keyPressed(KeyEvent e) { }@Overridepublic void keyReleased(KeyEvent e) { }/** 画子弹*/public void drawBullet(Frame frame, Graphics g, int x, int y){bullet.paintIcon(frame,g,x,y);}}

//坦克

package tank_game;import testdata.SLinkList;import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;public class enemyTank extends SLinkList implements KeyListener {int step;ImageIcon  Eup = new ImageIcon("E:\\张wp.c\\Eclipse Java\\tank\\.con\\res\\EtankUp.png");ImageIcon Edown = new ImageIcon("E:\\张wp.c\\Eclipse Java\\tank\\.con\\res\\EtankDown.png");ImageIcon Eleft = new ImageIcon("E:\\张wp.c\\Eclipse Java\\tank\\.con\\res\\EtankLeft.png");ImageIcon Eright = new ImageIcon("E:\\张wp.c\\Eclipse Java\\tank\\.con\\res\\EtankRight.png");ImageIcon Eboom1 = new ImageIcon("E:\\张wp.c\\Eclipse Java\\tank\\.con\\res\\explode1.png");ImageIcon Eboom2 = new ImageIcon("E:\\张wp.c\\Eclipse Java\\tank\\.con\\res\\explode2.png");public int getStep() {return step;}public void setStep(int step) {this.step = step;}public enemyTank(){this.step = 4;}public void drawTank(int dir, Frame frame,Graphics g,int x,int y){switch (dir){//上下左右case 1:Eup.paintIcon(frame,g,x,y);break;case 2:Edown.paintIcon(frame,g,x,y);break;case 3:Eleft.paintIcon(frame,g,x,y);break;case 4:Eright.paintIcon(frame,g,x,y);break;}}public void drawBom1(Frame frame,Graphics g,int x,int y){Eboom1.paintIcon(frame,g,x,y);}public void drawBom2(Frame frame,Graphics g,int x,int y){Eboom2.paintIcon(frame,g,x,y);}@Overridepublic void keyTyped(KeyEvent e) {}@Overridepublic void keyPressed(KeyEvent e) {}@Overridepublic void keyReleased(KeyEvent e) {}
}

//行动

package tank_game;
import testdata.Node;
import javax.swing.*;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.net.URI;
import java.net.URL;
import java.util.Random;public class action extends JFrame  implements KeyListener , ActionListener, MouseListener {static File f1 =new File("E:\\张wp.c\\Eclipse Java\\tank\\.con\\sound\\boom.wav");static File f2 = new File("E:\\张wp.c\\Eclipse Java\\tank\\.con\\sound\\tankPK.wav");int enemyTanks;//敌方坦克数量int useTanks;//添加的坦克数量JButton again;myTank player1;myTank player2;myButtet zidan;enemyTank Etanks;//敌方坦克enemyButtet Ezidan;//敌方子弹drawMap laoying;drawMap tuzhuan;drawMap shizhuan;drawMap caocong;drawMap shui;//游戏是否结束boolean isExist;//是否胜利boolean isWin;//是否爆炸boolean isBom;//Timer timer = new Timer(100, this);//播放音乐AudioClip clip;AudioClip bom;//击中后爆炸声public action() {this.setSize(800, 800);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setResizable(false);this.setTitle("坦克大战");this.getContentPane().setBackground(Color.BLACK);this.setFocusable(true);this.addKeyListener(this);init();this.setVisible(true);//这个一般放在最后显示窗口  --- 不然出现为null现象}public void addtanks(){Node node = null;if(useTanks==0){if(enemyTanks>4) {for (int i = 0; i < 4; i++) {node = new Node(175 + i * 150, 100+64, 2, 10, 50, 150);Etanks.addTail(node);useTanks++;}useTanks+=4;}else if(enemyTanks>0){for (int i = 0; i < enemyTanks; i++) {node = new Node(175 + i * 150, 100+64, 2, 10, 50, 150);Etanks.addTail(node);useTanks++;}useTanks+=enemyTanks;}else{isWin=true;}}}public void init() {//布局setLayout(null);//敌方坦克数量enemyTanks = 20;useTanks=0;//游戏是否结束isExist = false;isWin = false;isBom = false;/** 坦克*/player1 = new myTank();player2 = new myTank();//画子弹zidan = new myButtet();//初始化地方坦克数据Etanks = new enemyTank();//画子弹Ezidan = new enemyButtet();/** 画地图*///画老鹰laoying = new drawMap();//画土砖tuzhuan = new drawMap();//画石砖shizhuan = new drawMap();//画草丛caocong = new drawMap();//画水shui = new drawMap();//地图初始化initMap();//敌人初始化inittanks();//时间开始//重新开始again = new JButton();again.setFont(new Font("宋体",1,30));again.setText("重新开始");again.setBackground(Color.orange);again.setBounds(285,705,200,50);again.addMouseListener(this);this.add(again);//音乐loadSound();clip.loop();//循环播放playBom();timer.start();}/** 初始化坦克数据*/public void inittanks() {Node node;/** 我方坦克*/node = new Node(200, 600, 1, 20, 20, 100);player1.addTail(node);node = new Node(600, 600, 1, 20, 20, 100);player2.addTail(node);//敌方坦克/* for (int i = 1; i <= enemyTanks; i++) {node = new Node(int coordX, int coordY, int dir, int speed, int atk, int boold)}*/for (int i = 0; i < 4; i++) {node = new Node(175 + i * 150, 100+64, 2, 10, 10, 150);Etanks.addTail(node);useTanks++;}}/**初始化地图数据 */public void initMap() {Node node;//老鹰初始化数据node = new Node(1);laoying.addHead(node);//土砖初始化----int nx = 391 - 32;int ny = 716 - 32;int x = nx, y = ny;/** 基地土砖*/for (int i = 0; i < 2; i++) {x = nx;y += 32 * i;for (int j = 0; j < 3; j++) {if (x == 391 && y == 716) {System.out.println("跳过");} else {node = new Node(x, y, 100);tuzhuan.addTail(node);}x += 32;}}/** 外部土砖*/for (int i = 0; i < 5; i++) {for (int j = 0; j <6; j++) {node = new Node(100+150*i,120+32*j,100);tuzhuan.addTail(node);}node = new Node(100+150*i,120+32*6,100,false);shizhuan.addTail(node);}//石砖初始化for (int i = 0; i <= 3; i++) {node = new Node(120 + i * 32 - 32, 500 , 100, false);shizhuan.addTail(node);}for (int i = 0; i <=3 ; i++) {node = new Node(600+i*32,500,100,false);shizhuan.addTail(node);}for (int i = 0; i < 5; i++) {node = new Node(nx+i*32-32,ny-32-32,100,false);shizhuan.addTail(node);}//草丛初始化for (int i = 1; i <= 20; i++) {for (int j = 1; j < 3; j++) {node = new Node(120-64+i*32, 400 + j * 32);caocong.addTail(node);}}/** 水初始化*/for (int i = 0; i < 10; i++) {node = new Node(120+i*32+32*4,500);shui.addTail(node);}}/** 敌方坦克移动* */public void Move() {Random random  = new Random();Node node = null;int len;for (int i = 1; i <= Etanks.getLength(); i++) {/** 获取移动方向* */int moveDir = random.nextInt(13) + 1;//1-4 上下左右int x = Etanks.getarg(i, "coordX");int y = Etanks.getarg(i, "coordY");int speed = Etanks.getarg(i, "speed");int dir = Etanks.getarg(i, "dir");int atk = Etanks.getarg(i, "atk");int step = Etanks.getStep();//返回最大前进步数//375  700if (y < 500) {//向下,向左 , 向右moveDir = random.nextInt(10) + 3;}len = tankMove("enemy", x, y, dir, speed, i);if (step > 0) {step--;switch (dir) {case 1:if (y > 120) {if (len != -1) {y -= len;} else {y -= speed;}}break;case 2:if (y < 705) {if (len != -1) {y += len;} else {y += speed;}}break;case 3:if (x > 75) {if (len != -1) {x -= len;} else {x -= speed;}}break;case 4:if (x < 728) {if (len != -1) {x += len;} else {x += speed;}}}} else {step = 4;switch (moveDir) {case 1:case 2:dir = 1;break;case 8:case 5:case 6:dir = 2;break;case 3:case 9:case 10:dir = 3;break;case 4:case 7:case 11:dir = 4;break;}}if (moveDir == 12) {switch (dir) {case 2://上node = new Node(x, y + 14 + 5, speed, atk, dir);break;case 1://下node = new Node(x, y - 14 - 5, speed, atk, dir);break;case 3://左node = new Node(x - 14 - 5, y, speed, atk, dir);break;case 4://右node = new Node(x + 14 + 5, y, speed, atk, dir);break;}Ezidan.addTail(node);}/** 重新设置位置*/Etanks.setarg(i, x, y);/** 重新设置方向*/Etanks.setarg(i, dir, "dir");/** 重新设置步数*/Etanks.setStep(step);}}/** 坦克移动相差距离*/public int tankMove(String str,int tankX,int tankY,int dir,int speed ,int temp){int len = -1;int oldlen = -1;int x,y;/** 石砖*/for (int i = 1; i <= shizhuan.getLength(); i++) {x = shizhuan.getarg(i,"coordX");;y = shizhuan.getarg(i,"coordY");len = tankMoveJudge(tankX,tankY,dir,speed,x,y);if(len!=-1){if(oldlen==-1){oldlen=len;}else{oldlen=Math.min(oldlen,len);}}}/** 老鹰*/x = laoying.getarg(1,"coordX");y = laoying.getarg(1,"coordY");len = tankMoveJudge(tankX,tankY,dir,speed,x,y);if(len!=-1) {if (oldlen == -1) {oldlen = len;} else {oldlen = Math.min(oldlen, len);}}/** 土砖*/for (int i = 1; i <= tuzhuan.getLength(); i++) {x = tuzhuan.getarg(i, "coordX");y = tuzhuan.getarg(i, "coordY");len = tankMoveJudge(tankX, tankY, dir, speed, x, y);if (len != -1) {if (oldlen == -1) {oldlen = len;} else {oldlen = Math.min(oldlen, len);}}}/** 水*/for (int i = 1; i <= shui.getLength(); i++) {x = shui.getarg(i,"coordX");y = shui.getarg(i,"coordY");;len = tankMoveJudge(tankX,tankY,dir,speed,x,y);if(len!=-1){if(oldlen==-1){oldlen=len;}else{oldlen=Math.min(oldlen,len);}}}//p1for (int i = 1; i <= player1.getLength(); i++) {if (i == temp && str.equals("m1"))continue;x = player1.getarg(i, "coordX");y = player1.getarg(i, "coordY");len = tankMoveJudge(tankX, tankY, dir, speed, x, y);;if (len != -1) {if (oldlen == -1) {oldlen = len;} else {oldlen = Math.min(oldlen, len);}}}//p2for (int i = 1; i <= player2.getLength(); i++) {if(i==temp&&str.equals("m2"))continue;x = player2.getarg(i,"coordX");y = player2.getarg(i,"coordY");len = tankMoveJudge(tankX,tankY,dir,speed,x,y);if(len!=-1){if(oldlen==-1){oldlen=len;}else{oldlen=Math.min(oldlen,len);}}}//Etanksfor (int i = 1; i <= Etanks.getLength(); i++) {if (i == temp && str.equals("enemy"))continue;x = Etanks.getarg(i, "coordX");y = Etanks.getarg(i, "coordY");;len = tankMoveJudge(tankX, tankY, dir, speed, x, y);;if (len != -1) {if (oldlen == -1) {oldlen = len;} else {oldlen = Math.min(oldlen, len);}}}if(oldlen!=-1){return oldlen;}return -1;}/** 坦克移动距离判断*/public int tankMoveJudge(int tankX,int tankY,int dir,int speed,int x,int y){int  tx = tankX;int  ty = tankY;int len2,len3,len1,len = -1;if(dir == 1){ty -= speed;}else if(dir == 2){ty += speed;}else if(dir == 3){tx -= speed;}else if(dir == 4){tx += speed;}len2 = tx -x;len3 = ty -y;len1 = (int)Math.sqrt(len2*len2+len3*len3);if(len1<42){//最大移动距离if(dir == 1) {len = Math.abs((tankY - 14) - (y + 16));}else if(dir == 2) {len = Math.abs((tankY + 14) - (y - 16));}else if(dir ==3) {len = Math.abs((tankX - 14) - (x + 16));}else if(dir == 4){len = Math.abs((tankX + 14) - (x - 16));}if(len == 0) {if(len1<30){return 0;}else{return -1;}}if(len<speed)return len;}return -1;}/** 己方子弹*/public void myBulletsMove(){for (int i = 1; i <= zidan.getLength(); i++) {//子弹存储的是中心坐标int x = zidan.getarg(i,"coordX");int y = zidan.getarg(i,"coordY");int Dir = zidan.getarg(i,"dir");int speed = zidan.getarg(i,"speed");switch (Dir){case 1://上zidan.setarg(i, y-speed,"coordY");break;case 2://下zidan.setarg(i,y+speed,"coordY");break;case 3://左zidan.setarg(i, x-speed, "coordX");break;case 4://右zidan.setarg(i, x+speed, "coordX");break;}}for (int i = 1; i <= zidan.getLength(); i++) {if(zidan.getarg(i,"coordX")<40-5||zidan.getarg(i,"coordY")<90-5||zidan.getarg(i,"coordX")>10+750-5||zidan.getarg(i,"coordY")>742-5){zidan.delIndex(i);}}}/** 发射子弹*/public void shoot(int dir,int tankX,int tankY,int speed,int atk,String sign){Node node = null;switch (dir){case 1://上node = new Node(tankX,tankY- 14 - 5 ,speed,atk,dir);break;case 2://下node = new Node(tankX,tankY + 14 + 5,speed,atk,dir);break;case 3://左node = new Node(tankX-14-5,tankY,speed,atk,dir);break;case 4://右node = new Node(tankX+14+5,tankY,speed,atk,dir);break;}node.setSign(sign);zidan.addTail(node);}/** 敌方子弹*/public void enemyBulletsMove(){for (int i = 1; i <= Ezidan.getLength(); i++) {//子弹存储的是中心坐标int x = Ezidan.getarg(i,"coordX");int y = Ezidan.getarg(i,"coordY");int Dir = Ezidan.getarg(i,"dir");int speed = Ezidan.getarg(i,"speed");switch (Dir){case 1://上Ezidan.setarg(i, y-speed,"coordY");break;case 2://下Ezidan.setarg(i,y+speed,"coordY");break;case 3://左Ezidan.setarg(i, x-speed, "coordX");break;case 4://右Ezidan.setarg(i, x+speed, "coordX");break;}}for (int i = 1; i <= Ezidan.getLength(); i++) {if(Ezidan.getarg(i,"coordX")<40-5||Ezidan.getarg(i,"coordY")<90-5||Ezidan.getarg(i,"coordX")>10+750-5||Ezidan.getarg(i,"coordY")>742-5){Ezidan.delIndex(i);}}}Image offScreenImage = null;/** 己方攻击*/public void attack(Graphics g) {/** 子弹*/for (int i = 1; i <= zidan.getLength(); i++) {int x = zidan.getarg(i, "coordX");int y = zidan.getarg(i, "coordY");int atk = zidan.getarg(i,"atk");String sign = zidan.getSign(i);//两个中心之间相差两个矩形半径之和则相交--命中/** 老鹰坐标*/int lx = (375 + 375 + 32) / 2;int ly = (700 * 2 + 32) / 2;if (Math.sqrt((lx - x) * (lx - x) + (ly - y) * (ly - y)) < 5 + 16 && laoying.getarg(1, "blood") > 0) {isBom=true;//画爆炸laoying.drawmap(4, this, g, 359, 668);//子弹消失zidan.delIndex(i);//老鹰消失laoying.setarg(1, 0);isExist = true;}/** 土砖被击中*/for (int j = 1; j <= tuzhuan.getLength(); j++) {int tx = tuzhuan.getarg(j, "coordX");int ty = tuzhuan.getarg(j, "coordY");if (Math.sqrt((tx - x) * (tx - x) + (ty - y) * (ty - y)) < 5 + 16 && tuzhuan.getarg(j, "blood") > 0) {isBom=true;//子弹消失zidan.delIndex(i);//爆炸tuzhuan.drawmap(5, this, g, tx - 16, ty - 16);//被攻击掉血int blood = tuzhuan.getarg(j, "blood") - atk;System.out.println(blood);if (blood > 0) {tuzhuan.setarg(j, blood);} else {//删掉tuzhuan.delIndex(j);}break;}}/** 石砖被攻击*/for (int j = 1; j <= shizhuan.getLength(); j++) {int sx = shizhuan.getarg(j, "coordX");int sy = shizhuan.getarg(j, "coordY");if (Math.sqrt((sx - x) * (sx - x) + (sy - y) * (sy - y)) < 5 + 16 && shizhuan.getarg(j, "blood") > 0) {isBom=true;zidan.delIndex(i);shizhuan.drawmap(5, this, g, shizhuan.getarg(j, "coordX") - 14, shizhuan.getarg(j, "coordY") - 14);break;}}/** 敌方坦克被攻击*/for (int j = 1; j <= Etanks.getLength() ; j++) {int ex = Etanks.getarg(j,"coordX");int ey = Etanks.getarg(j,"coordY");if (Math.sqrt((ex - x) * (ex - x) + (ey - y) * (ey - y)) < 5 + 14 && Etanks.getarg(j, "blood") > 0) {isBom=true;//子弹消失zidan.delIndex(i);//爆炸Etanks.drawBom1( this, g, ex - 14, ey - 14);//被攻击掉血int blood = Etanks.getarg(j, "blood") - atk;System.out.println(blood);if (blood > 0) {Etanks.setarg(j, blood);} else {Etanks.drawBom2( this, g, ex - 14, ey - 14);//删掉//击败得分if(sign == "p1"){player1.setScore(player1.getScore()+20);}else{player2.setScore(player2.getScore()+20);}Etanks.delIndex(j);enemyTanks--;useTanks--;}break;}}}}/** 敌方攻击*/public void Eattack(Graphics g){/** 子弹*/for (int i = 1; i <= Ezidan.getLength(); i++) {int x = Ezidan.getarg(i, "coordX");int y = Ezidan.getarg(i, "coordY");int atk = Ezidan.getarg(i,"atk");//两个中心之间相差两个矩形半径之和则相交--命中/** 老鹰坐标*/int lx = (375 + 375 + 32) / 2;int ly = (700 * 2 + 32) / 2;if (Math.sqrt((lx - x) * (lx - x) + (ly - y) * (ly - y)) < 5 + 16 && laoying.getarg(1, "blood") > 0) {//isBom=true;//画爆炸laoying.drawmap(4, this, g, 359, 668);//子弹消失zidan.delIndex(i);//老鹰消失laoying.setarg(1, 0);;isExist = true;break;}/** 土砖被击中*/for (int j = 1; j <= tuzhuan.getLength(); j++) {int tx = tuzhuan.getarg(j, "coordX");int ty = tuzhuan.getarg(j, "coordY");if (Math.sqrt((tx - x) * (tx - x) + (ty - y) * (ty - y)) < 5 + 16 && tuzhuan.getarg(j, "blood") > 0) {//子弹消失// isBom=true;//爆炸tuzhuan.drawmap(5, this, g, tx - 16, ty - 16);//被攻击掉血int blood = tuzhuan.getarg(j, "blood") - atk;//System.out.println(blood);if (blood > 0) {tuzhuan.setarg(j, blood);} else {//删掉tuzhuan.delIndex(j);}Ezidan.delIndex(i);break;}}/** 石砖被攻击*/for (int j = 1; j <= shizhuan.getLength(); j++) {int sx = shizhuan.getarg(j, "coordX");int sy = shizhuan.getarg(j, "coordY");if (Math.sqrt((sx - x) * (sx - x) + (sy - y) * (sy - y)) < 5 + 16 && shizhuan.getarg(j, "blood") > 0) {// isBom=true;Ezidan.delIndex(i);shizhuan.drawmap(5, this, g, shizhuan.getarg(j, "coordX") - 14, shizhuan.getarg(j, "coordY") - 14);break;}}/** 己方坦克被攻击*/for (int j = 1; j <= player1.getLength() ; j++) {int ex = player1.getarg(j,"coordX");int ey = player1.getarg(j,"coordY");
;if (Math.sqrt((ex - x) * (ex - x) + (ey - y) * (ey - y)) < 5 + 14 && player1.getarg(j, "blood") > 0) {//  isBom=true;//子弹消失Ezidan.delIndex(i);//爆炸player1.drawBom1( this, g, ex - 14, ey - 14);//被攻击掉血int blood = player1.getarg(j, "blood") - atk;//System.out.println(blood);if (blood > 0) {player1.setarg(j, blood);} else {player1.drawBom2( this, g, ex - 14, ey - 14);//删掉if(player1.getLife() == 0)player1.delIndex(j);else{player1.setLife(player1.getLife()-1);player1.setarg(j,100,"blood");player1.setarg(j,200,600);}}break;}}for (int j = 1; j <= player2.getLength() ; j++) {int ex = player2.getarg(j,"coordX");int ey = player2.getarg(j,"coordY");if (Math.sqrt((ex - x) * (ex - x) + (ey - y) * (ey - y)) < 5 + 14 && player2.getarg(j, "blood") > 0) {//  isBom=true;//子弹消失Ezidan.delIndex(i);//爆炸player2.drawBom1( this, g, ex - 14, ey - 14);//被攻击掉血int blood = player2.getarg(j, "blood") - atk;//System.out.println(blood);if (blood > 0) {player2.setarg(j, blood);} else {player2.drawBom2( this, g, ex - 14, ey - 14);//删掉if(player2.getLife() == 0)player2.delIndex(j);else{player2.setLife(player2.getLife()-1);;player2.setarg(j,100,"blood");player2.setarg(j,600,600);}}break;}}}}public void Tmove(int keyCode){int len = 0;/** player1*/for (int i = 1; i <= player1.getLength() ; i++) {int dir =player1.getarg(i,"dir");int tankX = player1.getarg(i,"coordX");int tankY = player1.getarg(i,"coordY");int speed = player1.getarg(i,"speed");int atk =player1.getarg(i,"atk");//返回最大前进步数len = tankMove("m1",tankX,tankY,dir,speed,i);if(keyCode=='w'||keyCode=='W'){if((dir!=1||(dir==1&&tankY<100+20))){dir=1;}else if(len!=-1){tankY -= len;}else{tankY -=speed;}} else if(keyCode == 's'|| keyCode == 'S'){if((dir!=2||(tankY>700+4&&dir==2))){dir=2;}else if(len!=-1){tankY += len;}else{tankY +=speed;}}else if(keyCode =='a' || keyCode =='A'){if(dir!=3||(dir==3&&tankX<50+25)){dir=3;}else if(len!=-1){tankX -= len;}else{tankX-=speed;}}else if(keyCode =='d' || keyCode == 'D'){if(dir!=4||(dir==4&&tankX>728)){dir=4;}else if(len!=-1){tankX += len;}else{tankX +=speed;}}player1.setarg(i,tankX,tankY,speed,atk,dir);if(keyCode == KeyEvent.VK_SPACE|| keyCode == 'j'|| keyCode == 'J'){shoot(dir,tankX,tankY,speed,atk,"p1");}}/** player2*/for (int i = 1; i <= player2.getLength() ; i++) {int dir =player2.getarg(i,"dir");int tankX = player2.getarg(i,"coordX");int tankY = player2.getarg(i,"coordY");int speed = player2.getarg(i,"speed");int atk =player2.getarg(i,"atk");//返回最大前进步数len = tankMove("m2",tankX,tankY,dir,speed,i);if(keyCode == KeyEvent.VK_UP){if((dir!=1||(dir==1&&tankY<100+20))){dir=1;}else if(len!=-1){tankY -= len;}else{tankY -= speed;}} else if(keyCode == KeyEvent.VK_DOWN){if((dir!=2||(tankY>700+4&&dir==2))){dir=2;}else if(len!=-1){tankY+=len;}else{tankY+=speed;}}else if(keyCode == KeyEvent.VK_LEFT){if(dir!=3||(dir==3&&tankX<50+25)){dir=3;}else if(len!=-1){tankX-=len;}else{tankX-=speed;}}else if(keyCode == KeyEvent.VK_RIGHT){if(dir!=4||(dir==4&&tankX>728)){dir=4;}else if(len!=-1){tankX+=len;}else{tankX+=speed;}}player2.setarg(i,tankX,tankY,speed,atk,dir);if(keyCode == 96){shoot(dir,tankX,tankY,speed,atk,"p2");}}}/** 播放总音乐*/public void loadSound(){try {URI uri = f2.toURI();URL url = uri.toURL();clip= Applet.newAudioClip(url);}catch (Exception e){}}/** 爆炸声*/public void playBom(){try {URI uri = f1.toURI();URL url = uri.toURL();bom= Applet.newAudioClip(url);}catch (Exception e){}}@Overridepublic void update(Graphics g) {//1.得到缓冲图象if(this.offScreenImage==null){this.offScreenImage = this.createImage(800,800);}//2.得到缓冲图象的画笔Graphics gOffScreen = this.offScreenImage.getGraphics();//3.绘制缓冲图象Color c = gOffScreen.getColor();gOffScreen.setColor(Color.black);gOffScreen.fillRect(0, 0, 800,800);gOffScreen.setColor(c);//4.调用paint(),将缓冲图象的画笔传入paint(gOffScreen);//5.再将此缓冲图像一次性绘到代表屏幕的Graphics对象,即该方法传入的“g”上g.drawImage(offScreenImage, 0, 0, null);}@Overridepublic void paint(Graphics g){if(isExist){laoying.drawmap(8,this,g,275,350);return;}super.paint(g);//己方坦克//p1for (int i = 1; i <= player1.getLength() ; i++) {int x = player1.getarg(i,"coordX");int y =player1.getarg(i,"coordY");int dir = player1.getarg(i,"dir");player1.drawTank(this,g,x-14,y-14,dir);}//p2for (int i = 1; i <= player2.getLength() ; i++) {int x = player2.getarg(i,"coordX");int y =player2.getarg(i,"coordY");int dir = player2.getarg(i,"dir");player2.drawTank(this,g,x-14,y-14,dir);;}//画水for (int i = 1; i <= shui.getLength() ; i++) {int x = shui.getarg(i,"coordX");int y =shui.getarg(i,"coordY");shui.drawmap(7,this,g,x-16,y-16);}//己方子弹for (int i = 1; i <= zidan.getLength(); i++) {int x=zidan.getarg(i,"coordX");int y=zidan.getarg(i,"coordY");zidan.drawBullet(this,g,x-5,y-5);}//己方老鹰if(laoying.getarg(0,"blood")==1)laoying.drawmap(1,this,g,375,700);//画地图//画土砖for (int i = 1; i <= tuzhuan.getLength(); i++) {int x= tuzhuan.getarg(i,"coordX");int y= tuzhuan.getarg(i,"coordY");int blood = tuzhuan.getarg(i,"blood");if(blood>0){tuzhuan.drawmap(2,this,g,x-16,y-16);}}//画石砖for (int i = 1; i <= shizhuan.getLength() ; i++) {int x= shizhuan.getarg(i,"coordX");int y= shizhuan.getarg(i,"coordY");int blood = shizhuan.getarg(i,"blood");if(blood>0){shizhuan.drawmap(3,this,g,x-16,y-16);}}/** 敌人** *//** 敌方坦克*/for (int i = 1; i <= Etanks.getLength(); i++) {int x = Etanks.getarg(i,"coordX");int y = Etanks.getarg(i,"coordY");int blood = Etanks.getarg(i,"blood");int dir = Etanks.getarg(i,"dir");if(blood>0){Etanks.drawTank(dir,this,g,x-14,y-14);}}/** 敌方子弹*/for (int i = 1; i <= Ezidan.getLength(); i++) {int x=Ezidan.getarg(i,"coordX");int y=Ezidan.getarg(i,"coordY");Ezidan.drawBullet(this,g,x-5,y-5);}//画草丛for (int i = 1; i <= caocong.getLength() ; i++) {int x= caocong.getarg(i,"coordX");int y= caocong.getarg(i,"coordY");caocong.drawmap(6,this,g,x-16,y-16);}//攻击后变化attack(g);Eattack(g);//限制地图g.setColor(Color.ORANGE);g.setFont(new Font("宋体",1,30));g.drawLine(50,100,750,100);g.drawLine(50,732,750,732);g.drawLine(50,100,50,732);g.drawLine(750,100,750,732);/** 画分数*/g.drawString("p1 score:"+player1.getScore(),100,60);g.drawString("p2 score:"+player2.getScore(),450,60);/** 画生命*/g.drawString("p1 life:"+player1.getLife(),100,90);g.drawString("p2 life:"+player2.getLife(),450,90);}@Overridepublic void keyTyped(KeyEvent e) {int keyCode = e.getKeyCode();}@Overridepublic void keyPressed(KeyEvent e) {int keyCode = e.getKeyCode();}@Overridepublic void keyReleased(KeyEvent e) {int keyCode = e.getKeyCode();Tmove(keyCode);}@Overridepublic void actionPerformed(ActionEvent e) {if(!isExist) {enemyBulletsMove();myBulletsMove();Move();if(isBom){isBom=false;bom.play();}if(!isWin){addtanks();//胜利}else if(useTanks == 0){JOptionPane.showConfirmDialog(null,"胜利了",null,JOptionPane.OK_OPTION);isWin=false;useTanks=1;}}else{clip.stop();bom.stop();}repaint();}@Overridepublic void mouseClicked(MouseEvent e) {}@Overridepublic void mousePressed(MouseEvent e) {}@Overridepublic void mouseReleased(MouseEvent e) {dispose();clip.stop();bom.stop();new action();}@Overridepublic void mouseEntered(MouseEvent e) {}@Overridepublic void mouseExited(MouseEvent e) {}
}

//主函数

package tank_game;
public class main {public static void main(String[] args) {new action();}
}

如需需要图片、音频资源请私聊

java制作坦克大战相关推荐

  1. 如何用java让坦克发射子弹_java怎么制作坦克大战

    对于一些小游戏的实现方法是很多小伙伴们都好奇的,也有很多小游戏是由Java而制作的,那么你知道java怎么制作坦克大战吗?接下来的内容中我们就一起去看看吧. 详情请参照注释,这里就不多废话了,实现一下 ...

  2. java小组坦克大战游戏开发文档开发日志_java实现坦克大战游戏

    本文实例为大家分享了java实现坦克大战游戏的具体代码,供大家参考,具体内容如下 一.实现的功能 1.游戏玩法介绍 2.自定义游戏(选择游戏难度.关卡等) 3.自定义玩家姓名 4.数据的动态显示 二. ...

  3. 微信小游戏制作坦克大战(四)添加敌方坦克,敌方坦克可以随机移动

    微信小游戏制作坦克大战(四)添加敌方坦克,敌方坦克可以随机移动 首先导入敌方坦克素材 重命名为敌方坦克1 敌方坦克也移动到屏幕外面,后面使用克隆体来显示. 我们给敌方坦克添加事件 好的,现在敌方坦克已 ...

  4. 微信小游戏制作坦克大战(六)碰撞检测,主角坦克碰到敌方坦克、炮弹爆炸

    微信小游戏制作坦克大战(六)碰撞检测,主角坦克碰到敌方坦克.炮弹爆炸 导入坦克爆炸效果的图片和声音素材 给主角坦克添加事件 给爆炸动画添加事件 当主角坦克碰到敌方坦克或者炮弹时显示爆炸效果 下一篇文章 ...

  5. 基于Java的坦克大战游戏的设计与实现(论文+PPT+源码)

    幻灯片1 基于Java的坦克大战游戏的设计与实现 幻灯片2 CONTENTS 1 4 设计工具与相关技术 详细设计 2 5 系统分析 结论 3 总体设计 幻灯片3 PPT模板下载:http://www ...

  6. 微信小游戏制作坦克大战(五)敌方坦克可以发射炮弹

    微信小游戏制作坦克大战(五)敌方坦克可以发射炮弹 在资源管理器中复制炮弹,重命名为敌人坦克的炮弹. 修改敌方坦克的积木 给敌方坦克炮弹添加事件 现在,敌方坦克已经可以自动发射炮弹啦. 下一篇文章:微信 ...

  7. Java实现坦克大战,单机版和联网版

    Java实现坦克大战 源码获取途径 部分源代码 源码获取途径 百度网盘链接: 百度网盘地址 提取码:5r7i GitHub Github获取地址 部分源代码 public class TankClie ...

  8. 【JAVA程序设计】基于JAVA的坦克大战小游戏--入门级小游戏

    基于JAVA的坦克大战小游戏--入门级小游戏 零.项目获取 一.项目简介 二.开发环境 三.游戏玩法 四.运行截图 零.项目获取 获取方式(点击下载):是云猿实战 项目经过多人测试运行,可以确保100 ...

  9. java坦克大战登录界面设计_基于JAVA的坦克大战设计和实现-代码.doc

    JISHOU UNIVERSITY 本科生毕业设计 题 目:基于JAVA的坦克大战设计与实现作 者:学 号:所属学院:专业年级:指导教师:职 称:完成时间:2012年5月7日 吉首大学 基于JAVA的 ...

最新文章

  1. 视频与图像RGB/YUV格式详解
  2. egg.js java 生产数据_Egg 2.15.0 发布,阿里开源的企业级 Node.js 框架
  3. 第17章:图像分割提取
  4. Kafka精华问答 | Kafka有哪些使用场景?
  5. 物联网碰到云计算会怎么样?
  6. Win32的虚拟内存分配函数
  7. 大讲堂专访丨连接Oracle DBA与开发的桥梁:Oracle的redo与undo
  8. mybatis-plus主键生成策略
  9. visual studio 显示行号
  10. 初中计算机vb教程视频教程,关于中学信息技术vb的教学
  11. probability是什么意思_概率(Probability)的本质是什么?
  12. 关于统计分析软件Spss统计个案数和实际数据的个案数不一致问题
  13. android 多开app store,苹果手机如何做到微信双开?
  14. windows搜索文件内容的软件推荐
  15. PACS—医学影像信息化的基础
  16. 【烈日炎炎战后端】计算机网络(4.2万字)
  17. 大局已定,应届生三面京东成功拿下20K的Offer。
  18. C++中find()函数用法
  19. 【Lintcode】1367. Police Distance
  20. 组装计算机的游戏,如何组装游戏电脑

热门文章

  1. Objective-C学习资源
  2. 微信摇一摇插件ios_iOS-仿微信摇一摇
  3. 怎样将语音转换成文字
  4. 网络流:最大流,最小割 基本概念及算法
  5. uint8array和string的互转(包括中文字符串)
  6. js--计算两个时间相差几年、几月、几日
  7. 拉格朗日插值法 【python】
  8. python中文居中对齐处理
  9. vscode中选中多行多光标进行操作及一些常用的命令(快捷键)
  10. 一篇文章学会使用 CompletableFuture(JDK9)