urggr
urggr
2017-10-29 07:01

(菜 鸟提问,坦克大战问题,击中敌方坦克,全部消失,(只能击中左边的坦克,)谢谢啊)

  • java

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

import javax.swing.*;

public class I_event8 extends JFrame{
MyPanel mp;
public static void main(String[] args){
I_event8 i=new I_event8();
}
public I_event8(){
mp=new MyPanel();
this.add(mp);
this.addKeyListener(mp);
Thread t=new Thread(mp);
t.start();

    this.setTitle("卧槽");
    this.setLocation(300,300);
    this.setSize(500,400);
    //this.setResizable(false);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
}

}
class MyPanel extends JPanel implements KeyListener,Runnable{
MyTank mt=null;
Vector dtk=new Vector();
int tkNum=3;

public MyPanel(){
    mt=new MyTank(100,300);
    for(int i=0;i<tkNum;i++){
        DiTank dt=new DiTank((i)*230,0);
        dtk.add(dt);
    }
}
public void paint(Graphics g){
    super.paint(g);
    g.fillRect(0,0,500,400);

    this.DrawTank(mt.getX(),mt.getY(),g,0,this.mt.direction);
    for(int i=0;i<dtk.size();i++){
        DiTank dt=dtk.get(i);
        if(dtk.get(i).shengming){
            this.DrawTank(dtk.get(i).getX(),dtk.get(i).getY(),g,1,dtk.get(i).direction);
        }
    }
    for(int i=0;i<mt.aaa.size();i++){

        Bullet zd=mt.aaa.get(i);

        if(mt.zd!=null&&zd.shengming==true){

            g.setColor(Color.RED);
            g.fill3DRect(zd.x,zd.y,3,3,false);

        }if(zd.shengming==false){

            mt.aaa.remove(zd);
        }
    }
}

public void DrawTank(int x,int y,Graphics g,int type,int direction){
    switch(type){
    case 0:
        g.setColor(Color.YELLOW);
        break;
    case 1:
        g.setColor(Color.GREEN);
        break;
    }
    switch(direction){
    case 0:
        g.fill3DRect(x,y,8,30,false);
        g.fill3DRect(x+8,y+10,7,15,false);
        g.fill3DRect(x+15,y,8,30,false);
        g.drawOval(x+8,y+15,6,6);
        g.drawLine(x+11,y+17,x+11,y-1);
        break;
    case 1:
        g.fill3DRect(x,y,30,8,false);
        g.fill3DRect(x+8,y+8,15,7,false);
        g.fill3DRect(x,y+15,30,8,false);
        g.drawOval(x+13,y+8,6,6);
        g.drawLine(x+15,y+11,x+30,y+11);
        break;
    case 2:
        g.fill3DRect(x,y,8,30,false);
        g.fill3DRect(x+8,y+10,7,15,false);
        g.fill3DRect(x+15,y,8,30,false);
        g.drawOval(x+8,y+15,6,6);
        g.drawLine(x+11,y+17,x+11,y+30);
        break;
    case 3:
        g.fill3DRect(x,y,30,8,false);
        g.fill3DRect(x+8,y+8,15,7,false);
        g.fill3DRect(x,y+15,30,8,false);
        g.drawOval(x+13,y+8,6,6);
        g.drawLine(x+15,y+11,x-1,y+11);
        break;
    }
}
@Override
public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub

}
@Override
public void keyPressed(KeyEvent e) {
    // TODO Auto-generated method stub
    if(e.getKeyCode()==KeyEvent.VK_W){
        this.mt.setDirection(0);
        this.mt.up();
    }else if(e.getKeyCode()==KeyEvent.VK_D){
        this.mt.setDirection(1);
        this.mt.turnRight();
    }else if(e.getKeyCode()==KeyEvent.VK_S){
        this.mt.setDirection(2);
        this.mt.down();
    }else if(e.getKeyCode()==KeyEvent.VK_A){
        this.mt.setDirection(3);
        this.mt.turnLeft();
    }
    if(e.getKeyCode()==KeyEvent.VK_J){
        if(mt.aaa.size()<8){
            this.mt.fire();
        }
    }
    this.repaint();
}
@Override
public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub

}
@Override
public void run() {
    // TODO Auto-generated method stub
    while(true){
        try{
            Thread.sleep(100);
        }
        catch(Exception e){}
        for(int i=0;i<mt.aaa.size();i++){
            Bullet zd=mt.aaa.get(i);
            if(zd.shengming){
                for(int j=0;j<dtk.size();j++){
                    DiTank dt=dtk.get(j);
                    if(dt.shengming){
                        this.HitEnemy(zd, dt);
                    }
                }
            }
        }
        this.repaint();
    }
}
public void HitEnemy(Bullet zd,DiTank dt){
    for(int i=0;i<dtk.size();i++){
        switch(dtk.get(i).direction){
        case 0:
        case 2:
            if(zd.x>dtk.get(i).x&&zd.x<dtk.get(i).x+23&&zd.y>dtk.get(i).y&&zd.y<dtk.get(i).y+30){
                zd.shengming=false;
                dtk.get(i).shengming=false;
            }
            break;
        case 1:
        case 3:
            if(zd.x>dt.x&&zd.x<dt.x+30&&zd.y>dt.y&&zd.y<dt.y+23){
                zd.shengming=false;
                dtk.get(i).shengming=false;
            }
            break;
        }
    }
}

}

class Tank{
//Vector aaa=new Vector();
int x=0;
int y=0;
int speed=4;
int direction;
boolean shengming=true;

protected int getSpeed() {
    return speed;
}
protected void setSpeed(int speed) {
    this.speed=speed;
}
public int getDirection() {
    return direction;
}
public void setDirection(int direction) {
    this.direction = direction;
}

public Tank(int x,int y){
    this.x=x;
    this.y=y;
}
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 void paint(Graphics g){

}

}
class MyTank extends Tank{
Vector aaa=new Vector();
Bullet zd=null;
public MyTank(int x,int y){
super(x,y);
}
public void fire(){
switch(this.direction){
case 0:
zd=new Bullet(x+10,y,0);
aaa.add(zd);
break;
case 1:
zd=new Bullet(x+28,y+10,1);
aaa.add(zd);
break;
case 2:
zd=new Bullet(x+10,y+31,2);
aaa.add(zd);
break;
case 3:
zd=new Bullet(x-1,y+10,3);
aaa.add(zd);
break;
}
Thread t=new Thread(zd);
t.start();
}
public void up(){
y-=speed;
}
public void turnRight(){
x+=speed;
}
public void down(){
y+=speed;
}
public void turnLeft(){
x-=speed;
}
}
class DiTank extends Tank{
int x=0;
int y=0;
int speed=4;
int direction;
public DiTank(int x,int y){
super(x,y);
}
}
class Bullet implements Runnable{
int x;
int y;
int speed=2;
int direction;
boolean shengming=true;
public Bullet(int x,int y,int direction){
this.x=x;
this.y=y;
this.direction=direction;
}
@Override
public void run() {
while(true){
try{
Thread.sleep(50);
}
catch(Exception e){}

        // TODO Auto-generated method stub
        switch(direction){
        case 0:
            y-=speed;
            break;
        case 1:
            x+=speed;
            break;
        case 2:
            y+=speed;
            break;
        case 3:
            x-=speed;
            break;
        }
        if(x<0||x>500||y<0||y>400){
            this.shengming=false;
            break;
        }
    }
}

}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答