[原创]贪吃蛇
//做的不太好 有点白痴 没有什么扩展功能 请多多批评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][i]=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[i][0]=3;
a[i][30]=3;
}
this.creatFood();
}
public void arrayIni()
{
for(int i=0;i<20;i++)
{
for(int j=0;j<30;j++)
{
a[i][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%2)!=(direction%2))
{
direction=i;
}
}
public boolean go()
{
Node node1=(Node)linklist.getFirst();
x=node1.x;
y=node1.y;
switch(direction)
{
case up:
//x=node1.x;
x--;break;
case down:
//x=node1.x;
x++;break;
case right:
//y=node1.y;
y++;break;
case left:
//y=node1.y;
y--;break;
}
if((x>=20)||(y>=30)||(x<0)||(y<0)||(a[x][y]==1)||(a[x][y]==3))
{
//System.out.println(x+" "+y);
JOptionPane.showMessageDialog(null,"a 啊 不行了 ");
init();
return false;
}
else
{
if(a[x][y]==2)
{
linklist.addFirst(new Node(x,y));
a[x][y]=1;
creatFood();
}
else if(a[x][y]==0)
{
// System.out.println("asdf");
linklist.addFirst(new Node(x,y));
a[x][y]=1;
Node node=(Node)linklist.removeLast();
a[node.x][node.y]=0;
//System.out.println(node.x+"ok"+node.y+" "+a[node.x][node.y]);
}
repaint();
return true;
}
}
public void keyPressed(KeyEvent e)
{
//System.out.println("change");
if(e.getKeyCode()==KeyEvent.VK_UP)
{
changeDirection(up);
}
if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
changeDirection(down);
}
if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
changeDirection(left);
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{
changeDirection(right);
}
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
public void run()
{
init();
while(true)
{
try
{
if(pause)
{
Thread.sleep(100);
go();
//System.out.println("is go");
}
}catch(Exception e){System.out.println(e.getMessage());}
}
}
public void creatSpecial()
{
}
public void paint(Graphics g)
{
// g.setColor(Color.white);
// g.fillRect(0,0,150,100);
// System.out.println(direction);
super.paint(g);
g.setColor(Color.BLUE);
g.drawRect(0,0,300,200);
for(int i=0;i<=20;i++)
{
for(int j=0;j<=30;j++)
{
if(a[i][j]==1)
{
g.setColor(Color.red);
g.fillRect(10*j,10*i,9,9);
}
if(a[i][j]==2)
{
g.setColor(Color.yellow);
g.fillOval(10*j,10*i,10,10);
}
if(a[i][j]==3)
{
g.setColor(Color.magenta);
g.fillRect(10*j,10*i,9,9);
}
if(a[i][j]==4)
{
g.setColor(Color.green);
g.fillRect(10*j,10*i,9,9);
}
}
}
}
public static void main(String[] arg){
TestSneak sm=new TestSneak();
JFrame frame=new JFrame();
frame.add(sm,"Center");
frame.setUndecorated(true);
frame.setBounds(300,300,310,210);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
sm.setFocusable(true);
try
{
Thread thread =new Thread(sm);
thread.start();
}catch(Exception e){}
}
}
[此贴子已经被作者于2006-10-31 23:30:52编辑过]