import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.util.*;
class POS
{
int x, y;
void Set(int X, int Y)
{
x = X;
y = Y;
}
}
class Snake extends GameCanvas implements Runnable
{
//绘图对象
Graphics m_Graphics;
//随机数
Random m_Random;
//游戏循环标志
boolean m_Continue;
//游戏宽、高
final int m_W, m_H;
//游戏块宽、高
final int m_KW, m_KH;
//蛇最大长度
final int m_SnakeMaxLen;
//蛇
POS[] m_Snake;
//蛇当前长度
int m_SnakeLen;
//蛇头方向,0、1、2、3代表上下左右
int m_SnakeDir;
//果子
POS m_Apple;
Snake()
{
super(false);
//得到绘图对象
m_Graphics = getGraphics();
//得到随机数对象
m_Random = new Random();
//初始化游戏循环标志
m_Continue = true;
//初始化游戏宽高
m_W = m_H = 20;
//初始化游戏块宽高
m_KW = m_KH = 8;
//初始化蛇最大长度
m_SnakeMaxLen = m_W * m_H;
//初始化蛇
m_Snake = new POS[m_SnakeMaxLen];
for (int i = 0; i < m_SnakeMaxLen; ++i)
m_Snake[i] = new POS();
//初始化蛇长度
m_SnakeLen = 1;
//初始化蛇头位置
m_Snake[0].Set(10, 10);
//初始化蛇头方向
m_SnakeDir = 0;
//初始化果子
m_Apple = new POS();
m_Apple.Set(m_Random.nextInt(m_W), m_Random.nextInt(m_H));
}
public void run()
{
try
{
while (m_Continue)
{
//得到输入
int iInput = getKeyStates();
//上
if ((iInput & GameCanvas.UP_PRESSED) != 0)
{
m_SnakeDir = 0;
}
//下
if ((iInput & GameCanvas.DOWN_PRESSED) != 0)
{
m_SnakeDir = 1;
}
//左
if ((iInput & GameCanvas.LEFT_PRESSED) != 0)
{
m_SnakeDir = 2;
}
//右
if ((iInput & GameCanvas.RIGHT_PRESSED) != 0)
{
m_SnakeDir = 3;
}
//蛇运动
switch (m_SnakeDir)
{
case 0: m_Snake[0].y--;break;
case 1: m_Snake[0].y++;break;
case 2: m_Snake[0].x--;break;
case 3: m_Snake[0].x++;break;
}
//与果子的碰撞检测
if (m_Snake[0].x == m_Apple.x && m_Snake[0].y == m_Apple.y)
{
m_Apple.Set(m_Random.nextInt(m_W), m_Random.nextInt(m_H));
System.out.println("吃了1个果子");
}
//绘制
m_Graphics.setColor(0xffff00);
m_Graphics.fillRect(0, 0, m_KW * m_W, m_KH * m_H);
m_Graphics.setColor(0x000000);
for (int i = 0; i < m_SnakeLen; ++i)
m_Graphics.drawRect(m_Snake[i].x * m_KW, m_Snake[i].y * m_KH, m_KW, m_KH);
m_Graphics.setColor(0xff0000);
m_Graphics.drawRect(m_Apple.x * m_KW, m_Apple.y * m_KH, m_KW, m_KH);
flushGraphics();
Thread.sleep(120);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
public class Test extends MIDlet
{
Snake m_Snake;
Display m_Display;
Thread m_Thread;
public Test()
{}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException
{}
protected void pauseApp()
{}
protected void startApp() throws MIDletStateChangeException
{
//创建游戏对象
m_Snake = new Snake();
//得到本程序的界面管理器
m_Display = Display.getDisplay(this);
//创建游戏运行线程
m_Thread = new Thread(m_Snake);
//开启游戏运行线程
m_Thread.start();
//设置游戏画面为当前界面
m_Display.setCurrent(m_Snake);
}
}
可以提供源代码给你,但是我也要复习,没有时间帮你写解析 ,需要源代码 发邮件给我,lokepaqi@qq.com,说明要什么的,有简单聊天窗口,坦克 贪吃蛇没有写过
现在谁家也不缺孝子,又跪又求的,瞧这点出息!
-----------
可以使用百度Hi通知我
有时间可以解决你的问题
相近的要求也可以通知我
跪求高手帮忙写个JAVA程序嘛 交期末祖业的 不要太复杂了 ...
ES:\\436DC3636876573ABB4C87981541C3A6