zoukankan      html  css  js  c++  java
  • Java坦克大战(二)

    本文紧接上一篇讲解坦克大战这个项目,因为当初在学习的时候,是以这个案例逐步学习Java基础的,过程是先讲知识点,再将知识点逐步的融入到项目中,即给坦克添加新的功能。之前的Demo练习,想都记录下来。这里不会具体讲解知识点的概念,下面开始啦~

    Demo1:Flie的基本用法

    package com.fanghua1;
    
    import java.io.*;
    
    public class Demo1_9 {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            // 创建一个文件对象(d:\aa.txt是存在的)
            File f = new File("d:\aa.txt");
            // 得到文件路径
            System.out.println("文件的路径:" + f.getAbsolutePath());
            // 得到文件的大小(字节大小)
            System.out.println("文件的字节:" + f.length());
            System.out.println("可读" + f.canRead());
    
            // 创建文件和创建文件夹
            File f2 = new File("d:\222.txt");
            // 先判断文件是否存在
            if (!f2.exists()) {
                // 可以创建
                try {
                    f2.createNewFile();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } else {
                System.out.println("有文件,不能创建");
            }
            File f3 = new File("d:\ff");
            if (f3.isDirectory()) {
                System.out.println("文件夹存在");
            } else {
                // 创建文件夹
                f3.mkdir();
            }
    
            // 列出文件夹下面的所有文件
            File f4 = new File("d:\MyJavaDemo");
            if (f4.isDirectory()) {
                File lists[] = f4.listFiles();
                for (int i = 0; i < lists.length; i++) {
                    System.out.println("文件名:" + lists[i].getName());
                }
            }
    
        }
    
    }

    Demo2:演示FileInputStream

    package com.fanghua1;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    public class Demo1_11 {
        public static void main(String[] args) {
            File f = new File("d:\aa.txt");
            FileInputStream fis = null;
            byte bytes[] = new byte[1024];
            int n = 0;
            try {
                fis = new FileInputStream(f);
                try {
                    while ((n = fis.read(bytes)) != -1) {
                        String s = new String(bytes, 0, n);
                        System.out.println(s);
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    Demo3:演示FileOutputStream

    package com.fanghua1;
    
    import java.io.*;
    public class Demo2_1 {
        public static void main(String[] args) {
            // 这里:File f=new File("d:\ss.text");
            // 如果d:\ss.text不存在,它会直接创建这个文件
            // 如果存在,会进行覆盖。所以一定一定要进行判断
            File f = new File("d:\ss.text");
            // 字节输出流
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(f);
                // 
    :表示换行
                String s = "我的第一个字节输出流文件,文本内容
    ";
                String s1 = "我是上一句的下一行";
    
                fos.write(s.getBytes());
                fos.write(s1.getBytes());
    
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    fos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    Demo4:演示图片的拷贝

    package com.fanghua1;
    
    import java.io.*;
    
    public class Demo2_2 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            // 思路:先把图片读入到内存--->写入到某个文件
            // 因为是二进制文件,因策只能用字节流完成
            //可以不用File f=new File("c:\a.jpg");下面直接把c:\a.jpg给FileInoutStream即可
            FileInputStream fis = null;
            FileOutputStream fos = null;
            try {
                fis = new FileInputStream("c:\a.jpg");
                fos = new FileOutputStream("d:\a.jpg");// 这里a.jpg,你写什么就命名为什么
    
                byte buf[] = new byte[512]; // 512 是1024的一半
                int n = 0;// 记录实际读取到的字节数
                // 循环读取
                while ((n= fis.read(buf)) != -1) {
                    //输出到指定文件,验证:没有这句话在d盘会创建0字节的a.jpg文件
                    fos.write(buf);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    fis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                try {
                    fos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    Demo5:演示BufferedReader

    package com.fanghua1;
    
    import java.io.*;
    
    public class Demo2_3 {
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            // 不是这里:FileReader f=new FileReader("d:\MyJavaDemo\Demo6.java");
            BufferedReader br = null;
            BufferedWriter bw = null;
            try {
                FileReader f = new FileReader("d:\MyJavaDemo\Demo6.java");
                br = new BufferedReader(f);
                // 创建FileWriter对象
                FileWriter fw = new FileWriter("e:\赵云.txt");
                bw = new BufferedWriter(fw);
                // 循环读取
                String s = "";
                while ((s = br.readLine()) != null) {
                    // 输出到磁盘(+"
    "换行)
                    bw.write(s + "
    ");
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    br.close();
                    bw.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    坦克大战(1.2版本)(#^.^#)

    1.Menbers类

    package com.fanghua2;
    /**
     * 从Menbers.java里分离出来,代表坦克的功能类(同包访问机制)
     * 目的:减少Menbers.java文件的长度,整洁、规范
     */
    
    //坦克类
    class Tank1_2 {
        
        int x = 0;
        int y = 0;
    
        // 坦克方向:0表示上,1表示右,2表示下,3表示左
        int direct = 0;
        int speed = 1;
        //坦克的颜色
        int color;
    
        public int getColor() {
            return color;
        }
    
        public void setColor(int color) {
            this.color = color;
        }
    
        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;
        }
    
        // 构造函数
        public Tank1_2(int x, int y) {
            this.x = x;
            this.y = y;
    
        }
    
    }
    //敌人的坦克
    class EnemyTank extends Tank1_2{
    
        public EnemyTank(int x, int y) {
            super(x, y);
            // TODO Auto-generated constructor stub
        }
    }
    
    //我的坦克
    class Hero1_2 extends Tank1_2 {
        public Hero1_2(int x, int y) {
            super(x, y);
        }
    
        public void moveUp() {
            y -= speed;
        }
        public void moveRight() {
            x += speed;
        }
        public void moveDown() {
            y += speed;
        }
        public void moveLeft() {
            x -= speed;
        }
    }

    2.MyTankGame1_3类

    /*
     * 删掉很多之前的注释
     * 功能:画出坦克1.2版本
     * 坦克移动的时候,头尾相应进行更正
     * 学习:文件(类)剥离、线程
     */
    package com.fanghua2;
    
    import java.awt.*;
    import java.awt.event.KeyEvent;
    import java.util.Vector;
    
    import javax.swing.*;
    
    public class MyTankGame1_3 extends JFrame {
    
        Mypanel1_2 mp = null;
    
        public static void main(String[] args) {
            new MyTankGame1_3();
        }
    
        // 构造函数
        public MyTankGame1_3() {
            mp = new Mypanel1_2();
            this.add(mp);
            // 注册监听
            this.addKeyListener(mp);
    
            this.setSize(600, 500);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setVisible(true);
    
        }
    }
    
    // 我的面板
    class Mypanel1_2 extends JPanel implements java.awt.event.KeyListener {
    
        // 定义我的坦克
        Hero1_2 hero = null;
        // 定义敌人的坦克(不止一辆,线程安全,集合)
        Vector<EnemyTank> ets = new Vector<EnemyTank>();
        int enSize = 3;// 敌人坦克保持三个
    
        // 构造函数
        public Mypanel1_2() {
            hero = new Hero1_2(10, 10);
            for (int i = 0; i < enSize; i++) {
                
                // 创建一辆敌人的坦克
                EnemyTank et = new EnemyTank((i + 1) * 50, 0);
                et.setColor(0);
                //坦克默认反向是0(向上),这里改一下
                et.setDirect(2);
                // 加入
                ets.add(et);
    
            }
        }
    
        // 重写paint函数
        public void paint(Graphics g) {
            // 一定要调用
            super.paint(g);
            g.fillRect(0, 0, 600, 500);
            // 画出自己的坦克(将方向填进去)
            this.drawTank(hero.getX(), hero.getY(), g, this.hero.direct, 1);
            // 画出敌人的坦克(这里不用 i<enSize,而用ets.size,敌人坦克会被打死)
            for (int i = 0; i < enSize; i++) {
                this.drawTank(ets.get(i).getX(), ets.get(i).getY(), g, ets.get(i)
                        .getDirect(), 0);
    
            }
    
        }
    
        // 画出坦克的函数
        public void drawTank(int x, int y, Graphics g, int direct, int type) {
            // 坦克类型
            switch (type) {
            case 0:
                g.setColor(Color.green);
                break;
            case 1:
                g.setColor(Color.yellow);
                break;
            }
            // 方向设置
            switch (direct) {
            // 向上
            case 0:
                g.fill3DRect(x, y, 5, 30, false);
                g.fill3DRect(x + 15, y, 5, 30, false);
                g.fill3DRect(x + 5, y + 5, 10, 20, false);
                g.fillOval(x + 5, y + 10, 10, 10);
                g.drawLine(x + 10, y + 15, x + 10, y);
                break;
            // 向右
            case 1:
                g.fill3DRect(x, y, 30, 5, false);
                g.fill3DRect(x, y + 15, 30, 5, false);
                g.fill3DRect(x + 5, y + 5, 20, 10, false);
                g.fillOval(x + 10, y + 5, 10, 10);
                g.drawLine(x + 15, y + 10, x + 30, y + 10);
                break;
            // 向下
            case 2:
                g.fill3DRect(x, y, 5, 30, false);
                g.fill3DRect(x + 15, y, 5, 30, false);
                g.fill3DRect(x + 5, y + 5, 10, 20, false);
                g.fillOval(x + 5, y + 10, 10, 10);
                g.drawLine(x + 10, y + 15, x + 10, y + 30);
                break;
            // 向左
            case 3:
                g.fill3DRect(x, y, 30, 5, false);
                g.fill3DRect(x, y + 15, 30, 5, false);
                g.fill3DRect(x + 5, y + 5, 20, 10, false);
                g.fillOval(x + 10, y + 5, 10, 10);
                g.drawLine(x + 15, y + 10, x, y + 10);
                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_UP 
                    || e.getKeyCode() == KeyEvent.VK_W) {
                this.hero.moveUp();
                this.hero.setDirect(0);
            } else if (e.getKeyCode() == KeyEvent.VK_RIGHT
                    || e.getKeyCode() == KeyEvent.VK_D) {
                this.hero.setDirect(1);
                this.hero.moveRight();
            } else if (e.getKeyCode() == KeyEvent.VK_DOWN
                    || e.getKeyCode() == KeyEvent.VK_S) {
                this.hero.moveDown();
                this.hero.setDirect(2);
            } else if (e.getKeyCode() == KeyEvent.VK_LEFT
                    || e.getKeyCode() == KeyEvent.VK_A) {
                this.hero.moveLeft();
                this.hero.setDirect(3);
            }
            this.repaint();
        }
    
        @Override
        public void keyReleased(KeyEvent e) {
            // TODO Auto-generated method stub
    
        }
    }

    此时,我的坦克已经可以用键盘来操控:

  • 相关阅读:
    Android使用Fragment来实现TabHost的功能
    selendroid之toast处理
    selendroid之inspector
    杂技
    自动化测试框架
    保险数据分析
    订单分析指标
    CRM原型
    monkey 参数
    monkey基础操作
  • 原文地址:https://www.cnblogs.com/1693977889zz/p/8440871.html
Copyright © 2011-2022 走看看