//存储数据
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();
else
return 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);
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public 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(){ }
@Override
public void keyTyped(KeyEvent e) { }
@Override
public void keyPressed(KeyEvent e) { }
@Override
public 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);
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public 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(){}
@Override
public void keyTyped(KeyEvent e) { }
@Override
public void keyPressed(KeyEvent e) { }
@Override
public 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);
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public 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 700
if (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);
}
}
}
//p1
for (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);
}
}
}
//p2
for (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);
}
}
}
//Etanks
for (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){}
}
@Override
public 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);
}
@Override
public void paint(Graphics g){
if(isExist){
laoying.drawmap(8,this,g,275,350);
return;
}
super.paint(g);
//己方坦克
//p1
for (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);
}
//p2
for (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);
}
@Override
public void keyTyped(KeyEvent e) {
int keyCode = e.getKeyCode();
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
}
@Override
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
Tmove(keyCode);
}
@Override
public 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();
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
dispose();
clip.stop();
bom.stop();
new action();
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
//主函数
package tank_game;
public class main {
public static void main(String[] args) {
new action();
}
}
如需需要图片、音频资源请私聊