鱼C论坛

 找回密码
 立即注册
查看: 3015|回复: 2

[学习笔记] 0基础学习Java ——贪吃蛇的项目(跟别人学的...)

[复制链接]
发表于 2017-8-15 10:40:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 回忆一遥远 于 2017-8-16 07:34 编辑

先上各个文件

Dir.java
public enum Dir {
        L, U, R, D
}
记录了四个方向的标识



Egg.java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Random;

public class Egg {
        public int getRow() {
                return row;
        }

        public void setRow(int row) {
                this.row = row;
        }

        public int getCol() {
                return col;
        }

        public void setCol(int col) {
                this.col = col;
        }

        int row, col;
        int w = Yard.BLOCK_SIZE;
        int h = Yard.BLOCK_SIZE;
        private static Random r = new Random();
        private Color color = Color.GREEN;
        
        public Egg(int row, int col) {
                this.row = row;
                this.col = col;
        }

        public Egg() {
                this(r.nextInt(Yard.ROWS - 3) + 3, r.nextInt(Yard.COLS));
        }
        
        public void reAppear() {
                this.row = r.nextInt(Yard.ROWS - 3) + 3;
                this.col = r.nextInt(Yard.COLS);
        }
        
        public Rectangle getRect() {
                return new Rectangle(Yard.BLOCK_SIZE * col, Yard.BLOCK_SIZE * row, w, h);
        }
        
        void draw(Graphics g) {
                Color c = g.getColor();
                g.setColor(color);
                g.fillOval(Yard.BLOCK_SIZE * col, Yard.BLOCK_SIZE * row, w, h);
                g.setColor(c);
                if(color == Color.GREEN) color = Color.BLUE;
                else color = Color.GREEN;
        }
}
定义和蛇要吃的蛋的行为和属性

Snake.java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;

public class Snake {
        private Node head = null;
        private Node tail = null;
        private int size = 0;
        
        private Node n = new Node(20, 30, Dir.L);
        private Yard y;
        
/*        public Snake(Node node) {
                head = node;
                tail = node;
                size = 1;
        }*/
        
        public Snake(Yard y) {
                head = n;
                tail = n;
                size = 1;
                this.y = y;
        }
        
        public void addToTall() {
                Node node = null;
                switch(tail.dir) {
                case L :
                        node = new Node(tail.row, tail.col + 1, tail.dir);
                        break;
                case U :
                        node = new Node(tail.row + 1, tail.col, tail.dir);
                        break;
                case R :
                        node = new Node(tail.row, tail.col - 1, tail.dir);
                        break;
                case D :
                        node = new Node(tail.row - 1, tail.col, tail.dir);
                        break;
                }
                tail.next = node;
                node.prev = tail;
                tail = node;
                size++;
        }
        
        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);
                        break;
                }
                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() {
                // TODO Auto-generated method stub
                addToHead();
                deleteFromTail();
                checkDead();
        }

        private void checkDead() {
                // TODO Auto-generated method stub
                if(head.row < 2 || head.col < 0 || head.row > Yard.ROWS || 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() {
                // TODO Auto-generated method stub
                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 = Dir.L;
                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) {
                // TODO Auto-generated method stub
                int key = e.getKeyCode();
                switch(key) {
                case KeyEvent.VK_LEFT:
                        if(head.dir != Dir.R)
                                head.dir = Dir.L;
                        break;
                case KeyEvent.VK_UP:
                        if(head.dir != Dir.D)
                                head.dir = Dir.U;
                        break;
                case KeyEvent.VK_RIGHT:
                        if(head.dir != Dir.L)
                                head.dir = Dir.R;
                        break;
                case KeyEvent.VK_DOWN:
                        if(head.dir != Dir.U)
                                head.dir = Dir.D;
                        break;
                }
        }
}
定义了蛇的行为和属性



Yard.java
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
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();
        public boolean gameOver = false;
        
        public static final int ROWS = 50;
        public static final int COLS = 50;
        public static final int BLOCK_SIZE = 20;
        
        private int score = 0;
        
        Snake s = new Snake(this);
        Egg e = new Egg();
        
        Image offScreenImage = null;
        
        public void launch() {
                this.setLocation(300, 300);
                this.setSize(COLS * BLOCK_SIZE, ROWS * BLOCK_SIZE);
                this.addWindowListener(new WindowAdapter() {

                        @Override
                        public void windowClosing(WindowEvent e) {
                                // TODO Auto-generated method stub
                                System.exit(0);
                        }
                        
                });
                this.setVisible(true);
                this.addKeyListener(new KeyMonitor());
                
                new Thread(paintThread).start();
                //new Thread(paintThread).start();
        }
        
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                new Yard().launch();
        }

        public void stop() {
                gameOver = true;
        }
        
        @Override
        public void paint(Graphics g) {
                // TODO Auto-generated method stub
                Color c = g.getColor();
                g.setColor(Color.GRAY);
                g.fillRect(0, 0, COLS * BLOCK_SIZE, ROWS * BLOCK_SIZE);
                g.setColor(Color.DARK_GRAY);
                // Print line
                for(int i = 0; i < ROWS; i++) {
                        g.drawLine(0, BLOCK_SIZE * i, COLS * BLOCK_SIZE, BLOCK_SIZE * i);
                }
                for(int i = 0; i < COLS; i++) { 
                        g.drawLine(BLOCK_SIZE * i, 0, BLOCK_SIZE * i, BLOCK_SIZE * ROWS);
                }
                
                g.setColor(Color.YELLOW);
                g.drawString("score: " + score, 10, 60);
                
                if(gameOver) {
                        g.setFont(new Font("Verdana", Font.BOLD, 100));
                        g.drawString("Game over", 180, 400);

                        paintThread.pause();
                }
                
                g.setColor(c);
                
                s.eat(e);
                e.draw(g);
                s.draw(g);
                
                
        }
        
        @Override
        public void update(Graphics g) {
                // TODO Auto-generated method stub
                if(offScreenImage == null) {
                        offScreenImage = this.createImage(COLS * BLOCK_SIZE, ROWS * BLOCK_SIZE);
                }
                Graphics gOff = offScreenImage.getGraphics();
                paint(gOff);
                g.drawImage(offScreenImage, 0, 0, null);
        }
        
        
        private class PaintThread implements Runnable {
                private boolean runing = true;
                private boolean pause = false;
                @Override
                public void run() {
                        // TODO Auto-generated method stub
                        while(runing) {
                                if(pause) continue;
                                else repaint();
                                
                                try {
                                        Thread.sleep(120);
                                } catch (InterruptedException e) {
                                        e.printStackTrace();
                                }
                        }
                        
                }
                public void pause() {
                        this.pause = true;
                }
                
                public void reStart() {
                        /*this.pause = false;
                        s = new Snake(Yard.this);
                        gameOver = false;*/
                        new Yard().launch();
                }
                
                /*public void gameOver() {
                        runing = false;
                }*/
        }
        
        private class KeyMonitor extends KeyAdapter{

                @Override
                public void keyPressed(KeyEvent e) {
                        int key = e.getKeyCode();
                        if(key == KeyEvent.VK_F2) {
                                paintThread.reStart();
                        }
                        // TODO Auto-generated method stub
                        s.keyPressed(e);
                }
                
        }
        
        public int getScore() {
                return score;
        }
        
        public void setScore(int score) {
                this.score = score;
        }
        
}
定义了环境的属性...和游戏的判断(规则)

操作方法:方向键控制方向,F2 重置游戏


噗~不能上传图片了,明天再上传吧  




评分

参与人数 1鱼币 +7 收起 理由
小甲鱼 + 7

查看全部评分

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-8-15 11:10:51 | 显示全部楼层
数组可以使用 length 来获取数组的长度

Math.random() 获取 0-1 之间的随机数
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-8-16 17:13:56 | 显示全部楼层
谢谢分享,学习下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-20 22:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表