📄 dogcanvas.java
字号:
import java.io.IOException;
import java.util.Random;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
/**
*
* @author kenriv
* @since 2008年5月3日 凌晨3时10分
* @version 1.0
*
* 算法及实现完全原创.支持触摸屏事件处理
*/
public class DogCanvas extends Canvas implements CommandListener, Runnable
{
private DogMidlet midlet;
// private Graphics g;//仅生成一个g对象.
private Random rand = new Random();
private static boolean isRunning = false;// 判断线程是否运行
/*
* 游戏中所用到的布尔判断控制
*/
private boolean isVoice = true;// 判断声音开关
private boolean isFirst = true;// 是否是第一次
private boolean isPause = true;// 是否暂停
private boolean isDraw = true;// 是否绘制
private boolean isChanged = true;// 是否改变
private boolean isCount = false;// 是否开始计时.
private boolean isCollision = true;// 控制碰撞的时候只击打一次.
private boolean isPointer = false;// 此选择指针事件还是键盘事件
/*
* 游戏中所用到的设置相关节炎
*/
private String[] Switch = new String[] { "开", "关" };// 声音开关字符
private String[] opreate = new String[] { "键盘", "指针(触摸屏)" };
private int[] coordinateX = new int[] { 15, 70, 125 };// 精灵当前的X坐标数组
private int[] coordinateY = new int[] { 55, 110, 165 };// 精灵当前的Y坐标数组
private int difficult = 0;// 难度系数
private static int ROW_COLUMNS = 4;// 矩阵的行与列数
private int attackCount = 0;// 击中的次数.
/*
* 游戏中精灵位置的坐标等
*/
private int curMoveX = 15;// 锤子的X坐标.
private int curMoveY = 55;// 锤子的Y坐标.
private int curPigX = 15;// 当前精灵猪的X位置
private int curPigY = 55;// 当前精灵猪的Y位置
private int curPointerX = 15;// 当前指针X的位置
private int curPointerY = 15;// 当前指针Y的位置
private int changedIndex = 0;// 是否开始计时
private int curSpeed = 8;// 变化速度
private int showPrevMoveX = 55;// 显示图片数字当前坐标.
private int showBackMoveX = 25;// 显示图片数字十位时的坐标.
/*
* i游戏中所用到的常量。
*/
private final static short FRAME_TIME = 80;// 控制线程,每秒80帧
private final static short STATE_SPLASH = 0;// 游戏状态:闪屏
private final static short STATE_START = 1;// 游戏状态:开始
private final static short STATE_SETTINGS = 2;// 游戏状态:设置
private final static short STATE_HELP = 3;// 游戏状态:帮助
private final static short STATE_INSTRUCTIONS = 4;// 游戏状态:说明
private final static short STATE_OVER = 5;// 游戏状态:结束
private final static short STATE_PAUSE = 6;// 游戏状态:暂停
private int state = STATE_SPLASH;// 保存当前的游戏状态,此为闪屏
/*
* 菜单的Command事件
*/
private Command com_start = new Command("start Game", Command.BACK, 1);
private Command com_settings = new Command("Settings", Command.BACK, 1);
private Command com_help = new Command("Help", Command.BACK, 1);
private Command com_instructions = new Command("Instructions",
Command.BACK, 1);
private Command com_Exit = new Command("Exit Game", Command.BACK, 1);
private Command com_continue = new Command("Continue", Command.BACK, 1);
/*
* 游戏中所用到的资源
*/
private Image imgSplash = null;// 闪屏图片
private Image imgNewPig = null;// 击中前的精灵图片
private Image imgOldPig = null;// 击中后的精灵图片
private Image imgHammer = null;// 锤子的精灵图片
private Image imgShow = null;// 显示条的精灵图片
private Image imgNumber = null;// 数字的精灵图片
private Image imgHammerDown = null;// 锤子落下后的精灵图片
private Image imgRest = null;
private Sprite sprRole = null;// Pig精灵
private Sprite sprHammer = null;// 锤子精灵
private Sprite sprNumber = null;// 数字精灵
private Sprite sprIntegerNumber = null;// 整数精灵
static Player player;// 当前播放的音乐对象
/*
* 游戏资源的初始化.
*/
public DogCanvas(DogMidlet midlet) {
this.midlet = midlet;
imgSplash = DogMidlet.createImage("splash.png");
imgNewPig = DogMidlet.createImage("newRole.png");
imgOldPig = DogMidlet.createImage("oldRole.png");
imgHammer = DogMidlet.createImage("hammer.png");
imgShow = DogMidlet.createImage("show.png");
imgHammerDown = DogMidlet.createImage("hammerDown.png");
imgNumber = DogMidlet.createImage("number.png");
imgRest = DogMidlet.createImage("rest.png");
sprRole = new Sprite(imgNewPig, 55, 53);
sprHammer = new Sprite(imgHammer);
sprNumber = new Sprite(imgNumber, 10, 13);
sprIntegerNumber = new Sprite(imgNumber, 10, 13);
sprIntegerNumber.setVisible(false);
if (isFirst)
{
player = midlet.createPlayer("back.mid", "audio/midi");
midlet.startPlayer(player, -1);
isFirst = false;
}
GameMenu();
start();
}
/*
* 线程的启动.
*/
public void start()
{
isRunning = true;
Thread thread = new Thread(this);
thread.start();
}
/*
* 线程的中止.
*/
public static void stop()
{
isRunning = false;
}
/*
* 不同界同间的切换绘制.
*
*/
protected void paint(Graphics g)
{
switch (state) {
case STATE_SPLASH:
drawSplash(g);
break;
case STATE_START:
drawStart(g);
break;
case STATE_SETTINGS:
drawSettings(g);
break;
case STATE_HELP:
drawHelp(g);
break;
case STATE_INSTRUCTIONS:
drawInstructions(g);
break;
case STATE_PAUSE:
drawPause(g);
midlet.pauseApp();
break;
case STATE_OVER:
drawOver(g);
break;
}
// TODO Auto-generated method stub
}
/*
* 添加游戏菜单
*/
public void GameMenu()
{
this.addCommand(new Command("", Command.BACK, 1));
this.addCommand(com_Exit);
this.addCommand(com_instructions);
this.addCommand(com_help);
this.addCommand(com_settings);
this.addCommand(com_start);
this.setCommandListener(this);
}
public void delMenu()
{
this.removeCommand(com_start);
this.removeCommand(com_settings);
this.removeCommand(com_help);
this.removeCommand(com_instructions);
this.removeCommand(com_Exit);
this.addCommand(com_Exit);
this.addCommand(com_instructions);
this.addCommand(com_help);
this.addCommand(com_continue);
}
/*
* 绘制游戏闪屏
*/
public void drawSplash(Graphics g)
{
g.setColor(0xffffff);
g.fillRect(0, 0, getWidth(), getHeight());
g.drawImage(imgSplash, 0, 0, Graphics.LEFT | Graphics.TOP);
}
/*
* 绘制游戏开始
*/
public void drawStart(Graphics g)
{
g.setColor(0xffffff);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0x0000ff);
// 明天早上这里用那个算法去实现.
g.drawImage(imgShow, showPrevMoveX, 35, Graphics.TOP | Graphics.LEFT);
g.drawImage(imgRest, (getWidth() >> 1) - 50, getHeight() - 70,
0x10 | 0x4);
sprNumber.setPosition(showPrevMoveX + 75, 38);
sprIntegerNumber.setPosition(showBackMoveX + 75, 38);
sprNumber.paint(g);
sprIntegerNumber.paint(g);
if (showPrevMoveX == 15 || showBackMoveX == 25)
{
showPrevMoveX = 125;
showBackMoveX = 115;
} else
{
showPrevMoveX -= 5;
showBackMoveX -= 5;
}
// g.drawImage(arg0, arg1, arg2, arg3) 图片式的数字实现.
g.setColor(0x000000);
for (int i = 1, j = 0; i <= ROW_COLUMNS && j <= ROW_COLUMNS; i++, j++)
{
g.drawLine(15, 55 * i, 180, 55 * i);
g.drawLine(55 * j + 15, 55, 55 * j + 15, 220);
}
sprRole.setPosition(curPigX, curPigY);
if (isDraw)
{
sprRole.setImage(imgNewPig, 55, 53);
} else
{
sprRole.setImage(imgOldPig, 55, 53);
isDraw = true;
}
sprRole.paint(g);
if (isPointer)
{
sprHammer.setPosition(curPointerX - 10, curPointerY - 10);
} else
{
sprHammer.setPosition(curMoveX, curMoveY);
}
if (isChanged)
{
sprHammer.setImage(imgHammer, 49, 49);
} else
{
sprHammer.setImage(imgHammerDown, 66, 66);
isChanged = true;
}
sprHammer.paint(g);
// sprRole.setImage(imgOldDog, 55,53);
}
/*
* 绘制游戏帮助
*/
public void drawHelp(Graphics g)
{
g.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD,
Font.SIZE_LARGE));
g.setColor(0xffffff);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0x0000ff);
// 此处没有使用算法进行换行,暂时还看不明白.没仔细看.
g.drawString("游戏帮助:", 5, 5, Graphics.LEFT | Graphics.TOP);
g.drawString("3*3矩形内短暂时间内", 5, 35, Graphics.LEFT | Graphics.TOP);
g.drawString("随机出现地猪", 5, 65, Graphics.LEFT | Graphics.TOP);
g.drawString("操作:上,下,左,右,2", 5, 95, Graphics.LEFT | Graphics.TOP);
g.drawString(",4,6,8,支持触摸屏", 5, 125, Graphics.LEFT | Graphics.TOP);
g.drawString("5键与中键确定-#键返回", 5, 155, Graphics.LEFT | Graphics.TOP);
}
/*
* 绘制游戏设置
*/
public void drawSettings(Graphics g)
{
g.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD,
Font.SIZE_LARGE));
g.setColor(0xffffff);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0xff00ff);
g.drawString("*开关-7,9难度系数--#返回", (getWidth() >> 1) - 100,
(getHeight() >> 1) - 70, 0x10 | 0x4);
if (isVoice)
{
g.setColor(0x0000ff);
g.drawString("声音-----" + Switch[0], (getWidth() >> 1) - 100,
(getHeight() >> 1) - 100, 0x10 | 0x4);
//
} else
{
g.setColor(0x0000ff);
g.drawString("声音-----" + Switch[1], (getWidth() >> 1) - 100,
(getHeight() >> 1) - 100, 0x10 | 0x4);
}
g.setColor(0xff0000);
g.drawString("难度系数:" + difficult, (getWidth() >> 1) - 100,
(getHeight() >> 1) - 10, 0x10 | 0x4);
g.setColor(0x0000ff);
g.drawString("7-增加-9-减少", (getWidth() >> 1) - 100,
(getHeight() >> 1) + 15, 0x10 | 0x4);
g.setColor(0x0000ff);
if (!isPointer)
{
g.drawString("操作方式:" + opreate[0], (getWidth() >> 1) - 100,
(getHeight() >> 1) - 50, 0x10 | 0x4);
} else
{
g.drawString("操作方式:" + opreate[1], (getWidth() >> 1) - 100,
(getHeight() >> 1) - 50, 0x10 | 0x4);
}
g.drawString("-1-2-进行选择", (getWidth() >> 1) - 100,
(getHeight() >> 1) - 32, 0x10 | 0x4);
}
/*
* 绘制游戏说明
*/
public void drawInstructions(Graphics g)
{
g.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD,
Font.SIZE_LARGE));
g.setColor(0xffffff);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0xff00ff);
g.drawString("only a test,nothing!", 10, 10, Graphics.LEFT
| Graphics.TOP);
g.drawString("#键返回", 60, 40, Graphics.LEFT | Graphics.TOP);
}
/*
* 绘制游戏暂停
*/
public void drawPause(Graphics g)
{
g.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD,
Font.SIZE_LARGE));
g.setColor(0xff0000);
g.drawString("暂停中.....", (getWidth() >> 1) - 70,
(getHeight() >> 1) - 50, 0x10 | 0x4);
}
/*
* 绘制游戏结束
*/
public void drawOver(Graphics g)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -