这是我做的Java版贪食蛇:做得不好,不过你可以参考
/* Java版贪食蛇游戏 1.0版
* 玩法:蛇水平移动时,只能向上或向下改变方向。如果鼠标在蛇头的上方,
* 则方向改为向上,如果鼠标在蛇头的下方,则方向改为向下。
* 蛇上下移动时,只能向左或向右改变方向。如果鼠标在蛇头的左方,
* 则方向改为向左,如果鼠标在蛇头的右方,则方向改为向右。
*/
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.TimerTask;
import java.util.Timer;
import java.util.Random;
import javax.swing.JOptionPane;
public class Snake extends Applet {
//用于产生新的蛇身
Label star;
//定时器
Timer timer;
//定时器任务
myTimerTask timerTask;
//用于记录运行时间
TimerTotal timerTotal;
//初始蛇身数
int starNumber = 5;
//当前鼠标位置,与蛇的运动方向相比较,决定改变的方向
int mouseX , mouseY;
//目标蛇身的位置
int starX = 200 , starY = 152;
//用于产生蛇身位置的随机类
Random r;
//状态栏上显示的信息
String msg = "向下";
//蛇身的位置,X坐标和Y坐标
public int[][] mystar = new int[100][2];
//每次移动的距离
int moveDistance = 8;
//游戏级别,每吃到5个*,级别加1,速度减20
int level = 1;
//吃到的*数量
int eatStarNumbers = 0;
//允许移动的最快速度
int MaxSpeed = 60;
//游戏速度,每30秒,速度减20,值越小,速度越快
int speed = 500;
//用于显示的速度
int showSpeed = 1;
//原来的移动方向,用于重新调度
int olddirection = 2;
public void init() {
setBackground(Color.pink);
setForeground(Color.blue);
requestFocus();
addMouseListener(new MyMouseAdapter());
addMouseMotionListener(new MyMouseMotionAdapter());
addKeyListener(new MyKeyAdapter());
setLayout(null);
for(int i = starNumber-1 ;
i >= 0 ;
i--) {
mystar[0] = 200 ;
mystar[1] = (starNumber-i) * moveDistance;
}
star = new Label("*");
star.setBounds(starX , starY , 8 , 8);
this.add(star);
timer = new Timer();
timerTask = new myTimerTask();
timerTotal = new TimerTotal();
timer.schedule(timerTask , 0 , speed);
timer.schedule(timerTotal , 1000 * 30 , 1000 * 30 );
}
class TimerTotal extends TimerTask {
public void run() {
if (speed > MaxSpeed) {
speed -= 20;
showSpeed++;
}
timer.cancel();
timer = new Timer();
timerTask = new myTimerTask();
timerTotal = new TimerTotal();
timer.schedule(timerTask , 0 , speed);
timer.schedule(timerTotal , 1000 * 30 , 1000 * 30 );
}
}
class MyKeyAdapter extends KeyAdapter {
public void keyPressed(KeyEvent ke) {
int direction = timerTask.getdirection();
int key = ke.getKeyCode();
if (direction == 1 || direction == 2) {
if (key == KeyEvent.VK_RIGHT) {
timerTask.setdirection(4);
olddirection = 4;
msg = "向右";
}
else
if (key == KeyEvent.VK_LEFT) {
timerTask.setdirection(3);
olddirection = 3;
msg = "向左";
}
}
if (direction == 3 || direction == 4) {
if (key == KeyEvent.VK_DOWN) {
timerTask.setdirection(2);
olddirection = 2;
msg = "向下";
}
else
if (key == KeyEvent.VK_UP) {
timerTask.setdirection(1);
olddirection = 1;
msg = "向上";
}
}
}
}
class MyMouseMotionAdapter extends MouseMotionAdapter {
public void mouseMoved(MouseEvent me) {
mouseX = me.getX();
mouseY = me.getY();
}
}
class MyMouseAdapter extends MouseAdapter {
public void mousePressed(MouseEvent e) {
int direction = timerTask.getdirection();
//上下移动时,只判断鼠标水平位置
if (direction == 1 || direction == 2) {
if (mouseX > mystar[0][0]) {
timerTask.setdirection(4);
olddirection = 4;
msg = "向右";
}
else
{
timerTask.setdirection(3);
olddirection = 3;
msg = "向左";
}
}
//左右移动时,只判断鼠标垂直位置
if (direction == 3 || direction == 4) {
if (mouseY > mystar[0][1]) {
timerTask.setdirection(2);
olddirection = 2;
msg = "向下";
}
else
{
timerTask.setdirection(1);
olddirection = 1;
msg = "向上";
}
}
}
}
public void setstarLocation() {
r = new Random();
int X = r.nextInt(47);
int Y = r.nextInt(35);
starX = (X + 1) * moveDistance ;
starY = (Y + 1) * moveDistance ;
star.setLocation(starX,starY);
}
class myTimerTask extends TimerTask {
//direction 为移动的方向,1向上,2向下,3向左,4向右
//移动的方向
int direction = olddirection ;
int ii = 1;
int dead = 0;
public void setdirection(int direction) {
this.direction = direction;
}
public int getdirection() {
return this.direction;
}
public void run() {
for(int i = starNumber-1 ;
i >=1 ;
i--) {
mystar[0] = mystar[i-1][0];
mystar[1] = mystar[i-1][1];
}
switch(direction) {
case 1: //向上
//如果超范围,停止
if (mystar[0][1] < moveDistance * 2 ) {
timer.cancel();
dead = 1;
new ErrorDialog("你的贪食蛇越界了!");
}
else
mystar[0][1] -= moveDistance;
break;
case 2: //向下
//如果超范围,停止
if (mystar[0][1] >= 300 ) {
timer.cancel();
dead = 1;
new ErrorDialog("你的贪食蛇越界了!");
}
else
mystar[0][1] += moveDistance;
break;
case 3: //向左
//如果超范围,停止
if (mystar[0][0] <= 0 ) {
timer.cancel();
dead = 1;
new ErrorDialog("你的贪食蛇越界了!");
}
else
mystar[0][0] -= moveDistance;
break;
case 4: //向右
//如果超范围,停止
if (mystar[0][0] >= 400 - moveDistance ) {
timer.cancel();
dead = 1;
new ErrorDialog("你的贪食蛇越界了!");
}
else
mystar[0][0] += moveDistance;
break;
}
if (dead ==0) {
//以下判断吃*号 如果蛇头与*号坐标相同,则吃
if (mystar[0][0] == starX &&
mystar[0][1] == starY + moveDistance &&
starNumber < 100) {
starNumber++;
mystar[starNumber-1][0] = mystar[starNumber-2][0];
mystar[starNumber-1][1] = mystar[starNumber-2][1];
eatStarNumbers++;
if (eatStarNumbers == 5) {
eatStarNumbers = 0;
level++;
if (speed > MaxSpeed) {
speed -= 20;
showSpeed++;
}
timer.cancel();
timer = new Timer();
timerTask = new myTimerTask();
timerTotal = new TimerTotal();
timer.schedule(timerTask , 0 , speed);
timer.schedule(timerTotal , 1000 * 30 , 1000 * 30 );
}
setstarLocation();
}
else
{
//以下判断是否吃到了自己
for (ii=1;ii<starNumber;ii++) {
if(mystar[0][0]==mystar[ii][0] &&
mystar[0][1]==mystar[ii][1]) {
timer.cancel();
new ErrorDialog("你的贪食蛇吃自己了!!");
break;
}
}
}
repaint();
}
}
}
class ErrorDialog extends JOptionPane {
public ErrorDialog(String message) {
this.showMessageDialog(this,message,"游戏结束",JOptionPane.INFORMATION_MESSAGE);
}
}
public void paint(Graphics g) {
int i = 0;
while (i < starNumber) {
g.drawString("*",mystar[0] + 2,mystar[1] + 1);
i++;
}
showStatus(msg + " 目标:" + star.getX() + " , " + star.getY()
+ " 蛇头: " + mystar[0][0] + " , " + (mystar[0][1] - moveDistance)
+ " 数量:" + starNumber + " 级别:" + level + " 速度:" + showSpeed);
}
}