我自己写的贪吃蛇代码,在此感谢千里冰封的指导.可以运行在1.4的版本上.请大家多多指教!!!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Snake extends JFrame
{
private int sleep=300;
public int loc;
private MySnake ms;
public Rectangle food;
private boolean bool=false;
public boolean br=false;
public Snake()
{
Container c=getContentPane();
ms=new MySnake();
makeFood();
setSize(800,600);
setLocation(400,300);
show();
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
new Thread(new Runnable()
{
public void run()
{
while(true)
{
try
{
Thread.sleep(sleep);
}
catch (Exception e)
{
return;
}
if(br==false)
repaint();
}
}
}).start();
}
public void paint(Graphics g)
{
addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
loc=e.getKeyCode();
}
});
super.paint(g);
g.setColor(Color.RED);
g.fillRect(food.x,food.y,food.width,food.height);
ms.move(loc);
ms.drawMe(g);
ms.Food(food);
}
public static void main(String args[])
{
new Snake();
}
public void makeFood()
{
while(true)
{
int x=(int)(Math.random()*200);
int y=(int)(Math.random()*200);
if(x%22==0 && y%22==0 && x<778 && y<578 && x>22 && y>22)
{
food=new Rectangle(x,y,20,20);
break;
}
}
}
class MySnake
{
private ArrayList list,array;
private Rectangle rec,rect;
private int block=20;
private int bolckSpace=2;
private int j=0;
boolean bool=false;
private int total=0;
boolean b=false;
public MySnake()
{
list=new ArrayList();
array=new ArrayList();
list.add(new Rectangle(22+44,22,20,20));
list.add(new Rectangle(22+22,22,20,20));
list.add(new Rectangle(22,22,20,20));
}
public void Food(Rectangle food)
{
try
{
array.add(0,food);
rect=(Rectangle)array.get(0);
}
catch (Exception e)
{
return;
}
}
public void move(int x)
{
if(x==40)
{
bool=true;
rec=(Rectangle)list.get(0);
Rectangle rec1=new Rectangle(rec.x,rec.y+(block+bolckSpace),rec.width,rec.height);if(rec.y>556)b=true;
list.add(j,rec1);
}
else if(x==38)
{
bool=true;
rec=(Rectangle)list.get(0);
Rectangle rec1=new Rectangle(rec.x,rec.y-(block+bolckSpace),rec.width,rec.height);if(rec.y<44)b=true;
list.add(j,rec1);
}
else if(x==37)
{
bool=true;
rec=(Rectangle)list.get(0);
Rectangle rec1=new Rectangle(rec.x-(block+bolckSpace),rec.y,rec.width,rec.height);if(rec.x<44)b=true;
list.add(j,rec1);
}
else if(x==39)
{
bool=true;
rec=(Rectangle)list.get(0);
Rectangle rec1=new Rectangle(rec.x+(block+bolckSpace),rec.y,rec.width,rec.height);if(rec.x>756)b=true;
list.add(j,rec1);
}
if(b==true)
{
try
{
JOptionPane.showMessageDialog(Snake.this,"You Are Lose","Error Message",JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
catch (Exception e)
{
return;
}
}
}
public void drawMe(Graphics g)
{
if(bool==true)list.remove(list.size()-1);
g.setColor(Color.blue);
for(int i=0;i<list.size();i++)
{
rec=(Rectangle)list.get(i);
try
{
if(rect.x==rec.x && rect.y==rec.y){br=true;total=total+1;array.remove(0);makeFood();}
}
catch (Exception e)
{
return;
}
g.fillRect(rec.x,rec.y,rec.width,rec.height);
}
if(br==true && total%2==1){list.add(rect);}br=false;
}
}
};