zoukankan      html  css  js  c++  java
  • java SE 学习第一个项目-贪吃蛇

      经过一个学期以及寒假的学习,渐渐的对java这门语言有了一定的了解。java好在其面向对象的思想,以及封装、多态等一些与人解决问题相符合的思想,所以其才能一直排名靠前并且留存至今。在学习过程中,脑袋中慢慢的对javaSE形成了一个体系,以下是根据视频中我认为比较重要的学习一些模块以及我自己的学习顺序:

      面向对象-异常处理-容器-IO处理-多线程-网络编程-GUI;

      但这只是我做完贪吃蛇这个小游戏之后所获得的一些感悟所总结出来的,也许经过之后的学习以及项目经验的加深我会有另外的想法。

      跟着视频完成贪吃蛇之后,我发现我学习Java的历程是:

      一:懵懂无知,以为和学习C语言一样,只是完成一些小的算法题目,简单的排序以及简单的语法知识。这个过程是在学习到面向对象的时候,当时还在学校,因为课程比较多,无法拿出较多的时间来系统的学习Java,之后也只是为了应付考试将Java的视频看到了IO处理,却是跟本没有装进我的脑袋。

      二:渐入佳境:进入了寒假又重新将之前的视频看了一下,视频中也是讲解知识点,以及根据每个知识点编出的一些小的练习题,在这个过程中慢慢对面向对象的思想有了一点思路,而且马士兵老师不断强调多态是什么,我好像能够稍微理解了一些。带着快要结束的激动心情结束了GUI这章的讲解。

      三:可以将知识点串联起来了:最后这一个过程就是贪吃蛇项目过程中得到的一些感悟,在JavaSE这一部分由点到线再到面的过程快要完成了,再通过完成几个其他的项目,就能够掌握的更加牢固了。

      贪吃蛇项目中用到的知识点,面向对象(这个方面是融入在多态中的)、异常处理(这算是一个比较小的知识点,但却不能忽略)、多线程、GUI(用的是awt比较老的,但是只有用GUI才能做出好看的界面“虽然我做的比较丑”,并且使得自己有成就感)。

    import javafx.scene.layout.CornerRadii;
    
    import java.awt.*;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    
    public class Yard extends Frame {
        PaintThread paintThread = new PaintThread();
        private boolean gameOver = false;
        public static final int ROWS = 50;
        public static final int COLS = 50;
        public static final int BLOCK_SIZE = 15;
        private int score = 0;
        Snake s = new Snake(this);
        Egg e = new Egg();
    
        public void launch() {
    
            this.setLocation(0, 0);
            this.setSize(COLS * BLOCK_SIZE, ROWS * BLOCK_SIZE);
            this.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
            this.addKeyListener(new KeyMonitor());
            this.setVisible(true);
    
           new Thread(paintThread).start();
        }
    
        public static void main(String[] args) {
            new Yard().launch();
        }
    
    
        public void stop(){
            gameOver = true;
        }
    
        public int getScore() {
            return score;
        }
    
        public void setScore(int score) {
            this.score = score;
        }
    
        public void paint(Graphics g) {
            Color c = g.getColor();
            g.setColor(Color.gray);
            g.fillRect(0, 0, COLS * BLOCK_SIZE, ROWS * BLOCK_SIZE);
            g.setColor(Color.darkGray);
            for (int i = 0; i < ROWS - 1; i++) {
                g.drawLine(0, BLOCK_SIZE * i, COLS * BLOCK_SIZE, BLOCK_SIZE * i);
            }
            for (int i = 0; i < COLS - 1; i++) {
                g.drawLine(BLOCK_SIZE * i, 0, BLOCK_SIZE * i, ROWS * BLOCK_SIZE);
            }
    
            g.setColor(Color.YELLOW);
            g.drawString("score:"+score,10,60);
    
            g.setColor(c);
            s.eat(e);
            s.draw(g);
            e.draw(g);
            g.setColor(Color.cyan);
         if(gameOver){
                g.setFont(new Font("华文彩云",Font.BOLD,50));
                g.drawString("游戏结束",120,180);
                paintThread.gameOver();
          }
        g.setColor(c);
        }
    
        @Override
        public void update(Graphics g) {
            Image offScreenImage = null;
            if (offScreenImage == null) {
                offScreenImage = this.createImage(BLOCK_SIZE * COLS, BLOCK_SIZE * ROWS);
            }
            Graphics gOff = offScreenImage.getGraphics();
            paint(gOff);
            g.drawImage(offScreenImage, 0, 0, null);
        }
    
        private class PaintThread implements Runnable {
            private boolean running = true;
            public void run() {
                while (running) {
                    repaint();
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
    
            public void gameOver(){
                running = false;
            }
    
        }
        private  class KeyMonitor extends KeyAdapter {
            @Override
            public void keyPressed(KeyEvent e) {
                s.keyPressed(e);
            }
        }
    }
    import java.awt.*;
    import java.awt.event.KeyEvent;
    
    public class Snake {
        private final Yard y;
        private Node head = null;
        private Node tail = null;
        private int size = 0;
    
    
        private Node n = new Node(20,30,Dir.L);
    
         Snake(Yard y){
            head = n;
            tail = n;
            size = 1;
            this.y = y;
        }
    
        public  void addToHead(){
            Node node = null;
            switch (head.dir){
                case L:
                    node = new Node(head.row,head.col - 1,head.dir);
                    break;
                case U:
                    node = new Node(head.row - 1,head.col,head.dir);
                    break;
                case R:
                    node = new Node(head.row,head.col + 1,head.dir);
                    break;
                case D:
                    node = new Node(head.row + 1,head.col ,head.dir);
            }
            node.next = head;
            head.prev = node;
            head = node;
            size++;
        }
    
        public void draw(Graphics g){
            if(size <= 0) return;
            move();
            for(Node n = head;n != null;n = n.next){
                n.draw(g);
            }
    
        }
    
        private void move() {
            addToHead();
            deleteFromTail();
            checkDead();
        }
    
        private void checkDead() {
             if(head.row<2||head.row>Yard.ROWS||head.col<0||head.col>Yard.COLS){
                y.stop();
             }
             for(Node n = head.next;n !=null; n = n.next){
               if(head.row == n.row && head.col == n.col){
                   y.stop();
               }
             }
        }
    
        private void deleteFromTail() {
            if(size == 0) return;
            tail = tail.prev;
            tail.next = null;
        }
    
        private class Node{
            int w = Yard.BLOCK_SIZE;
            int h = Yard.BLOCK_SIZE;
            int row , col;
            Dir dir ;
            Node next = null;
            Node prev = null;
    
    
             Node(int row, int col,Dir dir) {
                this.row = row;
                this.col = col;
                this.dir = dir;
            }
    
            void draw(Graphics g){
                 Color c = g.getColor();
                 g.setColor(Color.BLACK );
                 g.fillRect(Yard.BLOCK_SIZE*col,Yard.BLOCK_SIZE*row,w,h);
                 g.setColor(c);
            }
        }
    
        public  void eat(Egg e){
            if(this.getRect().intersects(e.getRect())){
                e.reAppear();
                this.addToHead();
                y.setScore(y.getScore()+5);
            }
        }
    
        private Rectangle getRect(){
             return new Rectangle(Yard.BLOCK_SIZE*head.col,Yard.BLOCK_SIZE*head.row,head.w,head.h);
        }
    
        public void keyPressed(KeyEvent e){
        int key = e.getKeyCode();
        switch(key){
            case KeyEvent.VK_A:
                if(head.dir != Dir.R)
                head.dir = Dir.L;
            break;
            case KeyEvent.VK_W:
                if(head.dir != Dir.D)
                    head.dir = Dir.U;
            break;
            case KeyEvent.VK_D:
                if(head.dir != Dir.L)
                    head.dir = Dir.R;
            break;
            case KeyEvent.VK_S:
                if(head.dir != Dir.U)
                head.dir = Dir.D;
            break;
    }
        }
    }
    1 public enum Dir {
    2     L, U, R, D
    3 }
     1 import java.awt.*;
     2 import java.util.Random;
     3 
     4 public class Egg {
     5     int row, col;
     6     int w = Yard.BLOCK_SIZE;
     7 
     8     int h = Yard.BLOCK_SIZE;
     9     private static  Random r = new Random();
    10     private Color color = Color.GREEN;
    11     public Egg(int row, int col) {
    12         this.row = row;
    13         this.col = col;
    14     }
    15 
    16     public void reAppear(){
    17         this.row = r.nextInt(Yard.ROWS-2)+2;
    18         this.col = r.nextInt(Yard.COLS);
    19     }
    20     public Egg(){
    21     this(r.nextInt(Yard.ROWS-2)+2,r.nextInt(Yard.COLS));
    22     }
    23 
    24     public Rectangle getRect(){
    25         return new Rectangle(Yard.BLOCK_SIZE*this.col,Yard.BLOCK_SIZE*this.row,this.w,this.h);
    26     }
    27     public void draw(Graphics g){
    28         Color c = g.getColor();
    29         g.setColor(color);
    30         g.fillOval(Yard.BLOCK_SIZE*col,Yard.BLOCK_SIZE*row,w,h);
    31         g.setColor(c);
    32         if(color == Color.GREEN){
    33             color = Color.RED;
    34         }
    35         else color = Color.GREEN;
    36     }
    37 }
  • 相关阅读:
    转载 线程池 异步I/O线程 <第三篇>
    转载 线程初步了解
    gdal库中设置prj4库全路径的用法
    比较ArrayList、LinkedList、Vector
    JavaScript创建日志文件并记录时间的做法
    从length与length()开始谈Java
    Java异常处理示例
    hudson添加批处理编译命令的注意事项
    使用相对路径导入ado库的方法
    如何将字段中带逗号的SQLite数据库数据导入到MySQL
  • 原文地址:https://www.cnblogs.com/ArnoldSchwarzenegger/p/12266760.html
Copyright © 2011-2022 走看看