JAVA 간이 탱크 대전

64298 단어 자바 기반
package mytankgame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Vector;

public class MyTankGame implements ActionListener{

    JFrame jf=null;
    StartPanel sp=null;
    JMenuBar jmb=null;
    //  
    JMenu jm=null;
    //    
    JMenuItem ji1=null;
    JMenuItem ji2=null;
    JMenuItem ji3=null;
    //            
    MyTankGame()
    {
        jf=new JFrame("    ");
        sp=new StartPanel();
        jf.setBounds(300, 200, 800, 700);
        //   
        jmb=new JMenuBar();
        //  
        jm=new JMenu("  ");
        //        
        ji1=new JMenuItem("    ");
        ji1.setActionCommand("newgame");
        ji1.addActionListener(this);
        //    
        ji2=new JMenuItem("    ");
        ji2.setActionCommand("saveexit");
        ji2.addActionListener(this);

        jf.setJMenuBar(jmb);
        jmb.add(jm);
        jm.add(ji1);
        jm.add(ji2);
        jm.add(ji3);
        jf.add(sp);
        new Thread(sp).start();
        jf.setVisible(true);
    }
    //   
    public static void main(String[] args) {
                 MyTankGame mtg=new MyTankGame();
    }

    @Override
    //      
    public  void actionPerformed(ActionEvent e) {
        //            
        if(e.getActionCommand().equals("newgame"))
        {
            MyPanel mp=new MyPanel();
            jf.remove(sp);
            jf.add(mp);
            new Thread(mp).start();
            jf.addKeyListener(mp);
            jf.setVisible(true);
        }else if(e.getActionCommand().equals("saveexit"))//          
        {
            Recorder.saveExit();
            System.exit(0);
        }
    }


}
//          ,          
class StartPanel extends JPanel implements Runnable
{
    int times=0;
    public void paint(Graphics g)
    {
        super.paint(g);
        g.fillRect(0, 0, 500, 400);
        //         
        if(times%2==0)
        {
        g.setColor(Color.lightGray);
        Font myfont=new Font("    ", Font.BOLD, 100);
        g.setFont(myfont);
        g.drawString("STAGE :1", 30, 150);
        }
    }

    @Override
    public void run() {
        while(true)
        {
        try {
            Thread.sleep(500);
        } catch (Exception e) {
            // TODO: handle exception
        }
        times++;
        this.repaint(); 
        }
    }
}
//     
class MyPanel extends JPanel implements KeyListener,Runnable
{
    //         
    Image i1=null;
    Image i2=null;
    Image i3=null;
    Image i4=null;
    Image i5=null;
    Image i6=null;
    Image i7=null;
    Image i8=null;
    //        
    Vector vboom=new Vector();

    HeroTank hero=null;
    //         
    Vector v=new Vector();



