日期:2014-05-20 浏览次数:20694 次
import javax.swing.*;
import java.awt.*;
import java.util.*;
class GameThread implements Runnable
{
private GamePanle gamePanel;
private final int fps = 40;
public GameThread(final GamePanle gamePanel)
{
this.gamePanel = gamePanel;
}
public void run()
{
while (true)
{
try
{
gamePanel.logic();
gamePanel.draw();
Thread.sleep(1000/fps);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
class Snake
{
//蛇头的开始位置
public int x = 200;
public int y = 100;
private int speed = 5;
//1,2,3,4 表示4个方向
private byte direct = 1;
//简单起见就3节
private ArrayList<Body> bodys = new ArrayList<Body>();
class Body
{
int x,y;
int w = 20;
int h = 20;
//0 往下走 1往上走 2 往右 3 往左
int dir = 3;
public Body(int x,int y)
{
this.x = x;
this.y = y;
}
public void move()
{
//测试距离
int dleft = x;
int dright = GamePanle.WIDTH - x;
int dtop = y;
int dbottom = GamePanle.HEIGHT - y;
System.out.println("{"+dleft+","+dright+","+dtop+","+dbottom+"}");
//靠左边的时候
if (dleft <= w)
{
//到了左上角
if (dtop < h)
{
dir = 1 ;
}
//到了左下角
else if (dbottom < h)
{
dir = 2 ;
}
else
//中间
{
dir = 0;
}
}
//靠右边的时候
if (dright <= w)
{