基本信息
源码名称:Java 贪吃蛇 示例源码下载
源码大小:3.22KB
文件格式:.java
开发语言:Java
更新时间:2014-11-12
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍
利用Java语言编写的贪吃蛇


package firstBag;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;

import ListenerBag.Listener;
import ToolsBag.tools;

public class Snack {
	
	public static  final int UP=-1;
	public static  final int DOWN=1;
	public static  final int LEFT=2;
	public static  final int RIGHT=-2;
	private int oldDirection,newDirection;
	private Point tail;
	private boolean life;
	private LinkedList<Point> body=new LinkedList<Point>();
	private Set<Listener> listeners = new HashSet<Listener>(); //注册多个事件监听器
	public Snack(){                                 //实现蛇的初始化
		init();                           //初始化方法
	}
	public void init(){
		int x=tools.WIDTH/2;
		int y=tools.HEIGHT/2;
		for(int i=0;i<2;i  ){              //初始化蛇的身体大小
			body.addLast(new Point(x--,y));
		}
		oldDirection=newDirection=RIGHT;
		life=true;
	}
	public void Move(){             //蛇的移动方法
		System.out.println("snack's Move");
		if(!(oldDirection newDirection == 0)){
			oldDirection=newDirection;
		}
		tail=body.removeLast();      //1.去尾
		int x=body.getFirst().x;
		int y=body.getFirst().y;
		switch(oldDirection){
		case UP:
			y--;
			if(y<0){
				y=tools.HEIGHT-1;
			}
			break;
		case DOWN:
			y  ; 
			if(y>=tools.HEIGHT){
				y=0;
			}
			break;
		case LEFT:
			x--;  
			if(x<0){
				x=tools.WIDTH-1;
			}
			break;
		case RIGHT:
			x  ; 
			if(x>=tools.WIDTH){
				x=0;
			}
			break;
		}
		Point Newhead=new Point(x,y);       //新的蛇头坐标
		body.addFirst(Newhead);          //将新的蛇头坐标加上实现加头
	} 
	
	public void Changedirection(int direction){               //蛇的改变方向方法
		System.out.println("changed direction");
			newDirection=direction;
	}
	
	public void EatFood(){                         //蛇吃食物的方法
		System.out.println("eat food");              
		body.addLast(tail);                //蛇吃到食物后将上面移动是去掉的尾巴重新加在蛇身体后面
	}
	
	public boolean Iseatbody(){                     //判断蛇是否吃到自己的身体
		System.out.println("shi fou chidaozijide shenti  ");
		for(int i=1;i<body.size();i  ){
			if(body.get(i).equals(this.getHead())){
				return true;
			}
		}
		return false;
	}
	
	public void  over(){               //结束游戏
		life=false;
	}
	
	public void Draw(Graphics g){                //将蛇的所有属性显示在屏幕上
		System.out.println("snack xianshi ");
		g.setColor(Color.RED);
		for(Point p:body){
			g.fill3DRect(p.x*tools.CELL_SIZE, p.y*tools.CELL_SIZE
					, tools.CELL_SIZE, tools.CELL_SIZE, true);
		}
	}
	public Point getHead(){
		return body.getFirst();
	}
	private class Snackfreemove implements Runnable{   //通过写一个内部类实现蛇的自动游走
		public void run() {
			// TODO Auto-generated method stub
			while(life)
			{
				Move();
				for(Listener L : listeners){
					L.snackmove(Snack.this);
			     }
			try  {
					Thread.sleep(200);
				 } catch (Exception e) {
					e.printStackTrace();
				    }
			}
		}
	}
		public void start(){
			new Thread(new Snackfreemove()).start();
		}
		
		public void addListener(Listener L){
			if(L !=null)
				this.listeners.add(L);
		}
	}