⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snake.java

📁 贪吃蛇java版本。自己写的
💻 JAVA
字号:
import java.awt.Graphics;


public class Snake {
	public static final int UP    = 0;
	public static final int DOWN  = 1;
	public static final int RIGHT = 2;
	public static final int LEFT  = 3;
	//private Node food;
	private int curDirect;//当前方向
	
	private Node head = new Node(0, 0, null);//头节点
	private Node tail = head;//尾节点
	
	public Snake(int length)//构造函数,length为初始长度
	{
		curDirect = Snake.RIGHT;
		for(int i=0; i<length; i++)
		{//一开始有一定的长度
			this.insert(new Node(i,0));
		}
	}
	
	public Node getHead()
	{
		return this.head;
	}
	
	public Node getTail()
	{
		return this.tail;
	}
	
	public void display(Graphics g)//显示
	{
		Node p = head.getNext();
		while(p != null)
		{
			p.display(g);
			p = p.getNext();
		}
	}
	
	public void insert(Node node)//添加节点
	{
		tail.setNext(node);
		tail = tail.getNext();
	}
	
	
	
	public void eat(Node food,FoodManager fm)
	{
		this.insert(food);
		fm.setEaten(true);
	}
	
	public void move()//移动
	{//小蛇(链表)的头上删掉一个节点,在尾部加入一个新的节点(新走到的地方)
		Node p;
		if(curDirect == Snake.DOWN)
		{
			p = new Node(tail.getX(), tail.getY()+1);
			this.insert(p);
			head.setNext(head.getNext().getNext());
		}
		else if(curDirect == Snake.UP)
		{
			p = new Node(tail.getX(), tail.getY()-1);
			this.insert(p);
			head.setNext(head.getNext().getNext());
		}
		else if(curDirect == Snake.RIGHT)
		{
			p = new Node(tail.getX()+1, tail.getY());
			this.insert(p);
			head.setNext(head.getNext().getNext());
		}
		else if(curDirect == Snake.LEFT)
		{
			p = new Node(tail.getX()-1, tail.getY());	
			this.insert(p);
			head.setNext(head.getNext().getNext());
		}
	}
	
	public boolean goToBoundary(GamePanel gamePanel)//判断蛇是否走出界了
	{
		int x = gamePanel.getWidth();
		int y = gamePanel.getHeight();
		
		if((tail.getX() >= x/10 - 1 && this.curDirect == Snake.RIGHT)
				||(tail.getX() <= 0 && this.curDirect == Snake.LEFT) 
				||(tail.getY() <= 0 && this.curDirect == Snake.UP)
				||(tail.getY() >= y/10 - 1 && this.curDirect == Snake.DOWN))
		{//出界了
			return true;
		}
		else
			return false;
	}
	
	public boolean eatSelf()
	{
		Node p = head.getNext();
		
		while(p != tail)
		{
			if(p.equals(this.tail))
			{
				return true;
			}
			p = p.getNext();
		}
		
		return false;
	}
	
	public void changeDirect(int direct)//改变方向
	{
		switch(direct)
		{
		case Snake.UP:
			if(curDirect != Snake.DOWN)
				curDirect = Snake.UP;
			break;
		case Snake.DOWN:
			if(curDirect != Snake.UP)
				curDirect = Snake.DOWN;
			break;
		case Snake.RIGHT:
			if(curDirect != Snake.LEFT)
				curDirect = Snake.RIGHT;
			break;
		case Snake.LEFT:
			if(curDirect != Snake.RIGHT)
				curDirect = Snake.LEFT;
			break;
		}
	}
	
	/**
	 * 判断是否吃到食物
	 * @param food
	 * @return 吃到食物返回true,未吃到返回false
	 */
	public boolean eatFood(Node food)
	{
		if(this.tail.equals(food))
			return true;
		else 
			return false;
		/*switch(this.curDirect)
		{
		case Snake.UP:
			p = new Node(this.tail.getX(), this.tail.getY()-1);
			break;
		case Snake.DOWN:
			p = new Node(this.tail.getX(), this.tail.getY()+1);
			break;
		case Snake.RIGHT:
			p = new Node(this.tail.getX()+1, this.tail.getY());
			break;
		case Snake.LEFT:
			p = new Node(this.tail.getX()-1, this.tail.getY());
			break;
		default:
			break;
		}*/
		
		//if(p.equals(food))
		//if(p.getX() == food.getX() && p.getY() == food.getY())
			//return true;
		//else 
			//return false;
	}
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -