简易贪吃蛇(JAVA版)源代码

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.LinkedList;

import javax.swing.*;
class snakeModel extends JPanel implements Runnable{
	public snakeModel(){
		iniMap();
		createFood();
	}
	private int[][] map=new int[90][50];
	private LinkedList snakeBody=new LinkedList();//记录snake状态
	//private LinkedList rcrdSnake;//记录snake坐标值
	private LinkedList rcrdSnake;//记录snake坐标值
	private JButton startButton=new JButton("开始"),
					pauseButton=new JButton("暂停"),
					exitButton=new JButton("退出");
	private int foodLocationX,foodLocationY;
	private int direct=1,status=0;//1=右,2=左,5=上,6=下
	private int speedTime=100;//设置snake运动速度
	protected void iniMap(){//初始化游戏,没有实体部分全部设置为0
		for(int i=0;i<90;i++){
			for(int j=0;j<50;j++)
				map[i][j]=0;
		}
		/**初始化snake**/
		addSnakeBody(44,25);
		addSnakeBody(45,25);
		addSnakeBody(46,25);
	}
	protected void addSnakeBody(int x,int y){//记录snake状态
		rcrdSnake=new LinkedList();//记录snake坐标值
		rcrdSnake.add(x);
		rcrdSnake.addLast(y);
		map[x][y]=1;
		snakeBody.addFirst(rcrdSnake);
	}
	protected void removeSnakeBody(){
		int x,y;
		rcrdSnake=(LinkedList) snakeBody.getLast();
		x=(int)rcrdSnake.getFirst();
		y=(int)rcrdSnake.getLast();
		map[x][y]=0;
		snakeBody.removeLast();
	}
	protected boolean move(){
		int x,y;
		rcrdSnake=(LinkedList) snakeBody.getFirst();
		x=(int)rcrdSnake.getFirst();
		y=(int)rcrdSnake.getLast();
		switch(direct){
			case 1://向右运动
				x++;break;
			case 2://向左运动
				x--;break;
			case 5://向上运动
				y--;break;
			case 6://向下运动
				y++;break;
		}
		if(x>89||x<0||y<0||y>49){//碰到边界
			//drawString("Failed");
			status=1;
			return false;
		}
		else if(x==foodLocationX&&y==foodLocationY){//吃到食物
			addSnakeBody(x,y);
			createFood();
			//return true;
		}
		else {//普通移动
			if(map[x][y]==1){
				System.out.printf("\nfoodX%d foodY%d\n",foodLocationX,foodLocationY);
				System.out.printf("\nX%d Y%d\n",x,y);
				System.out.printf("\n蛇体%d\n",map[x][y]);
				return false;
			}
			else{//碰到自己
				System.out.printf("\nX%d Y%d\n",x,y);
				System.out.printf("\n蛇体%d\n",map[x][y]);
				addSnakeBody(x,y);
				//snakeBody.removeLast();
				removeSnakeBody();//移除snake最后一个点
				System.out.println(snakeBody.size());
				//return false;
			}
		}
		return true;
		
	}
	protected void changeDirect(int newDirect){
		if(Math.abs(newDirect-direct)!=1)
			direct=newDirect;
			
	}
	protected void createFood(){//生成食物
		int i,j;
		do{
			i=(int)Math.rint(Math.random()*89);
			j=(int)Math.rint(Math.random()*49);
		}while(map[i][j]==1);
		map[i][j]=1;
		foodLocationX=i;
		foodLocationY=j;
		System.out.println("createFooding....");
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(move()){
			try{
				System.out.println("running");
				repaint();
				Thread.sleep(speedTime);
			}catch(InterruptedException e){
				System.out.println(e);
			}
		}
		//move();
	}

	public void paintComponent(Graphics g){//绘制贪吃蛇并创建食物
		super.paintComponent(g);
		g.setColor(Color.RED);
		g.drawRect(10, 0, 544, 304);//绘制游戏边框
		int x,y;
		for(int i=0;i

你可能感兴趣的:(源代码)