    MyPanel()
    {
        //          
        Recorder.setV(v);
        //       
        hero=new HeroTank(10,10);
        //       
        for (int x=0;x<3;x++)
        {
            enemyTank enemy=new enemyTank((x+1)*100,0);
            //           ,       
            enemy.setEt(v);
            enemy.setDirect(2);
            new Thread(enemy).start();
            Bullet bullet=new Bullet(enemy.x+23,enemy.y+25,enemy.direct);
            enemy.enemyV.add(bullet);
            new Thread(bullet).start();
            v.add(enemy);   
        }
        i1=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/blast1.gif"));
        i2=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/blast2.gif"));
        i3=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/blast3.gif"));
        i4=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/blast4.gif"));
        i5=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/blast5.gif"));
        i6=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/blast6.gif"));
        i7=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/blast7.gif"));
        i8=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/blast8.gif"));
    }
    public void paint(Graphics g)
    {

        super.paint(g);
        g.fillRect(0, 0, 500, 400);
        //           
        showInfo(g);
        //      
        if(hero.isLive)
        {
        drawTank(hero.getX(),hero.getY(),g,1,hero.getDirect());
        }
        //         
        for (int x=0;xif(et.isLive==true)
            {
            drawTank(et.getX(),et.getY(),g,0,et.direct);
            for(int i=0;iif(b.isLive)
                {
                g.drawOval(b.x,b.y, 2, 2);
                }else
                {
                    et.enemyV.remove(b);
                }
            }
            }
        }

        //       
        for(int i=0;iif(mybullet!=null&&mybullet.isLive==true)
            {
                 g.drawOval(mybullet.x, mybullet.y, 2, 2);
            }
            if(mybullet.isLive==false)
            {
                hero.vb.remove(i);
            }
        }
        //       
        for(int j=0;j//System.out.println(b.x+":"+b.y);
            if(b.live>42)
            {
                g.drawImage(i1, b.x, b.y, 60, 60,this);

            }else if(b.live>36)
            {
                g.drawImage(i2, b.x, b.y, 60, 60, this);

            }else if(b.live>30)
            {
                g.drawImage(i3, b.x, b.y, 60, 60, this);

            }else if(b.live>24)
            {
                g.drawImage(i4,b.x, b.y, 60, 60, this);

            }else if(b.live>18)
            {
                g.drawImage(i5, b.x, b.y, 60, 60, this);

            }else if(b.live>12)
            {
                g.drawImage(i6, b.x, b.y, 60, 60, this);

            }else if(b.live>6)
            {
                g.drawImage(i7, b.x, b.y, 60, 60, this);
            }else 
            {
                g.drawImage(i8, b.x, b.y, 60, 60, this);
            }
            b.lifeDown();
            if(b.live==0)
            {
                vboom.remove(b);
            }
        }
    }
    //           
    public void showInfo(Graphics g)
    {
        this.drawTank(50,450,g,0,0);
        g.setColor(Color.BLACK);
        Font myfont=new Font("    ", Font.BOLD, 30);
        g.setFont(myfont);
        g.drawString(Recorder.getEnemynum()+"", 100, 480);
        this.drawTank(150,450,g,1,0);
        g.drawString(Recorder.getMenum()+"", 200, 480);
        g.setColor(Color.black);
        g.drawString("     ", 530, 50);
        this.drawTank(530, 90, g, 0, 0);
        g.setColor(Color.black);
        g.drawString(Recorder.getAllen()+"", 600, 120);

    }   
    //      
    public void drawTank(int x,int y,Graphics g,int type,int direct)
    {
        //                
        switch(type)
        {
        case 0:
            g.setColor(Color.CYAN);
            break;
        case 1:
            g.setColor(Color.ORANGE);
            break;
        }
        //             
        switch(direct)
        {
        case 0:
            g.fill3DRect(x, y, 10, 50,false);
            g.fill3DRect(x+5, y+5,25,40,false);
            g.fill3DRect(x+30, y, 10, 50,false);;
            g.fillOval(x+9, y+13, 20, 20);
            g.drawLine(x+20, y+18, x+20, y-15);
            break;
        case 1:
            g.fill3DRect(x, y, 50, 10, false);
            g.fill3DRect(x+5, y+5, 40, 25, false);
            g.fill3DRect(x, y+30, 50, 10, false);
            g.fillOval(x+13, y+9, 20, 20);
            g.drawLine(x+13, y+18, x+65, y+18);
            break;
        case 2:
            g.fill3DRect(x, y, 10, 50,false);
            g.fill3DRect(x+5, y+5,25,40,false);
            g.fill3DRect(x+30, y, 10, 50,false);;
            g.fillOval(x+9, y+13, 20, 20);
            g.drawLine(x+20, y+18, x+20, y+63);
            break;
        case 3:
            g.fill3DRect(x, y, 50, 10, false);
            g.fill3DRect(x+5, y+5, 40, 25, false);
            g.fill3DRect(x, y+30, 50, 10, false);
            g.fillOval(x+13, y+9, 20, 20);
            g.drawLine(x+20, y+18,x-15 , y+18);
            break;
        }
        this.repaint();

    }
    //      ,              
    public void hitTank(Tank et,Bullet b)
    {
        switch(et.direct)
        {
        case 0:
        case 2:
            if(b.x>et.x&&b.x45&&b.y>et.y&&b.y50)
            {

                et.isLive=false;
                b.isLive=false;
                if(et instanceof enemyTank)
                {
                    Recorder.enemydown();
                    Recorder.allUp();
                }else
                {
                    Recorder.medown();
                }
                Boom bm=new Boom(et.x,et.y);
                vboom.add(bm);

            }
            break;
        case 1:
        case 3:
            if(b.x>et.x&&b.x50&&b.y>et.y&&b.y45)
            {

                et.isLive=false;
                b.isLive=false;
                if(et instanceof enemyTank)
                {
                    Recorder.enemydown();
                    Recorder.allUp();
                }else
                {
                    Recorder.medown();
                }

                Boom bm=new Boom(et.x,et.y);
                vboom.add(bm);
            }
            break;
        }
    }
    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub

    }
    @Override
    //    
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode()==KeyEvent.VK_W)
        {
            //    
            this.hero.setDirect(0);
            this.hero.moveUp();
        }else if(e.getKeyCode()==KeyEvent.VK_D)
        {
            this.hero.setDirect(1);
            this.hero.moveRight();
        }else if(e.getKeyCode()==KeyEvent.VK_S)
        {
            this.hero.setDirect(2);
            this.hero.moveDown();
        }else if(e.getKeyCode()==KeyEvent.VK_A)
        {
            this.hero.setDirect(3);
            this.hero.moveLeft();
        }
        if(e.getKeyCode()==KeyEvent.VK_J)
        {
            if(hero.isLive)
            {
            if(hero.vb.size()<5)
            {
            hero.shot();
            }
            }
        }
        if(e.getKeyCode()==KeyEvent.VK_SPACE)
        {
            hero.speed=0;
            hero.bullet.speed=0;
            hero.setDirect(hero.direct);

            for(int i=0;i0;
                v.get(i).setDirect(v.get(i).direct);
                for(int j=0;j0;
                }
            }
        }

    }
    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub

    }
    @Override
    public void run() {
        while(true)
        {
        try{
            Thread.sleep(100);

        }catch (Exception e) {
            // TODO: handle exception
        }
        //             
        for (int i=0;iif(b.isLive)
            {
                for (int j=0;jif(et.isLive)
                    {
                        hitTank(et,b);
                    }
                }
            }
        }
        //              
        for(int i=0;ifor(int j=0;jif(hero.isLive)
                {
                hitTank(hero,b);
                }
            }
        }
        this.repaint();
    }
    }
}
//   
class Recorder
{
    //       
    private static int enemynum=20;
    //       
    private static int menum=3;
    //       
    private static int allen=0;

