weixin_41731967 2018-03-01 15:40 采纳率: 12.5%
浏览 1366
已结题

java 坦克大战 子弹击中敌方坦克 子弹消失 坦克不会消失

import java.util.Vector;

public class MyTank {
int x=0;
int y=0;
int direction=0;
int sudu=2;
public MyTank(){

}
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 getDirection() {
    return direction;
}
public void setDirection(int direction) {
    this.direction = direction;
}
public int getSudu() {
    return sudu;
}
public void setSudu(int sudu) {
    this.sudu = sudu;
}
public MyTank(int x,int y,int direction,int sudu){
    this.x=x;
    this.y=y;
    this.direction=direction;
    this.sudu=sudu;
}
public MyTank(int x,int y,int direction){
    this.x=x;
    this.y=y;
    this.direction=direction;
}
public MyTank(int x,int y){
    this.x=x;
    this.y=y;
}

public void shang(){
    y-=sudu;
    this.setY(y);
}

public void xia(){
    y+=sudu;
    this.setY(y);
}
public void zuo(){
    x-=sudu;
    this.setX(x);
}
public void you(){
    x+=sudu;
    this.setX(x);
}

Zd zd=new Zd(this.getX(),this.getY(),this.direction);
Vector<Zd> zds=new Vector<Zd>();
//判断发射方向
public void fs(){
    switch(this.direction){
    case 0:
        zd=new Zd(x+8,y-5,this.direction);
        zds.add(zd);
        break;
    case 1:
        zd=new Zd(x+8,y+28,this.direction);
        zds.add(zd);
        break;
    case 2:
        zd=new Zd(x-5,y+8,this.direction);
        zds.add(zd);
        break;
    case 3:
        zd=new Zd(x+28,y+8,this.direction);
        zds.add(zd);
        break;
    }
    Thread t=new Thread(zd);
    t.start();
}

}
//子弹类
class Zd implements Runnable{
int x=0;
int y=0;
int direction=0;
int sudu=8;
boolean shengming=true;
public Zd(int x,int y){
this.x=x;
this.y=y;
}
public Zd(int x,int y,int direction){
this.x=x;
this.y=y;
this.direction=direction;
}
public void run(){
while(true){
try {
Thread.sleep(100);
} catch (Exception e) {
}
switch(direction){
case 0:
y-=sudu;
break;
case 1:
y+=sudu;
break;
case 2:
x-=sudu;
break;
case 3:
x+=sudu;
break;
}
//判断子弹出界面
if(x500||y400){
this.shengming=false;
}
}

}

}


//敌方坦克类
public class EnemyTank {
int x=0;
int y=0;
int direction=0;
int sudu=5;
boolean shengming=true;

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 getDirection() {
    return direction;
}
public void setDirection(int direction) {
    this.direction = direction;
}
public int getSudu() {
    return sudu;
}
public void setSudu(int sudu) {
    this.sudu = sudu;
}

public EnemyTank(int x,int y,int direction){
    this.x=x;
    this.y=y;
    this.direction=direction;
}

public EnemyTank(int x,int y){
    this.x=x;
    this.y=y;
}

}


