liaisong 2018-03-04 05:32 采纳率: 100%
浏览 1071
已采纳

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);
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.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条回答 默认 最新

  • hanzy88 2018-03-04 05:39
    关注

    这样的
    //敌方坦克
    这一行,直到
    //绘制敌方坦克
    这一行。
    敌方坦克的初始化,你将他们移动到paint()这个方法的上面去

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。