首页

JavaSE贪吃蛇源码

java

2020-6-28

下载地址

该程序使用JavaSE技术完成,IDE工具为Eclipse,可运行,运行类为Yard

下载后请阅读先阅读readme文件

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

/*
 * 作为一个内部类,必须先new出包装类,才能new其内部的类
 * 蛇移动时就相当于将蛇尾部节点去掉,在头部加一个新的节点
 * 
 * */

public class Snake {
	Node head = null;
	Node tail = null;
	int size = 0;
	Node n = new Node(20, 30, Direction.UP);		//初始节点
	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;		//指定是哪个Yard对象
	}
	
	//蛇的尾部添加方法,未使用
	public void addToTail() {
		Node node = null;
		switch(tail.dir) {
			case UP :
				node = new Node(tail.x, tail.y   1, tail.dir);
				break;
			case RIGHT :
				node = new Node(tail.x - 1, tail.y, tail.dir);
				break;
			case DOWN :
				node = new Node(tail.x, tail.y - 1, tail.dir);
				break;
			case LEFT :
				node = new Node(tail.x   1, tail.y, tail.dir);
				break;
			//default :;break;
		}
		tail.next = node;			//构成双向回环
		node.prev = tail;		//构建双向回环列表
		tail = node;
		//size   ;
	}
	
	//蛇的头部添加方法
	public void addToHead() {
		Node node = null;
		switch(head.dir) {
			case UP :
				node = new Node(head.x, head.y - 1,head.dir);
				break;
			case RIGHT :
				node = new Node(head.x   1, head.y, head.dir);
				break;
			case DOWN :
				node = new Node(head.x, head.y   1, head.dir);
				break;
			case LEFT :
				node = new Node(head.x - 1, head.y, head.dir);
				break;
			//default :;break;
		}
		node.next = head;		//把这个新的节点放在头的上面
		head.prev = node;		//构建双向回环列表
		head = node;		//把这个新的节点变成新的头节点
		//size   ;
	}
	
	//蛇的尾部节点删除方法
	private void deleteFromTail() {
		// TODO Auto-generated method stub&
		//if (tail == null) return;		判断蛇是否存在
		if(size == 0) return;
		tail = tail.prev;
		tail.next = null;
	}
	
	//蛇的移动方法
	private void move() {
		// TODO Auto-generated method stub&
		addToHead();
		deleteFromTail();
		isDead();
	}
	
	//判断死亡
	public boolean isDead() {
		if (head.x < 1 || head.x > Yard.COLS - 1 || head.y < 3 || head.y > Yard.ROWS - 1) {		//判断蛇是否出界
			return false;
		}
		if (size > 1) {
			if (this.getRect(head).intersects(this.getRect(tail))) {			//在蛇身大于1的情况下,蛇身是否与蛇尾相撞
				return false;
			}
		}
		for(Node node = head.next;node != null;node = node.next) {		//判断蛇与身体是否相撞
			if (head.x == node.x && head.y == node.y)
				return false;
		}
		return true;
	}
	
	//画出这条蛇
	public void draw(Graphics g) {
		if (size <=0) return;
		move();		//调用蛇的移动方法,最好放在前面,否则运行时可能不太灵敏
		for(Node node = head; node != null; node = node.next) {		//画出该蛇的各个节点
			node.draw(g);
		}
	}

	//节点类,使用内部类构建
	private class Node {
		int w = Yard.BLOCK_SIZE;
		int h = Yard.BLOCK_SIZE;
		int x,y;		// x应为列数cols,y应为行数rows
		Direction dir = Direction.LEFT;
		Node next = null;
		Node prev = null;
		
		public Node(int x,  int y, Direction dir) {
			//super();&
			this.x = x;
			this.y = y;
			this.dir = dir;
		}
		
		void draw(Graphics g) {
			Color c = g.getColor();
			g.setColor(Color.BLACK);
			g.fillRect(x * Yard.BLOCK_SIZE,  y * Yard.BLOCK_SIZE,  w,  h);
			g.setColor(c);
		}
	}//结束内部类
	
	//获取节点所在位置
	private Rectangle getRect(Node obj) {
		return new Rectangle(Yard.BLOCK_SIZE * obj.x, Yard.BLOCK_SIZE * obj.y, obj.w, obj.h);
	}
	
	//检测蛇头与蛋碰撞
	public void eat(Egg e) {
		if(this.getRect(head).intersects(e.getRect())) {		//相交,Rectangle类中的一个方法,判断矩形是否相交
			e.reApear();
			this.addToHead();
			this.size   ;
			y.score  = 5;
		}
	}
	
	//检测键盘事件
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub&
		int key = e.getKeyCode();
		switch(key) {
			case KeyEvent.VK_UP:
				if (head.dir != Direction.DOWN)		//阻止蛇向相反方向变向
					head.dir = Direction.UP;
				break;
			case KeyEvent.VK_RIGHT:
				if (head.dir != Direction.LEFT)
					head.dir = Direction.RIGHT;
				break;
			case KeyEvent.VK_DOWN:
				if (head.dir != Direction.UP)	
					head.dir = Direction.DOWN;
				break;
			case KeyEvent.VK_LEFT:
				if (head.dir != Direction.RIGHT)
					head.dir = Direction.LEFT;
				break;
		}
	}
} 
资源下载此资源下载价格为3D币,请先

资源文件列表

贪吃蛇/readme.txt , 91
贪吃蛇/Snake/.classpath , 301
贪吃蛇/Snake/.project , 381
贪吃蛇/Snake/.settings/org.eclipse.jdt.core.prefs , 598
贪吃蛇/Snake/bin/Direction.class , 986
贪吃蛇/Snake/bin/Egg.class , 1424
贪吃蛇/Snake/bin/Snake$Node.class , 1084
贪吃蛇/Snake/bin/Snake.class , 3538
贪吃蛇/Snake/bin/Yard$1.class , 599
贪吃蛇/Snake/bin/Yard$KeyMonitor.class , 908
贪吃蛇/Snake/bin/Yard$PaintThread.class , 1250
贪吃蛇/Snake/bin/Yard.class , 3334
贪吃蛇/Snake/src/Direction.java , 51
贪吃蛇/Snake/src/Egg.java , 1154
贪吃蛇/Snake/src/Snake.java , 4427
贪吃蛇/Snake/src/Yard.java , 4137
贪吃蛇/snake.exe , 770048
没有账号? 忘记密码?

社交账号快速登录