import javax.microedition.lcdui.*;
public class Displayable1 extends Canvas implements CommandListener {
private int[] loc = new int[2];
private int[] SelectArea = new int[4];
private int[] MoveArea = new int[4];
private Map MyMap = new Map();
private boolean selected;
private int level;
public Displayable1() {
try {
jbInit();
}catch (Exception e) {
e.printStackTrace();
}
System.out.println("displayable has init.....");
}
private void Init_game(){
this.loc = MyMap.read_map(this.level);
this.SelectArea[0] = this.loc[0];
this.SelectArea[1] = this.loc[1];
this.SelectArea[2] = 1;
this.SelectArea[3] = 1;
this.MoveArea[0] = -1;
this.MoveArea[1] = -1;
this.MoveArea[2] = 0;
this.MoveArea[3] = 0;
}
private void jbInit() throws Exception {
this.selected = false;
this.level = 1;
Images.init();
Init_game();
setCommandListener(this);
addCommand(new Command("Exit", Command.EXIT, 1));
}
public void commandAction(Command command, Displayable displayable) {
if (command.getCommandType() == Command.EXIT) {
MIDlet1.quitApp();
}
}
public void paint(Graphics g) {
try {
g.drawImage(Images.image_Frame, 0, 0,
Graphics.TOP | Graphics.LEFT);
}
catch (Exception ex) {
System.out.println("there is a :"+ex);
}
MyMap.draw_map(g);
if ( this.selected )
g.setColor(0,255,0);
g.drawRect(this.SelectArea[0] * Images.UNIT + Images.LEFT,
this.SelectArea[1] * Images.UNIT + Images.TOP,
this.SelectArea[2] * Images.UNIT,
this.SelectArea[3] * Images.UNIT);
g.setColor(255,255,255);
if (this.selected) {
g.setColor(255, 0, 255);
g.drawRect(this.MoveArea[0] * Images.UNIT + Images.LEFT,
this.MoveArea[1] * Images.UNIT + Images.TOP,
this.MoveArea[2] * Images.UNIT,
this.MoveArea[3] * Images.UNIT);
g.setColor(255, 255, 255);
}
System.out.println(Runtime.getRuntime().freeMemory());
System.out.println(Runtime.getRuntime().totalMemory());
}
private void setRange() {
if (this.MyMap.Grid[this.loc[1]][this.loc[0]] == Images.DLEFT) {
this.loc[0] -= 1;
}else if (this.MyMap.Grid[this.loc[1]][this.loc[0]] == Images.DUP) {
this.loc[1] -= 1;
}else if (this.MyMap.Grid[this.loc[1]][this.loc[0]] == Images.DLEFTUP) {
this.loc[0] -= 1;
this.loc[1] -= 1;
}
this.SelectArea[0] = this.loc[0];
this.SelectArea[1] = this.loc[1];
if (this.loc[0] + 1 < Images.WIDTH) {
this.SelectArea[2] = this.MyMap.Grid[this.loc[1]][this.loc[0] + 1] != (byte) '1' ?
1 : 2;
}else {
this.SelectArea[2] = 1;
}
if (this.loc[1] + 1 < Images.HEIGHT) {
this.SelectArea[3] = this.MyMap.Grid[this.loc[1] + 1][this.loc[0]] != (byte) '2' ?
1 : 2;
}else {
this.SelectArea[3] = 1;
}
}
private boolean setMoveRange() {
for (int i = 0; i < this.SelectArea[2]; i++) {
for (int j = 0; j < this.SelectArea[3]; j++) {
if (this.loc[1] + j >= Images.HEIGHT ||
this.loc[0] + i >= Images.WIDTH ||
(!isInRange(this.loc[0] + i, this.loc[1] + j) &&
this.MyMap.Grid[this.loc[1] + j][this.loc[0] + i] !=
Images.BLANK)) {
return false;
}
}
}
this.MoveArea[0] = this.loc[0];
this.MoveArea[1] = this.loc[1];
this.MoveArea[2] = this.SelectArea[2];
this.MoveArea[3] = this.SelectArea[3];
return true;
}
private boolean isInRange(int x, int y) {
if (x >= this.SelectArea[0] &&
x < this.SelectArea[0] + this.SelectArea[2] &&
y >= this.SelectArea[1] &&
y < this.SelectArea[1] + this.SelectArea[3]) {
return true;
}else {
return false;
}
}
private boolean isInRange2(int x, int y) {
if (x >= this.MoveArea[0] &&
x < this.MoveArea[0] + this.MoveArea[2] &&
y >= this.MoveArea[1] &&
y < this.MoveArea[1] + this.MoveArea[3]) {
return true;
}else {
return false;
}
}
protected void keyPressed(int keyCode) {
switch (getGameAction(keyCode)) {
case Canvas.UP:
if (!this.selected) {
if (this.loc[1] - 1 >= 0) {
this.loc[1]--;
setRange();
repaint();
}
}else {
if (this.loc[1] - 1 >= 0) {
this.loc[1]--;
if (setMoveRange()) {
repaint();
}else {
this.loc[1]++;
}
}
}
break;
case Canvas.DOWN:
if (!this.selected) {
if (this.loc[1] + 1 < Images.HEIGHT) {
if (this.MyMap.Grid[this.loc[1] + 1][this.loc[0]] ==
Images.DUP){
this.loc[1]++;
if (this.loc[1] + 1 < Images.HEIGHT) {
this.loc[1]++;
setRange();
repaint();
}else {
this.loc[1]--;
}
}else {
this.loc[1]++;
setRange();
repaint();
}
}else {
}
}else {
if (this.loc[1] + 1 < Images.HEIGHT) {
this.loc[1]++;
if (setMoveRange()) {
repaint();
}else {
this.loc[1]--;
}
}
}
break;
case Canvas.LEFT:
if (!this.selected) {
if (this.loc[0] - 1 >= 0) {
this.loc[0]--;
setRange();
repaint();
}
}else {
if (this.loc[0] - 1 >= 0) {
this.loc[0]--;
if (setMoveRange()) {
repaint();
}else {
this.loc[0]++;
}
}
}
break;
case Canvas.RIGHT:
if (!this.selected) {
if (this.loc[0] + 1 < Images.WIDTH) {
if (this.MyMap.Grid[this.loc[1]][this.loc[0] + 1] ==
Images.DLEFT) {
this.loc[0]++;
if (this.loc[0] + 1 < Images.WIDTH) {
this.loc[0]++;
setRange();
repaint();
}else {
this.loc[0]--;
}
}else {
this.loc[0]++;
setRange();
repaint();
}
}else {
}
}else {
if (this.loc[0] + 1 < Images.WIDTH) {
this.loc[0]++;
if (setMoveRange()) {
repaint();
}else {
this.loc[0]--;
}
}
}
break;
case Canvas.FIRE:
if (this.selected) {
Move();
repaint();
this.selected = false;
if ( win()) {
System.out.println("win");
}
}else {
if (this.MyMap.Grid[this.loc[1]][this.loc[0]] ==
Images.BLANK) {
}else {
this.selected = true;
}
repaint();
}
break;
}
}
private boolean win(){
if ( this.MyMap.Grid[Images.HEIGHT - 2 ][Images.WIDTH - 3 ] == Images.CAOCAO )
return true;
else
return false;
}
private void PrintGrid(String a) {
System.out.println(a);
for (int i = 0; i < Images.HEIGHT; i++) {
for (int j = 0; j < Images.WIDTH; j++) {
System.out.print( (char)this.MyMap.Grid[i][j]);
}
System.out.println("");
}
}
private void Move() {
if (this.MoveArea[0] == -1 || this.MoveArea[1] == -1 ||
this.SelectArea[0] == -1 || this.SelectArea[1] == -1) {
}else {
byte[][] temp = new byte[this.SelectArea[3]][this.SelectArea[2]];
for (int i = 0; i < this.SelectArea[2]; i++) {
for (int j = 0; j < this.SelectArea[3]; j++) {
temp[j][i] =
this.MyMap.Grid[this.SelectArea[1] +j]
[this.SelectArea[0] + i];
}
}
for (int i = 0; i < this.SelectArea[2]; i++) {
for (int j = 0; j < this.SelectArea[3]; j++) {
this.MyMap.Grid[this.MoveArea[1] + j]
[this.MoveArea[0] + i] = temp[j][i];
}
}
for (int i = 0; i < this.SelectArea[3]; i++) {
for (int j = 0; j < this.SelectArea[2]; j++) {
if (
华容道手机拼图游戏(经典)
需积分: 9 46 浏览量
2008-09-23
23:40:43
上传
评论
收藏 268KB RAR 举报
筋斗云王
- 粉丝: 63
- 资源: 10
最新资源
- Screenshot_20240427_031602.jpg
- 网页PDF_2024年04月26日 23-46-14_QQ浏览器网页保存_QQ浏览器转格式(6).docx
- 直接插入排序,冒泡排序,直接选择排序.zip
- 在排序2的基础上,再次对快排进行优化,其次增加快排非递归,归并排序,归并排序非递归版.zip
- 实现了7种排序算法.三种复杂度排序.三种nlogn复杂度排序(堆排序,归并排序,快速排序)一种线性复杂度的排序.zip
- 冒泡排序 直接选择排序 直接插入排序 随机快速排序 归并排序 堆排序.zip
- 课设-内部排序算法比较 包括冒泡排序、直接插入排序、简单选择排序、快速排序、希尔排序、归并排序和堆排序.zip
- Python排序算法.zip
- C语言实现直接插入排序、希尔排序、选择排序、冒泡排序、堆排序、快速排序、归并排序、计数排序,并带图详解.zip
- 常用工具集参考用于图像等数据处理
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