相信大家都玩过nokia手机上的贪吃蛇游戏。在该游戏中,玩家操纵一条贪吃的蛇在迷宫里行走,贪吃蛇按玩家所按的方向键折行,蛇头吃到各种食物(比如大力丸)后,会有各种反应(比如蛇身变长),如果贪吃蛇碰上墙壁或者自身的话,就gameover了(当然也可能是减去一条生命)。
要实现该游戏其实并不麻烦,关键就是要找到一个合适的核心算法。本文就给出一个参考实现,你可以基于该demo做扩展。要说明的一点是:本文只演示最核心的算法,要实现一个完整的游戏,你还需要做很多的扩展,重构。
实例代码
该程序包括3个java文件。一个是snakemidlet,另外2个分别是一个canvas(snakecanvas)和一个代表贪吃蛇的类snake: snakemidlet.java
import javax.microedition.lcdui.display;
import javax.microedition.midlet.midlet;
import javax.microedition.midlet.
midletstatechangeexception;
/**
* @author jagie
*/
public class snakemidlet extends midlet
{
protected void startapp()
throws midletstatechangeexception
{
// todo auto-generated method stub
display.getdisplay(this)
.setcurrent(new snakecanvas());
}
/* (non-javadoc)
* @see javax.microedition
.midlet.midlet#pauseapp()
*/
protected void pauseapp()
{
// todo auto-generated method stub
}
/* (non-javadoc)
* @see javax.microedition
.midlet.midlet#destroyapp(boolean)
*/
protected void destroyapp(boolean arg0)
throws midletstatechangeexception
{
// todo auto-generated method stub
}
}
snakecanvas.java
import javax.microedition.lcdui.canvas;
import javax.microedition.lcdui.graphics;
/**
* @author jagie
*
*/
public class snakecanvas extends
canvas implements runnable
{
snake snake=new snake();
snakecanvas(){
snake.init();
new thread(this).start();
}
protected void paint(graphics g)
{
g.setcolor(0);
g.fillrect(0,0,this.getwidth(),
this.getheight());
snake.paint(g);
}
/**
* 游戏主线程,驱动蛇移动
*/
public void run()
{
while(true){
snake.move();
repaint();
try
{
thread.sleep(50);
} catch (interruptedexception e)
{
// todo auto-generated
catch block
e.printstacktrace();
}
}
}
/**
* 按键相应,产生新蛇头
*/
protected void keypressed(int c)
{
int ga=this.getgameaction(c);
switch (ga)
{
case canvas.up:
snake.breakinto(1);
break;
case canvas.down:
snake.breakinto(3);
break;
case canvas.left:
snake.breakinto(4);
break;
case canvas.right:
snake.breakinto(2);
break;
}
}
}
snake.java
import java.util.vector;
import javax.microedition.lcdui.graphics;
/**
*
* @author jagie
* 贪吃蛇
*/
public class snake
{
//蛇环节,每个环节为一个int[] sec
//sec[0]:环节起点x,sec[1]:
环节起点y,sec[2]:环节方向,sec[3]:
环节长度
vector sections = new vector();
/**
* 初始化sections
* 开始的时候,整条蛇只有一段。
*
*/
public void init()
{
int[] head =
{ 10, 10, 2, 50 };
sections.addelement(head);
}
/**
* 绘制
* @param g
*/
public synchronized
void paint(graphics g)
{
if (sections.isempty())
{
return;
}
g.setcolor(0, 255, 0);
for (int i = 0; i < sections.size();
i++)
{
int[] sec = (int[])
sections.elementat(i);
//sec[0]:起点x,sec[1]:
起点y,sec[2]:方向,sec[3]:
长度
switch (sec[2]) {
case 1:
g.drawline(sec[0], sec[1],
sec[0], sec[1] - sec[3]);
break;
case 2:
g.drawline(sec[0], sec[1],
sec[0] + sec[3], sec[1]);
break;
case 3:
g.drawline(sec[0], sec[1],
sec[0], sec[1] + sec[3]);
break;
case 4:
g.drawline(sec[0], sec[1],
sec[0] - sec[3], sec[1]);
break;
}
}
}
/**
*
* @author jagie
*
* 蛇的爬行。本质上是蛇头长度++,蛇尾长度--。
同时移动蛇尾起点。如果蛇尾长度小于0,则去掉蛇尾。
*/
public synchronized void move()
{
if (sections.isempty())
{
return;
}
//蛇尾
int[] tail = (int[])
sections.elementat
(sections.size() - 1);
//蛇头
int[] head = (int[])
sections.elementat(0);
//根据蛇尾环节的方向移动蛇尾。
switch (tail[2])
{
case 1:
tail[1]--;
break;
case 2:
tail[0]++;
break;
case 3:
tail[1]++;
break;
case 4:
tail[0]--;
break;
}
//蛇尾缩短
tail[3]--;
//蛇头增长
head[3]++;
//蛇尾<0,则去掉蛇尾
if (tail[3] <= 0)
{
sections.removeelement(tail);
}
}
/**
* 蛇分段
* @param dir 新蛇头的方向
*/
public synchronized void
breakinto(int dir)
{
if (sections.isempty())
{
return;
}
int[] head = (int[])
sections.elementat(0);
//新蛇头方向和旧蛇头方向一致,
则无反应。
//todo 可以考虑加速。
if (dir == head[2])
{
return;
}
//增加新蛇头
int[] newhead=new int[4];
//新蛇头的起始位置,
与旧蛇头的运动方向有关。
switch (head[2])
{
case 1:
newhead[0]=head[0];
newhead[1]=head[1]-head[3];
newhead[2]=dir;
newhead[3]=0;
//蛇头总是第一个元素
sections.insertelementat(newhead, 0);
break;
case 2:
newhead[0]=head[0]+head[3];
newhead[1]=head[1];
newhead[2]=dir;
newhead[3]=0;
sections.insertelementat(newhead, 0);
break;
case 3:
newhead[0]=head[0];
newhead[1]=head[1]+head[3];
newhead[2]=dir;
newhead[3]=0;
sections.insertelementat(newhead, 0);
break;
case 4:
newhead[0]=head[0]-head[3];
newhead[1]=head[1];
newhead[2]=dir;
newhead[3]=0;
sections.insertelementat(newhead, 0);
break;
}
}
}
闽公网安备 35060202000074号