自己写了个贪吃蛇的小程序,程序还没有完全完成,目前为止遇到几个问题,请高手指教下,
代码如下:
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import javax.swing.*;
//节点类,覆盖equals方法给LinkedList的contains用
class Slot
{
int x;
int y;
Slot(int x,int y)
{
this.x = x;
this.y = y;
}
Slot()
{
this(0,0);
}
public boolean equals(Object o)
{
Slot s = (Slot) o;
return x == s.x && y == s.y;
}
}
//存储每一帧的数据
class SnakeandAppleData
{
//定义常量:40*20的格子,蛇的方向
static final int LENGTH = 40;
static final int HEIGTH = 20;
static final int SNAKE_UP = 0;
static final int SNAKE_DOWN = 1;
static final int SNAKE_LEFT = 2;
static final int SNAKE_RIGHT = 3;
//自身数据:蛇身数据,苹果位置,蛇头方向
private LinkedList<Slot> snakeBody = new LinkedList<Slot>();
private Slot apple = new Slot();
private int direction = SNAKE_UP;
//初始化蛇身
private void init()
{
snakeBody.add(new Slot(LENGTH/2,HEIGTH/2-1));
snakeBody.add(new Slot(LENGTH/2,HEIGTH/2));
snakeBody.add(new Slot(LENGTH/2,HEIGTH/2+1));
}
//初始化,蛇刚开起于中心,自带三个节点
SnakeandAppleData()
{
init();
setRandomApple();
}
//判定:蛇与苹果(蛇头碰到或则苹果在蛇身里),蛇与墙的关系
public boolean isOnSelf()
{
if(snakeBody.contains(apple))
return true;
return false;
}
public boolean isOnWall()
{
if(snakeBody.getFirst().x <= 1 && direction ==SNAKE_LEFT
|| snakeBody.getFirst().x >=LENGTH && direction ==SNAKE_RIGHT
|| snakeBody.getFirst().y <=1 && direction ==SNAKE_UP
|| snakeBody.getFirst().y >=HEIGTH && direction == SNAKE_DOWN)
return true;
return false;
}
public boolean isOnApple()
{
if(snakeBody.getFirst().equals(apple))
return true;
return false;
}
//设置随机苹果,要求苹果不能在蛇身里生成
public void setRandomApple()
{
apple.x = (int)(Math.random()*LENGTH) +1;
apple.y = (int)(Math.random()*HEIGTH) +1;
while(isOnSelf())
{
apple.x = (int)(Math.random()*LENGTH) +1;
apple.y = (int)(Math.random()*HEIGTH) +1;
}
System.out.println("apple.x: "+apple.x+" apple.x: "+apple.y);
}
//蛇的动作:根据方向移动,吃苹果增加头部
public void moveOnly()
{
moveandEat();
//snakeBody.pop();会移除链表的第一个元素
snakeBody.removeLast();
}
public void moveandEat()
{
if(direction == SNAKE_UP)
{
snakeBody.addFirst(new Slot(snakeBody.getFirst().x,snakeBody.getFirst().y-1));
}
if(direction == SNAKE_DOWN)
{
snakeBody.addFirst(new Slot(snakeBody.getFirst().x,snakeBody.getFirst().y+1));
}
if(direction == SNAKE_LEFT)
{
snakeBody.addFirst(new Slot(snakeBody.getFirst().x-1,snakeBody.getFirst().y));
}
if(direction == SNAKE_RIGHT)
{
snakeBody.addFirst(new Slot(snakeBody.getFirst().x+1,snakeBody.getFirst().y));
}
}
public void resetAll()
{
snakeBody.clear();
init();
setRandomApple();
}
//设置方向
public void setDirection(int direction)
{
this.direction = direction;
}
//获取内部数据
public LinkedList<Slot> getSnakeData()
{
return snakeBody;
}
public Slot getApple()
{
return apple;
}
}
//制作一个动态画板,画帧并更新帧
class SnakePanel extends JPanel implements Runnable,KeyListener
{
//游戏速度
private int speed = 0;
//游戏分数
private int score = 0;
//游戏暂停与否,蛇还活着与否
private boolean isPaused = true ;
private boolean isKilled = false;
//蛇数据
private SnakeandAppleData snakeData = new SnakeandAppleData();
//对参数进行设定
public void setSpeed(int speed)
{
this.speed = speed;
}
public void setPaused(boolean b)
{
isPaused = b;
}
public void setDirection(int direction)
{
snakeData.setDirection(direction);
}
public int getSpeed()