原帖及讨论:http://bbs.bc-cn.net/dispbbs.asp?boardid=8&id=100035
//做的不太好 有点白痴 没有什么扩展功能 请多多批评
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class TestSneak extends JPanel implements KeyListener,Runnable{
final int width=30;
final int height=20;
int x=0,y=0;
int a[][]=new int[21][31];
int direction=4;
final int up=1;
final int down=3;
final int left=2;
final int right=4;
final int length=8;//初始化长度
final int initx=8;
final int inity=5;
Node node;
Thread thread;
Food food=new Food();
LinkedList linklist;
SpecialThread st;
int count;
boolean pause=true;
public TestSneak(){
this.setBackground(Color.black);
//this.setSize(400,320);
this.addKeyListener(this);
this.setLayout(new BorderLayout());
this.setOpaque(true);
}
public void init()
{
arrayIni();
direction=4;
count=0;
linklist=new LinkedList();
for(int i=10;i>=inity;i--)
{
node=new Node(initx,i);
a[initx]=1;
linklist.add(node);
}
for(int j=0;j<=30;j++){
a[0][j]=3;
a[20][j]=3;
}
for(int i=0;i<=20;i++){
a[0]=3;
a[30]=3;
}
this.creatFood();
}
public void arrayIni()
{
for(int i=0;i<20;i++)
{
for(int j=0;j<30;j++)
{
a[j]=0;
}
}
}
public void creatFood()
{
do
food.creatFood();
while(a[food.randomx][food.randomy]==1||a[food.randomx][food.randomy]==3);
a[food.randomx][food.randomy]=2;
count++;
///label.setText(String.valueOf(count));
}
public void changeDirection(int i)
{
if((i%