import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Tank1 {

public static void main(String[] args) {
    MianBan mb=new MianBan();
}

}
class MianBan extends JFrame{
HuaBi hb=null;
public MianBan(){
this.setSize(500,300);
this.setLocation(500, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    hb=new HuaBi();
    this.add(hb);
    this.addKeyListener(hb);
    this.setVisible(true);
}

}

class HuaBi extends JPanel implements KeyListener ,Runnable{
MyTank mt=null;
public HuaBi(){
mt=new MyTank(140,232);
}

public void paint(Graphics g){
    super.paint(g);
    g.fillRect(0, 0, 500, 400); //背景

    //我放坦克
    this.huaTank(mt.getX(), mt.getY(), g, 1, this.mt.getDirection());

    //子弹
    for(int i=0;i<mt.zds.size();i++){
        Zd zd=mt.zds.get(i);
        if(mt.zd!=null&&mt.zd.shengming==true){
            g.setColor(Color.white);
            g.fill3DRect(zd.x,zd.y, 3, 3, false);
        }
        if(zd.shengming==false){
            mt.zds.remove(i);
        }
    }       

    //敌方坦克
    Vector<EnemyTank> dtk=new Vector<EnemyTank>();
    EnemyTank et=null;
    int num=3;
    for(int i=0;i<num;i++){
        et=new EnemyTank((i)*226+6,10,1);
        dtk.add(et);
    }

    //绘制敌方坦克
    for(int i=0;i<dtk.size();i++){
        et=dtk.get(i);
        if(et!=null&&et.shengming==true){
            this.huaTank(et.getX(), et.getY(), g, 0, 1);
        }
        if(et.shengming==false){
            dtk.remove(et);
        }
    }

    //比较子弹和坦克坐标
    for(int i=0;i<mt.zds.size();i++){
        for(int j=0;j<dtk.size();j++){
            if(mt.zds.get(i).shengming==true){
                if(dtk.get(j).shengming==true){
                    this.jzdf(mt.zds.get(i), dtk.get(j));
                }
            }
        }
    }
    this.repaint();
}

//判断子弹是否击中敌方坦克
public void jzdf(Zd zd,EnemyTank et){
    if(zd.x>et.x&&zd.x<et.x+20&&zd.y>et.y&&zd.y<et.y+30){
        zd.shengming=false;
        et.shengming=false;
    }
}

//fx为坦克方向
public void huaTank(int x,int y,Graphics g,int color,int fx){
    switch(color){
    case 0:
        g.setColor(Color.green);
        break;
    case 1:
        g.setColor(Color.yellow);
        break;
    }
    //坦克方向
    switch(fx){
    case 0: //上
        g.fill3DRect(x, y, 5, 26, false);
        g.fill3DRect(x+15, y, 5, 26, false);
        g.fill3DRect(x+4, y+4, 16, 20, false);
        g.fillOval(x+4, y+8, 10, 10);
        g.drawLine(x+9, y+15, x+9, y-4);
        break;
    case 1:
        //下
        g.fill3DRect(x, y, 5, 26, false);
        g.fill3DRect(x+15, y, 5, 26, false);
        g.fill3DRect(x+4, y+1, 16, 20, false);
        g.fillOval(x+4, y+5, 10, 10);
        g.drawLine(x+9, y+15, x+9, y+27);
        break;
    case 2:
        //左
        g.fill3DRect(x, y, 26, 5, false);
        g.fill3DRect(x, y+15, 26, 5, false);
        g.fill3DRect(x+4, y+4, 20, 16, false);
        g.fillOval(x+10, y+5, 10, 10);
        g.drawLine(x+15, y+9, x-4, y+9);
        break;
    case 3:
        //右
        g.fill3DRect(x, y, 26, 5, false);
        g.fill3DRect(x, y+15, 26, 5, false);
        g.fill3DRect(x+1, y+4, 20, 16, false);
        g.fillOval(x+5, y+5, 10, 10);
        g.drawLine(x+15, y+9, x+27, y+9);
        break;
    }
    this.repaint();
}
public void keyTyped(KeyEvent e) {

}
public void keyPressed(KeyEvent e) {
    if(e.getKeyCode()==KeyEvent.VK_W){

// System.out.println("上");
this.mt.setDirection(0);
this.mt.shang();
}else if(e.getKeyCode()==KeyEvent.VK_A){
// System.out.println("左");
this.mt.zuo();
this.mt.setDirection(2);
}else if(e.getKeyCode()==KeyEvent.VK_D){
// System.out.println("右");
this.mt.you();
this.mt.setDirection(3);
}else if(e.getKeyCode()==KeyEvent.VK_S){
// System.out.println("下");
this.mt.setDirection(1);
this.mt.xia();
}
if(e.getKeyCode()==KeyEvent.VK_J){
if(mt.zds.size()<5){
this.mt.fs();
}
}
this.repaint();
}
public void keyReleased(KeyEvent e) {

}

@Override
public void run() {

}

}

  • 写回答

2条回答

  • qq_15906587 2018-03-02 01:36
    关注

    专门写一个存坦克生命值的类,你给坦克设定生命值,如果被命中一次,生命值减一,减到0就让坦克消失;设置一个事件,当子弹碰撞到坦克,就让存坦克的图片消失

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog