码迷,mamicode.com
首页 > 编程语言 > 详细

java贪吃蛇

时间:2015-08-18 21:22:34      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:贪吃蛇   java   

/*-----Yard.java-----*/
package tanchishe;

import java.awt.Color;
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 {

    public static final int ROWS = 45;  
    public static final int COLS = 45;
    public static final int BLOCK_SIZE = 15;

    Image offScreenImage = null;

    Snake s = new Snake(); //蛇对象

    Egg e = new Egg();

    @Override
    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.DARK_GRAY);
        for(int i=1;i<ROWS;i++) {
            g.drawLine(0, BLOCK_SIZE*i, COLS*BLOCK_SIZE ,BLOCK_SIZE*i);
        }

        //划竖线
        for(int i=1;i<COLS;i++) {
            g.drawLine(BLOCK_SIZE*i, 0, BLOCK_SIZE*i, ROWS*BLOCK_SIZE);
        }

        g.setColor(c);
        s.draw(g);  //画蛇
        e.draw(g);  //画蛋
        s.eat(e);
    }

    @Override
    public void update(Graphics g) {   //去除屏幕闪烁

        if(offScreenImage == null) {
            offScreenImage = this.createImage(COLS*BLOCK_SIZE , ROWS*BLOCK_SIZE);
            Graphics gOff = offScreenImage.getGraphics();
            paint(gOff);
            g.drawImage(offScreenImage, 0, 0, null);
        }
    }

    public void lanch() {
        this.setLocation(200,300);
        this.setSize(COLS*BLOCK_SIZE, ROWS*BLOCK_SIZE);

        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        this.setVisible(true);
        this.addKeyListener(new KeyMonitor());

        new Thread(new PaintThread()).start();

    }

    private class PaintThread implements Runnable { //新建画图线程
        public void run() {
            while(true) {
                repaint(); 
                try {
                    Thread.sleep(100);

                } 
                catch (Exception e) {
                    e.getMessage();
                }
            }   
        }
    }

    public class KeyMonitor extends KeyAdapter { //键盘输入
        @Override
        public void keyPressed(KeyEvent e) {
            s.keyPressed(e);
        }

    }

    public static void main(String[] args) {
        new Yard().lanch(); 

    }
}
/*-----Snake.java-----*/
package tanchishe;

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); //节点初始化

    public Snake() {  //蛇初始化
        head = n;
        tail = n;
        size = 1;   
    }

    public void addToTail() {
        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;
        for(Node n=head; n!=null; n=n.next) {
            n.draw(g);    //画蛇每个节点
        }
        move();
    }

    private void move() {  //移动
        addToHead();//前面增加一节
        deleteFromTail();//后面减少一节
    }

    private void deleteFromTail() { //后面减少一节
        if(size==0) return;
        tail = tail.prev;
        tail.next = null;

    }

    public void keyPressed(KeyEvent e) { //键盘输入
        int key = e.getKeyCode();
        switch(key) {
        case KeyEvent.VK_LEFT :
            head.dir = Dir.L;
            break;
        case KeyEvent.VK_UP :
            head.dir = Dir.U;
            break;
        case KeyEvent.VK_RIGHT :
            head.dir = Dir.R;
            break;
        case KeyEvent.VK_DOWN :
            head.dir = Dir.D;
            break;
        }

    }

    public void eat(Egg e) {
        if(this.getRect().intersects(e.getRect())) {
            e.reAppear();
            this.addToHead();
        }   
    }

    private Rectangle getRect() {    //得到蛇头位置
        return new Rectangle(Yard.BLOCK_SIZE * head.col, Yard.BLOCK_SIZE * head.row, head.w, head.h);
    }

    private class Node {
        int w = Yard.BLOCK_SIZE; //节点宽度
        int h = Yard.BLOCK_SIZE; //节点高度
        int row, col;
        Node next = null;
        Node prev = null;
        Dir dir = Dir.L;

        public Node(int row,int col, Dir dir) {
            this.row = row;
            this.col = col;
            this.dir = dir;
        }   

        private void draw(Graphics g) {
            Color c = g.getColor();
            g.setColor(Color.yellow);
            g.fillRect(Yard.BLOCK_SIZE*col, Yard.BLOCK_SIZE*row, w, h);
            g.setColor(c);
        }
    }

}
/*-----Egg.java-----*/
package tanchishe;

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;
    }

    public void reAppear() {
        this.row = r.nextInt(Yard.ROWS);
        this.col = r.nextInt(Yard.COLS);
    }

    int row, col;
    int w = Yard.BLOCK_SIZE;
    int h = Yard.BLOCK_SIZE;

    private static Random r = new Random();

    public Egg(int row, int col) {
        this.row = row;
        this.col = col;
    }

    public Egg() {
        this(r.nextInt(Yard.ROWS), r.nextInt(Yard.COLS));
    }

    public Rectangle getRect() {
        return new Rectangle(Yard.BLOCK_SIZE * col, Yard.BLOCK_SIZE * row, w, h);
    }

    public void draw(Graphics g) { //画蛋
        Color c = g.getColor();
        g.setColor(Color.GREEN);
        g.fillRect(Yard.BLOCK_SIZE*col, Yard.BLOCK_SIZE*row, w, h);
        g.setColor(c);
    }
}
/*-----Dir.java-----*/
package tanchishe;

public enum Dir {  //枚举方向
    L, U, R, D;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

java贪吃蛇

标签:贪吃蛇   java   

原文地址:http://blog.csdn.net/sjtu_chenchen/article/details/47759521

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!