原文地址:JAVA简易贪吃蛇的实现 作者:云台听雪
//---主方法-----------------
public class GreedSnake {
public static void main(String[] args) {
SnakeModel model=new SnakeModel();
SnakeControl control=new SnakeControl(model);
SnakeView view=new SnakeView(model,control);
new Thread(model).start();
new Thread(view).start();
}
}
//---SnakeModel------------
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Random;
public class SnakeModel implements Runnable {
static boolean [][]matrix;
int maxX,maxY;
static int freshTime;
int bufDir;
Node head;
Node food;
public static final int UP=1;
public static final int DOWN=3;
public static final int LEFT=2;
public static final int RIGHT=4;
LinkedList snakeList=new LinkedList();
public Node createFood(){
Random r=new Random();
int x,y;
while(true){
x=r.nextInt(maxX);
y=r.nextInt(maxY);
if(!matrix[x][y]){
break;
}
}
Node n=new Node(x,y,LEFT);
matrix[x][y]=true;
System.out.println("matrix "+x+" "+y);
return n;
}
public SnakeModel(){
head=new Node(2,3,RIGHT);
maxX=30;
maxY=30;
freshTime=200;
matrix=new boolean[maxX][maxY];
snakeList.add(head);
snakeList.add(new Node(2,2,DOWN));
food=createFood();
bufDir=head.drct;
}
public void move(){
ListIterator it=snakeList.listIterator();
//吃掉食物
System.out.println("head "+head.x+" "+head.y);
boolean near=false;
switch(head.drct){
case UP:
if(food.x==head.x&&food.y==(head.y-1)){
near=true;
}
break;
case DOWN:
if(food.x==head.x&&food.y==(head.y+1)){
near=true;
}
break;
case LEFT:
if(food.x==(head.x-1)&&food.y==head.y){
near=true;
}
break;
case RIGHT:
if(food.x==(head.x+1)&&food.y==head.y){
near=true;
}
break;
}
if(near){
System.out.println("eat");
snakeList.addFirst(food);
food.drct=head.drct;
head=food;
food=createFood();
near=false;
}
else{
it.next();
while(it.hasNext()){
Node n=it.next();
if(head.x==n.x&&head.y==n.y){
System.out.println("你输了");
System.exit(0);
}
}
while(it.hasPrevious()){
Node n=it.previous();
if(it.hasPrevious()){
if(snakeList.get(it.previousIndex()).xx){
n.drct=LEFT;
}
else if(snakeList.get(it.previousIndex()).x>n.x){
n.drct=RIGHT;
}
else if(snakeList.get(it.previousIndex()).yy){
n.drct=UP;
}
else if(snakeList.get(it.previousIndex()).y>n.y){
n.drct=DOWN;
}
}
}
while(it.hasNext()){
Node n=it.next();
try{
nodeMove(n);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("你输了");
System.exit(0);
}
}
}
}
public void changeDirection(){
if(Math.abs(bufDir-head.drct)==2){
bufDir=head.drct;
}
else
head.drct=bufDir;
}
public void nodeMove(Node n){
matrix[n.x][n.y]=false;
switch(n.drct){
case UP:
n.y--;
break;
case DOWN:
n.y++;
break;
case LEFT:
n.x--;
break;
case RIGHT:
n.x++;
break;
}
matrix[n.x][n.y]=true;
}
public void run(){
while(true){
changeDirection();
move();
try{
Thread.sleep(freshTime);
}catch(Exception e){}
}
}
}
class Node{
int x;
int y;
int drct;
Node(int x,int y,int direct){
this.x=x;
this.y=y;
drct=direct;
}
}
//---SnakeControl-------------------------
import java.awt.Frame;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class SnakeControl extends KeyAdapter{
SnakeModel model;
SnakeControl(SnakeModel model){
this.model=model;
}
public void keyPressed(KeyEvent e){
switch(e.getKeyCode()){
case 38:
model.bufDir=SnakeModel.UP;
break;
case 40:
model.bufDir=SnakeModel.DOWN;
break;
case 37:
model.bufDir=SnakeModel.LEFT;
break;
case 39:
model.bufDir=SnakeModel.RIGHT;
}
}
}
//---SnakeView----------------------
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class SnakeView extends Frame implements Runnable{
int nodeWidth=10;
int nodeHeight=10;
SnakeView(SnakeModel model,SnakeControl control){
model.maxX=model.maxX;
model.maxY=model.maxY;
setSize(model.maxX*nodeWidth+20,model.maxY*nodeHeight+40);
setLocation(300,200);
setVisible(true);
addKeyListener(control);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
setBackground(Color.RED);
setResizable(false);
}
public void repaint(){
Graphics g=getGraphics();
for(int i=0;imatrix.length;i++){
for(int j=0;jmatrix[i].length;j++){
if(SnakeModel.matrix[i][j]){
g.setColor(Color.BLACK);
}
else{
g.setColor(Color.YELLOW);
}
g.fillRect(i*nodeWidth+10, j*nodeHeight+30, nodeWidth-1, nodeHeight-1);
}
}
}
public void run(){
while(true){
try{
Thread.sleep(SnakeModel.freshTime);
}catch(Exception e){
System.out.println("refresh error");
}
repaint();
}
}
}