import com.nttdocomo.ui.Canvas; |
import com.nttdocomo.ui.Display; |
import com.nttdocomo.ui.Graphics; |
import com.nttdocomo.ui.IApplication; |
|
public class TemplateAppli extends IApplication 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]; |
|
Canvas canvas; |
|
public void start() { |
canvas = new Canvas() { |
public void paint(Graphics g) { |
// このメソッドは利用しないので実装する必要はない |
} |
}; |
Display.setCurrent(canvas); |
thread = new Thread(this); |
thread.start(); |
} |
|
public void resume() { |
// このイベントは利用しないので実装する必要はない |
} |
|
public void run() { |
int x = 0; |
int y = 0; |
int count = 0; |
initField(); |
int[][] current = new int[pattern[0].length][pattern[0].length]; |
copy(pattern[0], next); |
Graphics g = canvas.getGraphics(); |
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; |
} |
} |
} |
|
g.lock(); |
|
// メインフィールドを描画 |
drawBlocks(g); |
|
g.unlock(true); |
|
if (count == 0) { |
sleep(150); |
} else { |
sleep(50); |
} |
count++; |
} |
} |
} |
|
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(Graphics.getColorOfName(Graphics.GRAY)); |
g.fillRect(j * blockSize, i * blockSize, blockSize, blockSize); |
} else if (field[i][j] == 1) { |
g.setColor(Graphics.getColorOfName(Graphics.RED)); |
g.fillRect(j * blockSize, i * blockSize, blockSize, blockSize); |
} else { |
g.setColor(Graphics.getColorOfName(Graphics.BLACK)); |
g.fillRect(j * blockSize, i * blockSize, blockSize, blockSize); |
} |
} |
} |
} |
|
void sleep(long millis) { |
try { |
Thread.sleep(millis); |
} catch (InterruptedException e) { |
// 例外は発生しない |
} |
} |
|
} |