继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

java建议坦克大战代码(1)

慕祈
关注TA
已关注
手记 18
粉丝 57
获赞 293

package t1;

public class BaoZha {
int x, y;
int shengcunqi = 9;
boolean shengming = true;

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

public void scqjs() {

    if (shengcunqi > 0) {
        shengcunqi--;

    } else {
        this.shengming = false;
    }

}

}

package t1;

import java.util.Vector;

import t1.DiTank;

class DiTank extends Tank implements Runnable
{
int time=0;
Vector<ZiDan> dzd=new Vector<ZiDan>();
Vector<DiTank> dtk;

public Vector<DiTank> getDtk() {
    return dtk;
}

public void setDtk(Vector<DiTank> dtk) {
    this.dtk = dtk;
}

public DiTank(int x, int y) {
    super(x, y);
}

public boolean hxpz() {
    boolean b=false;
    switch (fangxiang){

    case 0:

        for(int i=0;i<dtk.size();i++)
        {
            DiTank dt=dtk.get(i);

            if(dt!=this)
            {
                if(dt.fangxiang==0dt.fangxiang==2)
                {
                    if(this.x>=dt.x && this.x<=dt.x+20 && this.y>=dt.y && this.y<=dt.y+30)
                    {
                        return true;
                    }
                    if(this.x+20>=dt.x && this.x+20<=dt.x+20 && this.y>=dt.y && this.y<=dt.y+30)
                    {
                        return true;
                    }
                }
                if(dt.fangxiang==3dt.fangxiang==1)
                {
                    if(this.x>=dt.x && this.x<=dt.x+30 && this.y>=dt.y && this.y<=dt.y+20)
                    {
                        return true;
                    }
                    if(this.x+20>=dt.x && this.x+20<=dt.x+30 && this.y>=dt.y && this.y<=dt.y+20)
                    {
                        return true;
                    }
                }
            }
        }
        break;
    case 1:
        for(int i=0;i<dtk.size();i++)
        {
            DiTank dt=dtk.get(i);
            if(dt!=this)
            {
                if(dt.fangxiang==0dt.fangxiang==2)
                {
                    if(this.x>=dt.x&&this.x<=dt.x+20&&this.y>=dt.y&&this.y<=dt.y+30)
                    {
                        return true;
                    }
                    //下一点
                    if(this.x>=dt.x&&this.x<=dt.x+20&&this.y+20>=dt.y&&this.y+20<=dt.y+30)
                    {
                        return true;
                    }
                }
                if(dt.fangxiang==3dt.fangxiang==1)
                {
                    if(this.x>=dt.x&&this.x<=dt.x+30&&this.y>=dt.y&&this.y<=dt.y+20)
                    {
                        return true;
                    }
                    if(this.x>=dt.x&&this.x<=dt.x+30&&this.y+20>=dt.y&&this.y+20<=dt.y+20)
                    {
                        return true;
                    }
                }
            }
        }

        break;
    case 2:
        for(int i=0;i<dtk.size();i++)
        {
            DiTank dt=dtk.get(i);
            if(dt!=this)
            {
                if(dt.fangxiang==0dt.fangxiang==2)
                {
                    if(this.x>=dt.x&&this.x<=dt.x+20&&this.y+30>=dt.y&&this.y+30<=dt.y+30)
                    {
                        return true;
                    }
                    if(this.x+20>=dt.x&&this.x+20<=dt.x+20&&this.y+30>=dt.y&&this.y+30<=dt.y+30)
                    {
                        return true;
                    }
                }
                if(dt.fangxiang==3dt.fangxiang==1)
                {
                    if(this.x>=dt.x&&this.x<=dt.x+30&&this.y+30>=dt.y&&this.y+30<=dt.y+20)
                    {
                        return true;
                    }

                    if(this.x+20>=dt.x&&this.x+20<=dt.x+30&&this.y+30>=dt.y&&this.y+30<=dt.y+20)
                    {
                        return true;
                    }
                }
            }
        }
        break;
    case 3:
        for(int i=0;i<dtk.size();i++)
        {
            DiTank dt=dtk.get(i);
            if(dt!=this)
            {
                if(dt.fangxiang==0dt.fangxiang==2)
                {
                    if(this.x+30>=dt.x && this.x+30<=dt.x+20 && this.y>=dt.y && this.y<=dt.y+30)
                    {
                        return true;
                    }
                    //下点 
                    if(this.x+30>=dt.x && this.x+30<=dt.x+20 && this.y+20>=dt.y && this.y+20<=dt.y+30)
                    {
                        return true;
                    }
                }
                if(dt.fangxiang==3dt.fangxiang==1)
                {
                    if(this.x+30>=dt.x && this.x+30<=dt.x+30 && this.y>=dt.y && this.y<=dt.y+20)
                    {
                        return true;
                    }
                    if(this.x+30>=dt.x && this.x+30<=dt.x+30 && this.y+20>=dt.y && this.y+20<=dt.y+20)
                    {
                        return true;
                    }
                }
            }
        }

        break;
    }       
    return b;//以上所有return都在循环中,不一起认为不一定会有return被执行。
}

public void run() {
    // TODO Auto-generated method stub
    while(true)
    {
        switch (fangxiang) {
        case 0:
            for(int i=0;i<30;i++)
            {
                if(y>0&&!hxpz())
            {y-=sudu;}
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {}
            }
            break;
        case 1:
            for(int i=0;i<30;i++)
            {
                if(x>0&&!hxpz())
                {x-=sudu;}
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {}
            }

            break;
        case 2:
            for(int i=0;i<30;i++)
            {
                if(y<500&&!hxpz())
                {y+=sudu;}
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {}
            }

            break;
        case 3:
            for(int i=0;i<30;i++)
            {
                if(x<700&&!hxpz())
                {x+=sudu;}
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {}
            }

            break;

        default:
            break;
        }

        this.fangxiang=(int)(Math.random()*4);//随机敌方坦克的方向

        if(this.shengming==false)
        {
            break;
        }

        this.time+=2;//子弹时间
        //System.out.println("time"+time);

        if(time%2==0)
        {
        //System.out.println("1");
            if(this.shengming)
        {
            if(dzd.size()<6)
            {
                ZiDan zd=null;
                System.out.println("2");

                switch (this.fangxiang) {

                case 0:zd=new ZiDan(x, y, 0);break;
                case 1:zd=new ZiDan(x, y+10, 1);break;
                case 2:zd=new ZiDan(x, y+30, 2);break;
                case 3:zd=new ZiDan(x+30, y+10, 3);break;
                }
                dzd.add(zd);
                Thread zdfs=new Thread(zd);
                zdfs.start();

            }
        }

        }
    }
}

}