    private static BufferedReader br=null;
    private static BufferedWriter bw=null;

    private static Vector v=new Vector();

    public static Vector getV() {
        return v;
    }
    public static void setV(Vector v) {
        Recorder.v = v;
    }
    public static int getAllen() {
        return allen;
    }
    public static void setAllen(int allen) {
        Recorder.allen = allen;
    }
    public static int getEnemynum() {
        return enemynum;
    }
    public  void setEnemynum(int enemynum) {
        this.enemynum = enemynum;
    }
    public static int getMenum() {
        return menum;
    }
    public void setMenum(int menum) {
        this.menum = menum;
    }
    public static void enemydown()
    {
        enemynum--;
    }
    public static void medown()
    {
        menum--;
    }
    public static void allUp()
    {
        allen++;
    }
    public static void saveExit()
    {
        //                           
        try
        {
        bw=new BufferedWriter(new FileWriter("myRecorder.txt"));
        for(int i=0;iif(v.get(i).isLive)
            {
            bw.write(allen+""+"\r
"
); String recorder=v.get(i).x+" "+v.get(i).y+" "+v.get(i).direct+"\r
"
; bw.write(recorder); } } }catch (Exception e) { // TODO: handle exception }finally { try { bw.close(); }catch (Exception e) { // TODO: handle exception } } } } // class Boom { // int x; int y; // , , int live=48; boolean isLive=true; public Boom(int x,int y) { this.x=x; this.y=y; } public void lifeDown() { if(live>0) { live--; }else { isLive=false; } } } // class Tank { // int x=0; int y=0; // int direct=0; int speed=1; boolean isLive=true; public int getSpeed() { return speed; } public void setSpeed(int speed) { this.speed = speed; } public int getDirect() { return direct; } public void setDirect(int direct) { this.direct = direct; } 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; } Tank(int x,int y) { this.x=x; this.y=y; } } // class enemyTank extends Tank implements Runnable { int times=0; // , Vector enemyV=new Vector(); // panel , Vector vv=new Vector(); Bullet bullet=null; enemyTank(int x,int y) { super(x,y); } // public void setEt(Vector v) { this.vv=v; } // public boolean isTouch() { boolean flag=false; switch(this.direct) { case 0: for(int i=0;i// if(et!=this) { // if(et.direct==0||et.direct==2) { // if(this.x>=et.x&&this.x45&&this.y>=et.y&&this.y<=et.y+50) { return true; } if(this.x+45>=et.x&&this.x+45<=et.x+45&&this.y>=et.y&&this.y<=et.y+50) { return true; } } // if(et.direct==1||et.direct==3) { if(this.x>=et.x&&this.x<=et.x+50&&this.y>=et.y&&this.y<=et.y+45) { return true; } if(this.x+45>=et.x&&this.x+45<=et.x+50&&this.y>=et.y&&this.y<=et.y+45) { return true; } } } } break; case 1: for(int i=0;i// if(et!=this) { // if(et.direct==0||et.direct==2) { // if(this.x+50>=et.x&&this.x+5045&&this.y>=et.y&&this.y<=et.y+50) { return true; } if(this.x+50>=et.x&&this.x+50<=et.x+45&&this.y+45>=et.y&&this.y+45<=et.y+50) { return true; } } // if(et.direct==1||et.direct==3) { if(this.x+50>=et.x&&this.x+50<=et.x+50&&this.y>=et.y&&this.y<=et.y+45) { return true; } if(this.x+50>=et.x&&this.x+50<=et.x+50&&this.y+45>=et.y&&this.y+45<=et.y+45) { return true; } } } } break; case 2: for(int i=0;i// if(et!=this) { // if(et.direct==0||et.direct==2) { // if(this.x>=et.x&&this.x45&&this.y+50>=et.y&&this.y+50<=et.y+50) { return true; } if(this.x+45>=et.x&&this.x+45<=et.x+45&&this.y+50>=et.y&&this.y+50<=et.y+50) { return true; } } // if(et.direct==1||et.direct==3) { if(this.x>=et.x&&this.x<=et.x+50&&this.y+50>=et.y&&this.y+50<=et.y+45) { return true; } if(this.x+45>=et.x&&this.x+45<=et.x+50&&this.y+50>=et.y&&this.y+50<=et.y+45) { return true; } } } } break; case 3: for(int i=0;i// if(et!=this) { // if(et.direct==0||et.direct==2) { // if(this.x>=et.x&&this.x45&&this.y>=et.y&&this.y<=et.y+50) { return true; } if(this.x>=et.x&&this.x<=et.x+45&&this.y+45>=et.y&&this.y+45<=et.y+50) { return true; } } // if(et.direct==1||et.direct==3) { if(this.x>=et.x&&this.x<=et.x+50&&this.y>=et.y&&this.y<=et.y+45) { return true; } if(this.x>=et.x&&this.x<=et.x+50&&this.y+45>=et.y&&this.y+45<=et.y+45) { return true; } } } } break; } return flag; } @Override public void run() { while(true) { // , , switch(direct) { case 0: for(int i=0;i<30;i++) { if(y>0&&!isTouch()) { y-=speed; try { Thread.sleep(50); } catch (Exception e) { } } } break; case 1: for(int i=0;i<30;i++) { if(x<500&&!isTouch()) { x+=speed; try { Thread.sleep(50); } catch (Exception e) { } } } break; case 2: for(int i=0;i<30;i++) { if(y<400&&!isTouch()) { y+=speed; try { Thread.sleep(50); } catch (Exception e) { } } } break; case 3: for(int i=0;i<30;i++) { if(x>0&&!isTouch()) { x-=speed; try { Thread.sleep(50); } catch (Exception e) { } } } break; } times++; if(times%2==0) { if(isLive) { Bullet bullet=null; if(enemyV.size()<5) { switch(direct) { case 0: bullet=new Bullet(x+20,y-15,0); enemyV.add(bullet); break; case 1: bullet=new Bullet(x+65,y+18,1); enemyV.add(bullet); break; case 2: bullet=new Bullet(x+20,y+63,2); enemyV.add(bullet); break; case 3: bullet=new Bullet(x-15,y+18,3); enemyV.add(bullet); break; } new Thread(bullet).start(); } } } // direct=(int)(Math.random()*4); if(isLive==false) { break; } } } } // class HeroTank extends Tank { int speed=1; Bullet bullet=null; Vector vb=new Vector(); HeroTank(int x,int y) { super(x,y); } public void shot() { switch(direct) { case 0: bullet=new Bullet(x+20,y-15,0); vb.add(bullet); break; case 1: bullet=new Bullet(x+65,y+18,1); vb.add(bullet); break; case 2: bullet=new Bullet(x+20,y+63,2); vb.add(bullet); break; case 3: bullet=new Bullet(x-15,y+18,3); vb.add(bullet); break; } new Thread(bullet).start(); } public void moveUp() { y-=speed; } public void moveRight() { x+=speed; } public void moveDown() { y+=speed; } public void moveLeft() { x-=speed; } } // class Bullet implements Runnable { int x; int y; int direct; int speed=1; boolean isLive=true; Bullet(int x,int y,int direct) { this.x=x; this.y=y; this.direct=direct; } @Override public void run() { while(true) { // 50ms try{ Thread.sleep(50); }catch(InterruptedException e) { } // , switch(direct) { case 0: y-=speed; break; case 1: x+=speed; break; case 2: y+=speed; break; case 3: x-=speed; break; } // panel if(x<0||x>500||y<0||y>400) { isLive=false; break; } } } }

튜 토리 얼 을 따라 간이 판 탱크 대전 을 벌 여 자신 이 배 워 야 할 것 이 많다 는 것 을 알 게 되 었 다.매우 어렵 고 많은 것 도 아직 실현 되 지 않 았 다.앞으로 의 길 은 아직 멀 었 다.그대 와 함께 격려 하 다.

좋은 웹페이지 즐겨찾기