import javax.microedition.lcdui.Canvas; |
import javax.microedition.lcdui.Display; |
import javax.microedition.lcdui.Graphics; |
import javax.microedition.lcdui.Image; |
import javax.microedition.midlet.MIDlet; |
|
public class TemplateMIDlet extends MIDlet implements Runnable { |
|
int FIELD_WIDTH = 12; |
int FIELD_HEIGHT = 22; |
|
int[][][] pattern = { |
{ |
{0, 1, 0}, |
{0, 1, 0}, |
{0, 1, 0} |
}, { |
{0, 1, 0}, |
{0, 1, 1}, |
{0, 0, 0} |
} |
}; |
|
int mode = 0; // ゲーム内の状態を表すモード(0:落下開始、1:落下中、2:着地) |
Thread thread; |
int[][] field = new int[FIELD_HEIGHT][FIELD_WIDTH]; |
int[][] next = new int[pattern[0].length][pattern[0].length]; |
|
Image offi; |
Canvas canvas; |
|
int COLOR_GRAY = 0xC0C0C0; |
int COLOR_RED = 0xFF0000; |
int COLOR_BLACK = 0x000000; |
|
public TemplateMIDlet() { |
canvas = new Canvas() { |
public void paint(Graphics g) { |
g.drawImage(offi, 0, 0, Graphics.TOP | Graphics.LEFT); |
} |
}; |
offi = Image.createImage(canvas.getWidth(), canvas.getHeight()); |
Display.getDisplay(this).setCurrent(canvas); |
} |
|
protected void startApp() { |
if (thread == null) { |
thread = new Thread(this); |
thread.start(); |
} |
} |
|
protected void pauseApp() { |
// このイベントは利用しないので実装する必要はない |
} |
|
protected void destroyApp(boolean unconditional) { |
// このイベントは利用しないので実装する必要はない |
} |
|
public void run() { |
int x = 0; |
int y = 0; |
int count = 0; |
Graphics g = offi.getGraphics(); |
initField(); |
int[][] current = new int[pattern[0].length][pattern[0].length]; |
copy(pattern[0], next); |
while (true) { |
if (mode == 0) { |
copy(next, current); |
copy(pattern[0], next); |
x = FIELD_WIDTH / 2 - (int)Math.sqrt(pattern[0].length); |
y = 0; |
mode = 1; |
count = 0; |
} else if (mode == 1) { |
if (count != 0) { |
if (count % 20 == 0) { |
y++; |
} |
} |
|
for (int i = 0; i < FIELD_HEIGHT; i++) { |
for (int j = 0; j < FIELD_WIDTH; j++) { |
if (field[i][j] == 1) { |
field[i][j] = 0; |
} |
} |
} |
|
for (int i = 0; i < current.length; i++) { |
for (int j = 0; j < current[i].length; j++) { |
if (current[i][j] == 1) { |
field[y + i][x + j] = 1; |
} |
} |
} |
|
// メインフィールドを描画 |
drawBlocks(g); |
|
if (count == 0) { |
sleep(150); |
} else { |
sleep(50); |
} |
count++; |
} |
canvas.repaint(); |
canvas.serviceRepaints(); |
|
} |
} |
|
void copy(int[][] src, int[][] dest) { |
for (int i = 0; i < src.length; i++) { |
System.arraycopy(src[i], 0, dest[i], 0, src[i].length); |
} |
} |
|
void initField() { |
for (int i = 0; i < FIELD_HEIGHT; i++) { |
for (int j = 0; j < FIELD_WIDTH; j++) { |
if (j == 0 || j == FIELD_WIDTH - 1) { |
field[i][j] = -1; |
} else if (i == FIELD_HEIGHT - 1) { |
field[i][j] = -1; |
} else { |
field[i][j] = 0; |
} |
} |
} |
for (int i = 1; i < FIELD_WIDTH - 1; i++) { |
field[FIELD_HEIGHT - 1][i] = -1; |
} |
for (int i = 0; i < FIELD_HEIGHT; i++) { |
field[i][0] = -1; |
field[i][FIELD_WIDTH - 1] = -1; |
} |
} |
|
void drawBlocks(Graphics g) { |
int blockSize = canvas.getHeight() / (FIELD_HEIGHT + 2); |
for (int i = 0; i < FIELD_HEIGHT; i++) { |
for (int j = 0; j < FIELD_WIDTH; j++) { |
if (field[i][j] == -1) { |
g.setColor(COLOR_GRAY); |
g.fillRect(j * blockSize, i * blockSize, blockSize, blockSize); |
} else if (field[i][j] == 1) { |
g.setColor(COLOR_RED); |
g.fillRect(j * blockSize, i * blockSize, blockSize, blockSize); |
} else { |
g.setColor(COLOR_BLACK); |
g.fillRect(j * blockSize, i * blockSize, blockSize, blockSize); |
} |
} |
} |
} |
|
void sleep(long millis) { |
try { |
Thread.sleep(millis); |
} catch (InterruptedException e) { |
// 例外は発生しない |
} |
} |
|
} |