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 zds=new Vector();
//判断发射方向
public void fs(){
switch(this.direction){
case 0:
zd=new Zd(x+8,y-5,this.direction);
break;
case 1:
zd=new Zd(x+8,y+28,this.direction);
break;
case 2:
zd=new Zd(x-5,y+8,this.direction);
break;
case 3:
zd=new Zd(x+28,y+8,this.direction);
break;
}
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 {
} 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.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);
}

//绘制敌方坦克
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.xet.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() {

}
}

1个回答

//敌方坦克

//绘制敌方坦克