package t1;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;

import javax.swing.JPanel;

public class GuanKaPanel extends JPanel implements Runnable {

int time=0;

@Override
public void paint(Graphics g) {
    super.paint(g);
    g.fillRect(0, 0, 800, 600);
    if(time%2==0)
    {

        g.setColor(Color.pink);
        Font myFont=new Font("华文行楷",Font.BOLD,48);
        g.setFont(myFont);
        g.drawString("第一关", 320, 290);

    }

}

public void run() {
    // TODO Auto-generated method stub
    while(true)
        {
    try {
        Thread.sleep(500);
    } catch (Exception e) {}
    time++;
    this.repaint();
}
}

}

package t1;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Vector;

class weizhi
{
int x;
int y;
int fangxiang;

public weizhi(int x, int y, int fangxiang) {
    super();
    this.x = x;
    this.y = y;
    this.fangxiang = fangxiang;
}

}
public class JiLu {

private  static int dtsl=18;
private  static int mtsl=1;
private  static int sdtj=0;
private static FileWriter fw=null;
private static BufferedWriter bw=null;
private static FileReader fr=null;
private static BufferedReader br=null;
/*private static Vector<DiTank> dtk=new Vector<DiTank>();
static Vector<weizhi> wzjh=new Vector<weizhi>();*/

/*public static Vector<DiTank> getDtk() {
    return dtk;
}
public static void setDtk(Vector<DiTank> dtk) {
    JiLu.dtk = dtk;
}*/
public static int getSdtj() {
    return sdtj;
}
public static void setSdtj(int sdtj) {
    JiLu.sdtj = sdtj;
}
public static int getDtsl() {
    return dtsl;
}
public static void setDtsl(int dtsl) {
    JiLu.dtsl = dtsl;
}
public static int getMtsl() {
    return mtsl;
}
public static void setMtsl(int mtsl) {
    JiLu.mtsl = mtsl;
}

public static void dtjs()
{
    dtsl--;
}

public static void mtjs()
{
    mtsl--;
}

public static void sdtj()
{
    sdtj++;
}

public static void bcjl()
{
    try {
        fw=new FileWriter("D:/baocun.txt");
        bw=new BufferedWriter(fw);
        bw.write(sdtj+"\r\n");
    } catch (IOException e) {}
    finally 
    {
        try {
            bw.close();
            fw.close();
        } catch (IOException e) {}
    }       
}

/*public void cunpan()
{
    try {
        fw=new FileWriter("e:/baocun.txt");
        bw=new BufferedWriter(fw);
        bw.write(sdtj+"\r\n");
        for(int i=0;i<dtk.size();i++)
        {
            DiTank dt=dtk.get(i);
            if(dt.shengming)
            {
                String zb=dt.x+" "+dt.y+" "+dt.fangxiang+"";
                bw.write(zb+"\r\n");
            }
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}*/

public static void dqjl() {

    try {
        fr=new FileReader("D:/baocun.txt");
        br=new BufferedReader(fr);
        String s=br.readLine();
        sdtj=Integer.parseInt(s);
    } catch (NumberFormatException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}

package t1;

import java.util.Vector;

class MyTank extends Tank
{//子弹跟着坦克走,所以子弹功能大多与坦克有关
ZiDan zd=null;
Vector<ZiDan> aa=new Vector<ZiDan>();//存子弹

    public void fszd()
    {
        switch (this.fangxiang) {
        case 0:
            zd=new ZiDan(x+9, y-2,0);
            aa.add(zd);
            break;
        case 1:
            zd=new ZiDan(x-5, y+10,1);
            aa.add(zd);
            break;
        case 2:
            zd=new ZiDan(x+9, y+30,2);
            aa.add(zd);
            break;
        case 3:
            zd=new ZiDan(x+30, y+10,3);
            aa.add(zd);
            break;

        }
        Thread t=new Thread(zd);
        t.start();//子弹移动线程
    }

public MyTank(int x, int y) {

    super(x, y);

    }

    public void xiangshang()
    {
        y-=sudu;

    }

    public void xiangzuo()
    {
        x-=sudu;

    }

    public void xiangxia()
    {
        y+=sudu;
    }

    public void xiangyou()
    {
        x+=sudu;
    }

}

package t1;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.IOException;
import java.util.Vector;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.*;
import javax.swing.event.AncestorListener;

class Shengyin extends Thread {
private String wjm;

public Shengyin(String ypwj) {
    wjm = ypwj;
}

public void run() {
    File wjl = new File(wjm);

    AudioInputStream ypsrl = null;
    try {
        ypsrl = AudioSystem.getAudioInputStream(wjl);
    } catch (UnsupportedAudioFileException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    AudioFormat format = ypsrl.getFormat();// 得到音频码
    SourceDataLine apsj = null;
    DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
    // 解析音频码

    try {
        apsj = (SourceDataLine) AudioSystem.getLine(info);
        apsj.open(format);
    } catch (Exception e) {
    }
    apsj.start();

    int zjtj = 0;
    byte[] hczj = new byte[2048];

    try {
        while (zjtj != -1) {
            zjtj = ypsrl.read(hczj, 0, hczj.length);
            if (zjtj >= 0)
                apsj.write(hczj, 0, zjtj);
        }
    } catch (Exception e) {
    } finally {
        apsj.drain();// 将残留部分处理干净
        apsj.close();
    }

}

}

打开App,阅读手记
9人推荐
发表评论
随时随地看视频慕课网APP