现在使用JavaME的几乎没有了,我这里还有一些老古董 粘处理 留个纪念
DiamandCanvas.java
package russiandiamand;
import java.util.*;
import javax.microedition.lcdui.*;
import com.siemens.mp.game.*;
public class DiamandCanvas
extends Canvas
implements CommandListener, Runnable
{
private Command newCommand = new Command("重玩", Command.SCREEN, 2);
private Command highScoreCommand = new Command("高分", Command.SCREEN, 2);
private Command aboutCommand = new Command("关于", Command.SCREEN, 2);
final int orgin_x = 4;
final int WIDTH_DIAMAND = 16;
final int HIGHTH_DIAMAND = 20;
final int SIZE_DIAMAND = 4;
int[][] deadArray = new int[WIDTH_DIAMAND][HIGHTH_DIAMAND];
int[][] liveArray;
int[] livePos = new int[]{orgin_x, 0};
int score = 0;
boolean isRunning = true;
boolean isPausing = false;
Random random = new Random(Calendar.getInstance().getTime().getTime());
int currentDiamand = Math.abs(random.nextInt()) % 19;
int nextDiamand = Math.abs(random.nextInt()) % 19;;
//0~0,1~4,5~8,9~12,13~14,15~16,17~18
final int[][][] diamandArray =
new int[][][]{
//**
//**
{{1, 1, 0, 0},{1, 1, 0, 0},{0, 0, 0, 0},{0, 0, 0, 0}},
//***
// *
{{1, 1, 1, 0},{0, 1, 0, 0},{0, 0, 0, 0},{0, 0, 0, 0}},
// *
//**
// *
{{0, 1, 0, 0},{1, 1, 0, 0},{0, 1, 0, 0},{0, 0, 0, 0}},
// *
//***
{{0, 1, 0, 0},{1, 1, 1, 0},{0, 0, 0, 0},{0, 0, 0, 0}},
//*
//**
//*
{{1, 0, 0, 0},{1, 1, 0, 0},{1, 0, 0, 0},{0, 0, 0, 0}},
//**
// *
// *
{{1, 1, 0, 0},{0, 1, 0, 0},{0, 1, 0, 0},{0, 0, 0, 0}},
// *
//***
{{0, 0, 1, 0},{1, 1, 1, 0},{0, 0, 0, 0},{0, 0, 0, 0}},
//*
//*
//**
{{1, 0, 0, 0},{1, 0, 0, 0},{1, 1, 0, 0},{0, 0, 0, 0}},
//***
//*
{{1, 1, 1, 0},{1, 0, 0, 0},{0, 0, 0, 0},{0, 0, 0, 0}},
//**
//*
//*
{{1, 1, 0, 0},{1, 0, 0, 0},{1, 0, 0, 0},{0, 0, 0, 0}},
//***
// *
{{1, 1, 1, 0},{0, 0, 1, 0},{0, 0, 0, 0},{0, 0, 0, 0}},
// *
// *
//**
{{0, 1, 0, 0},{0, 1, 0, 0},{1, 1, 0, 0},{0, 0, 0, 0}},
//*
//***
{{1, 0, 0, 0},{1, 1, 1, 0},{0, 0, 0, 0},{0, 0, 0, 0}},
//**
// **
{{1, 1, 0, 0},{0, 1, 1, 0},{0, 0, 0, 0},{0, 0, 0, 0}},
// *
//**
//*
{{0, 1, 0, 0},{1, 1, 0, 0},{1, 0, 0, 0},{0, 0, 0, 0}},
// **
//**
{{0, 1, 1, 0},{1, 1, 0, 0},{0, 0, 0, 0},{0, 0, 0, 0}},
//*
//**
// *
{{1, 0, 0, 0},{1, 1, 0, 0},{0, 1, 0, 0},{0, 0, 0, 0}},
//****
{{1, 1, 1, 1},{0, 0, 0, 0},{0, 0, 0, 0},{0, 0, 0, 0}},
//*
//*
//*
//*
{{1, 0, 0, 0},{1, 0, 0, 0},{1, 0, 0, 0},{1, 0, 0, 0}},
};
final int leftMagin = 4;
ExtendedImage extImg = new ExtendedImage(Image.createImage(104, 80));
Image img = null;
/** Constructor */
public DiamandCanvas()
{
try
{
jbInit();
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* Component initialization
* @throws java.lang.Exception
*/
private void jbInit() throws Exception
{
// Set up this Displayable to listen to command events
setCommandListener(this);
// add the Exit command
addCommand(new Command("退出", Command.EXIT, 1));
addCommand(newCommand);
addCommand(highScoreCommand);
addCommand(aboutCommand);
img = Image.createImage("russiandiamand/res/diamand.png");
initLive();
Thread thread = new Thread(this);
thread.start();
}
public void run()
{
while (isRunning)
{
if (!isPausing)
{
handleDown();
repaint();
}
try
{
Thread.sleep(333);
}
catch (InterruptedException ex)
{
}
}
};
/**
* Handle command events
* @param command command
* @param displayable displayable
*/
public void commandAction(Command command, Displayable displayable)
{
if (command.getCommandType() == Command.EXIT)
{
if (score > Integer.parseInt(DiamandMIDlet.highScoreForm.getScore()))
{
DiamandMIDlet.highScoreForm.setScore("" + score);
}
DiamandMIDlet.quitApp();
}
else if (command == newCommand)
{
if (score > Integer.parseInt(DiamandMIDlet.highScoreForm.getScore()))
{
DiamandMIDlet.highScoreForm.setScore("" + score);
}
DiamandMIDlet.display.setCurrent(DiamandMIDlet.highScoreForm);
}
else if (command == highScoreCommand)
{
if (score > Integer.parseInt(DiamandMIDlet.highScoreForm.getScore()))
{
DiamandMIDlet.highScoreForm.setScore("" + score);
}
DiamandMIDlet.display.setCurrent(DiamandMIDlet.highScoreForm);
}
else if (command == aboutCommand)
{
Alert messageAlert = new Alert("消息", "版权所有,陈轶(2004), 版本: 0.1, 兼容西门子手机.", null, AlertType.INFO);
DiamandMIDlet.display.setCurrent(messageAlert, this);
}
}
protected void keyPressed(int keyCode)
{
if (!isRunning)
{
return;
}
int key = getGameAction(keyCode);
switch(key)
{
case Canvas.KEY_NUM2 :
case Canvas.UP:
handleUp();
updateUI();
break;
case Canvas.KEY_NUM4 :
case Canvas.LEFT:
handleLeft();
updateUI();
break;
case Canvas.KEY_NUM6 :
case Canvas.RIGHT:
handleRight();
updateUI();
break;
case Canvas.KEY_NUM8 :
case Canvas.DOWN:
handleDown();
updateUI();
break;
}
}
/**
* Required paint implementation
* @param g Graphics
*/
protected void paint(Graphics g)
{
updateUI();
}
public void handleUp()
{
if (canChange())
{
changeShape();
}
}
public void handleDown()
{
if (canDown())
{
System.out.println("down");
down();
}
else
{
System.out.println("!canDown");
makeLiveToDeath();
removeLine();
if (canMakeNewLive())
{
initLive();
}
else
{
isRunning = false;
}
}
}
public void handleLeft()
{
if (canLeft())
{
left();
}
}
public void handleRight()
{
if (canRight())
{
right();
}
}
public void makeLiveToDeath()
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (liveArray[i][j] == 1)
{
deadArray[livePos[0] + i][livePos[1] + j] = 1;
}
}
}
}
public void removeLine()
{
int removedLine = 0;
for (int i = 0; i < HIGHTH_DIAMAND; i++)
{
boolean isLineFill = true;
for (int j = 0; j < WIDTH_DIAMAND; j++)
{
if (deadArray[j][i] == 0)
{
isLineFill = false;
}
}
if(isLineFill)
{
removedLine ++;
for (int j = 0; j < WIDTH_DIAMAND; j++)
{
deadArray[j][0] = 0;
}
for (int j = i; j >= 1; j--)
{
for (int k = 0; k < WIDTH_DIAMAND; k++)
{
deadArray[k][j] = deadArray[k][j - 1];
}
}
}
}
if (removedLine != 0)
{
score += 10 * (2 ^ (removedLine - 1));
}
System.out.println("" + removedLine);
}
public void initLive()
{
livePos = new int[]{orgin_x, 0};
currentDiamand = nextDiamand;
nextDiamand = Math.abs(random.nextInt()) % 19;
liveArray = diamandArray[currentDiamand];
}
private boolean canMakeNewLive()
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (diamandArray[nextDiamand][i][j] == 1)
{
if (deadArray[orgin_x + i][j] == 1)
{
return false;
}
}
}
}
return true;
}
//0~0,1~4,5~8,9~12,13~14,15~16,17~18
private int getNextShapeId(int shapeId)
{
switch (shapeId)
{
case 0:
return 0;
case 1:
case 2:
case 3:
case 5:
case 6:
case 7:
case 9:
case 10:
case 11:
case 13:
case 15:
case 17:
return shapeId + 1;
case 4:
case 8:
case 12:
return shapeId - 3;
case 14:
case 16:
case 18:
return shapeId - 1;
default:
return 0;
}
}
private void changeShape()
{
currentDiamand = getNextShapeId(currentDiamand);
liveArray = diamandArray[currentDiamand];
}
private boolean canChange()
{
int[][] tmpArray = diamandArray[getNextShapeId(currentDiamand)];
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (tmpArray[i][j] == 1)
{
if (livePos[1] + j > HIGHTH_DIAMAND - 1)
{
return false;
}
if (livePos[1] + j < 0)
{
return false;
}
if (livePos[0] + i > WIDTH_DIAMAND - 1)
{
return false;
}
if (livePos[0] + i < 0)
{
return false;
}
if (deadArray[livePos[0] + i][livePos[1] + j] == 1)
{
return false;
}
}
}
}
return true;
}
private boolean canDown()
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (liveArray[i][j] == 1)
{
if (livePos[1] + j + 1 > (HIGHTH_DIAMAND - 1))
{
return false;
}
if (deadArray[livePos[0] + i][livePos[1] + j + 1] == 1)
{
return false;
}
}
}
}
return true;
}
private boolean canLeft()
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (liveArray[i][j] == 1)
{
if (livePos[0] + i - 1 < 0)
{
return false;
}
if (deadArray[livePos[0] + i - 1][livePos[1] + j] == 1)
{
return false;
}
}
}
}
return true;
}
private boolean canRight()
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (liveArray[i][j] == 1)
{
if (livePos[0] + i + 1 >= HIGHTH_DIAMAND)
{
return false;
}
if (deadArray[livePos[0] + i + 1][livePos[1] + j] == 1)
{
return false;
}
}
}
}
return true;
}
public void down()
{
livePos[1] = livePos[1] + 1;
}
public void left()
{
livePos[0] = livePos[0] - 1;
}
public void right()
{
livePos[0] = livePos[0] + 1;
}
public void updateUI()
{
extImg.clear((byte)0);
extImg.getImage().getGraphics().setColor(1);
extImg.getImage().getGraphics().drawLine(leftMagin, 0, leftMagin, 80);
// extImg.getImage().getGraphics().setColor(0x00FF00);
// extImg.getImage().getGraphics().setStrokeStyle(Graphics.DOTTED);
// for (int i = 0; i < WIDTH_DIAMAND; i++)
// {
// extImg.getImage().getGraphics().drawLine(leftMagin + SIZE_DIAMAND*i, 0, leftMagin + SIZE_DIAMAND*i, 80);
// }
// extImg.getImage().getGraphics().setStrokeStyle(Graphics.SOLID);
// extImg.getImage().getGraphics().setColor(1);
extImg.getImage().getGraphics().drawLine(leftMagin + 64, 0, leftMagin + 64, 80);
extImg.getImage().getGraphics().drawString("Score:", leftMagin + 68, 0, Graphics.TOP|Graphics.LEFT);
extImg.getImage().getGraphics().drawString("" + score, leftMagin + 68, 20, Graphics.TOP|Graphics.LEFT);
if (!isRunning)
{
extImg.getImage().getGraphics().drawString("GameOver", 30, 40, Graphics.TOP|Graphics.LEFT);
}
else
{
for (int i = 0; i < WIDTH_DIAMAND; i++)
{
for (int j = 0; j < HIGHTH_DIAMAND; j++)
{
if (deadArray[i][j] == 1)
{
extImg.getImage().getGraphics().drawImage(img, leftMagin + SIZE_DIAMAND * i, SIZE_DIAMAND * j, Graphics.LEFT | Graphics.TOP);
}
}
}
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (liveArray[i][j] == 1)
{
extImg.getImage().getGraphics().drawImage(img, leftMagin + (livePos[0] + i) * SIZE_DIAMAND,
(livePos[1] + j) * SIZE_DIAMAND, Graphics.LEFT | Graphics.TOP);
}
if (diamandArray[nextDiamand][i][j] == 1)
{
extImg.getImage().getGraphics().drawImage(img, leftMagin + 64 + (1 + i) * SIZE_DIAMAND,
40 + j * SIZE_DIAMAND, Graphics.LEFT | Graphics.TOP);
}
}
}
}
extImg.blitToScreen(0,0);
}
}
HighScoreForm.java
package russiandiamand;
import javax.microedition.lcdui.*;
public class HighScoreForm
extends Form
implements CommandListener
{
StringItem highScoreStringItem;
private Command okCommand = new Command("确认", Command.SCREEN, 1);
/** Constructor */
public HighScoreForm()
{
super("高分");
try
{
jbInit();
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* Component initialization
* @throws java.lang.Exception
*/
private void jbInit() throws Exception
{
// Set up this Displayable to listen to command events
highScoreStringItem = new StringItem("", "");
highScoreStringItem.setLabel("高分");
highScoreStringItem.setText("0");
setCommandListener(this);
// add the Exit command
addCommand(okCommand);
this.append(highScoreStringItem);
}
public void setScore(String score)
{
highScoreStringItem.setText(score);
}
public String getScore()
{
return highScoreStringItem.getText();
}
/**
* Handle command events
* @param command command
* @param displayable displayable
*/
public void commandAction(Command command, Displayable displayable)
{
if (command == okCommand)
{
DiamandMIDlet.display.setCurrent(DiamandMIDlet.diamandCanvas);
}
}
}
DiamandMIDlet.java
package russiandiamand;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
public class DiamandMIDlet
extends MIDlet
{
private static DiamandMIDlet instance;
public static DiamandCanvas diamandCanvas = new DiamandCanvas();
public static HighScoreForm highScoreForm = new HighScoreForm();
public static Display display;
// public static String highScore = "0";
final String recordStoreName = "DiamandScore";
/** Constructor */
public DiamandMIDlet()
{
instance = this;
display = Display.getDisplay(this);
}
/** Main method */
public void startApp()
{
try
{
RecordStore rs = RecordStore.openRecordStore(recordStoreName, false);
int recordCount = rs.getNumRecords();
String highScore = new String(rs.getRecord(1));
highScoreForm.setScore(highScore);
rs.closeRecordStore();
}
catch (RecordStoreException ex)
{
System.out.println(ex);
}
Display.getDisplay(this).setCurrent(diamandCanvas);
}
/** Handle pausing the MIDlet */
public void pauseApp()
{
}
/**
* Handle destroying the MIDlet
* @param unconditional unconditional
*/
public void destroyApp(boolean unconditional)
{
try
{
RecordStore.deleteRecordStore(recordStoreName);
}
catch (RecordStoreException ex)
{
System.out.println(ex);
}
try
{
RecordStore rs = RecordStore.openRecordStore(recordStoreName, true);
String highScore = highScoreForm.getScore();
rs.addRecord(highScore.getBytes(), 0, highScore.length());
rs.closeRecordStore();
}
catch (RecordStoreException ex)
{
System.out.println(ex);
}
}
/** Quit the MIDlet */
public static void quitApp()
{
instance.destroyApp(true);
instance.notifyDestroyed();
instance = null;
}
}
网友评论