import com.nttdocomo.ui.IApplication;
import com.nttdocomo.ui.Display;
import com.nttdocomo.ui.Graphics;
import com.nttdocomo.ui.Canvas;
import com.nttdocomo.ui.MediaManager;
import com.nttdocomo.ui.MediaImage;
import com.nttdocomo.ui.Image;
import com.nttdocomo.io.ConnectionException;
import com.nttdocomo.ui.UIException;
import java.util.Random;
public class BlockMain extends Canvas implements Runnable{
private int game;
private Random rand;
private int mapno;
private int delay;
private int delayCount;
private int delayLevelup;
private int bx; /* Ballの位置と角度 */
private int by;
private int bd;
private int newbx;
private int newby;
private int ballZan;
private int gameCount;
private int platex;
private int newPlatex;
private boolean plateMoveFlag;
private Image end;
private Image out;
private Image clear;
private Image perfect;
private Thread thread;
private int[][][] map ={
{{0,0,0,0,0,0,0,0}, /* 48 */
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{0,0,0,0,0,0,0,0}},
{{0,0,0,1,1,0,0,0}, /* 40 */
{0,0,1,1,1,1,0,0},
{0,1,1,1,1,1,1,0},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{0,1,1,1,1,1,1,0},
{0,0,1,1,1,1,0,0},
{0,0,0,1,1,0,0,0}},
{{0,0,0,0,0,0,0,0}, /* 48 */
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{2,2,2,2,2,2,2,2},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{2,2,2,2,2,2,2,2},
{0,0,0,0,0,0,0,0}},
{{1,1,1,3,3,1,1,1}, /* 48 */
{1,1,1,3,3,1,1,1},
{1,1,1,3,3,1,1,1},
{1,1,1,3,3,1,1,1},
{1,1,1,3,3,1,1,1},
{1,1,1,3,3,1,1,1},
{1,1,1,3,3,1,1,1},
{1,1,1,3,3,1,1,1}},
{{1,0,1,0,1,0,1,0}, /* 32 */
{0,1,0,1,0,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0,3,3,3}},
{{0,3,3,1,3,3,3,0}, /* 11 */
{0,3,0,0,0,0,3,0},
{0,3,0,1,1,0,3,0},
{0,1,0,1,1,0,1,0},
{0,3,0,1,1,0,3,0},
{0,3,0,1,1,0,3,0},
{0,3,0,0,0,0,3,0},
{0,3,3,3,3,3,3,0}},
{{1,1,1,1,1,1,1,1}, /* 57 */
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{3,3,3,3,1,3,3,3}},
{{3,0,3,0,0,3,0,3}, /* 4 */
{3,1,3,0,0,3,1,3},
{3,3,3,0,0,3,3,3},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{3,0,3,0,0,3,0,3},
{3,1,3,0,0,3,1,3},
{3,3,3,0,0,3,3,3}},
{{0,1,1,1,1,1,1,0}, /* 18 */
{0,3,3,3,3,3,3,0},
{0,1,0,1,1,0,1,0},
{0,3,3,3,3,3,3,0},
{0,1,1,0,0,1,1,0},
{0,3,3,3,3,3,3,0},
{0,1,0,1,1,0,1,0},
{0,3,3,3,3,3,3,0}},
};
private int blockno[] = {48,40,48,48,32,34,16,24,48,24,18,11,57,4,18};
private int blockZan;
BlockMain(){
MediaImage mi;
mi = MediaManager.getImage("resource:///gif/end.gif");
try{
mi.use();
}catch(ConnectionException e){
}catch(UIException e){
}
end = mi.getImage();
mi = MediaManager.getImage("resource:///gif/out.gif");
try{
mi.use();
}catch(ConnectionException e){
}catch(UIException e){
}
out = mi.getImage();
mi = MediaManager.getImage("resource:///gif/clear.gif");
try{
mi.use();
}catch(ConnectionException e){
}catch(UIException e){
}
clear = mi.getImage();
mi = MediaManager.getImage("resource:///gif/perfect.gif");
try{
mi.use();
}catch(ConnectionException e){
}catch(UIException e){
}
perfect = mi.getImage();
setSoftLabel(SOFT_KEY_1, "Start");
setSoftLabel(SOFT_KEY_2, "Exit");
/* 乱数初期化 */
rand = new Random();
game = 0;
thread = new Thread(this);
thread.start();
}
public void run(){
while(true){
try{
thread.sleep(8);
}catch (InterruptedException e){
break;
}
repaint();
}
}
private void drawMap(Graphics g){
g.lock();
for (int i = 0 ; i < 8 ; i++){
for (int j = 0 ; j < 8 ; j++){
if (map[mapno][i][j] == 1){
if ((i == 0) || (i == 1)){
g.setColor(Graphics.getColorOfName(Graphics.RED));
}else if ((i == 2) || (i == 3)){
g.setColor(Graphics.getColorOfName(Graphics.LIME));
}else if ((i == 4) || (i == 5)){
g.setColor(Graphics.getColorOfName(Graphics.BLUE));
}else if ((i == 6) || (i == 7)){
g.setColor(Graphics.getColorOfName(Graphics.YELLOW));
}
g.fillRect(2 + j * 12, 20 + i * 5, 11, 4);
}else if (map[mapno][i][j] == 2){
g.setColor(Graphics.getColorOfName(Graphics.GRAY));
g.fillRect(2 + j * 12, 20 + i * 5, 11, 4);
}else if (map[mapno][i][j] == 3){
g.setColor(Graphics.getColorOfName(Graphics.SILVER));
g.fillRect(2 + j * 12, 20 + i * 5, 11, 4);
}
}
}
g.unlock(true);
}
private void drawBallZan(Graphics g){
g.lock();
g.setColor(Graphics.getColorOfName(Graphics.WHITE));
for (int i = 0 ; i < ballZan ; i++){
g.fillRect(105 + i * 6 + 1, 20, 2, 4);
g.fillRect(105 + i * 6, 21, 4, 2);
}
g.drawLine(105, 32, 108, 36);
g.drawLine(105, 36, 108, 32);
if (ballZan == 2){
g.drawLine(112, 32, 114, 32);
g.drawLine(114, 32, 114, 34);
g.drawLine(114, 34, 112, 34);
g.drawLine(112, 34, 112, 36);
g.drawLine(112, 36, 114, 36);
}else if (ballZan == 1){
g.drawLine(113, 32, 114, 32);
g.drawLine(114, 32, 114, 36);
}else if (ballZan == 0){
g.drawRect(112, 32, 3, 5);
}
g.unlock(true);
}
public void paint(Graphics g){
if (game == 0){
mapno = 0;
blockZan = blockno[mapno];
ballZan = 2;
game = 1;
}else if (game == 1){
g.lock();
g.setColor(Graphics.getColorOfName(Graphics.BLACK));
g.fillRect(0, 0, 120, 120);
g.setColor(Graphics.getColorOfName(Graphics.WHITE));
g.fillRect(0, 0, 100, 120);
g.setColor(Graphics.getColorOfName(Graphics.BLACK));
g.fillRect(2, 2, 96, 118);
g.unlock(true);
drawMap(g);
drawBallZan(g);
bx = 740;
by = 600;
bd = 225;
newbx = 740;
newby = 600;
platex = 22;
newPlatex = 22;
plateMoveFlag = true;
delay = 6;
delayCount = 0;
delayLevelup = 0;
game = 2;
}else if (game == 2){
/* プレート表示 */
if (plateMoveFlag){
g.lock();
g.setColor(Graphics.getColorOfName(Graphics.BLACK));
g.fillRect(platex, 110, 16, 4);
g.setColor(Graphics.getColorOfName(Graphics.WHITE));
g.fillRect(newPlatex, 110, 16, 4);
g.unlock(true);
platex = newPlatex;
plateMoveFlag = false;
}
if (delayCount >= delay){
delayCount = 0;
if (checkBlock(g)){
moveBall();
}else{
delayLevelup++;
if (delayLevelup >= 8){
if (delay > 1){
delay --;
}
delayLevelup = 0;
}
}
drawBall(g);
}else{
delayCount++;
}
if (blockZan == 0){
gameCount = 0;
game = 3;
}
}else if (game == 3){
if (gameCount == 0){
gameCount++;
mapno++;
if (mapno == 15){
game = 6; /* All Clear */
}else{
g.lock();
g.drawImage(clear, 0, 30);
g.unlock(true);
}
}else if (gameCount > 100){
blockZan = blockno[mapno];
game = 1;
}else{
gameCount++;
}
}else if (game == 4){
if (gameCount == 0){
gameCount++;
ballZan --;
if (ballZan < 0){
game = 5;
}else{
g.lock();
g.drawImage(out, 0, 30);
g.unlock(true);
}
}else if (gameCount > 100){
game = 1;
}else{
gameCount++;
}
}else if (game == 5){
/* GAME OVER */
g.lock();
g.drawImage(end, 1, 30);
g.unlock(true);
/* Startボタン待ち */
}else if (game == 6){
/* All Clear */
g.lock();
g.drawImage(perfect, 1, 30);
g.unlock(true);
/* Startボタン待ち */
}
}
private void blockErase(Graphics g, int ex, int ey){
if (map[mapno][ey][ex] == 1){
map[mapno][ey][ex] = 0;
g.lock();
g.setColor(Graphics.getColorOfName(Graphics.BLACK));
g.fillRect(ex * 12 + 2, ey * 5 + 20, 11, 4);
g.unlock(true);
blockZan--;
}if (map[mapno][ey][ex] == 2){
map[mapno][ey][ex] = 1;
g.lock();
if ((ey == 0) || (ey == 1)){
g.setColor(Graphics.getColorOfName(Graphics.RED));
}else if ((ey == 2) || (ey == 3)){
g.setColor(Graphics.getColorOfName(Graphics.LIME));
}else if ((ey == 4) || (ey == 5)){
g.setColor(Graphics.getColorOfName(Graphics.BLUE));
}else if ((ey == 6) || (ey == 7)){
g.setColor(Graphics.getColorOfName(Graphics.YELLOW));
}
g.fillRect(ex * 12 + 2, ey * 5 + 20, 11, 4);
g.unlock(true);
}
}
private boolean checkBlock(Graphics g){
int blockx;
int blocky;
int x = bx / 10;
int y = by / 10;
/* ブロックの下から当たったかどうか */
if ((y == 25) || (y == 30) || (y == 35) || (y == 40)
|| (y == 45) || (y == 50) || (y == 55) || (y == 60)){
if ((bd == 30) || (bd == 45) || (bd == 60) || (bd == 120) || (bd == 135) || (bd == 150)){
blocky = (y - 25) / 5;
blockx = (x - 2) / 12;
if (map[mapno][blocky][blockx] != 0){
boundBall(2);
blockErase(g, blockx, blocky);
return false;
}
blockx = (x + 1) / 12;
if (map[mapno][blocky][blockx] != 0){
boundBall(2);
blockErase(g, blockx, blocky);
return false;
}
/* 対角線上での衝突 */
if ((bd == 30) || (bd == 45) || (bd == 60)){
if ((x == 10) || (x == 22) || (x == 34) || (x == 46)
|| (x == 58) || (x == 70) || (x == 82)){
blockx = (x - 10) / 12 + 1;
if (map[mapno][blocky][blockx] != 0){
bd = 225;
blockErase(g, blockx, blocky);
return false;
}
}
}else{
if ((x == 14) || (x == 26) || (x == 38) || (x == 50)
|| (x == 62) || (x == 74) || (x == 86)){
blockx = (x - 14) / 12;
if (map[mapno][blocky][blockx] != 0){
bd = 315;
blockErase(g, blockx, blocky);
return false;
}
}
}
}
}
/* ブロックの上から当たったかどうか */
if ((y == 16) || (y == 21) || (y == 26) || (y == 31)
|| (y == 36) || (y == 41) || (y == 46) || (y == 51)){
if ((bd == 210) || (bd == 225) || (bd == 240) || (bd == 300)
|| (bd == 315) || (bd == 330)){
blocky = (y - 16) / 5;
blockx = (x - 2) / 12;
if (map[mapno][blocky][blockx] != 0){
boundBall(4);
blockErase(g, blockx, blocky);
return false;
}
blockx = (x + 1) / 12;
if (map[mapno][blocky][blockx] != 0){
boundBall(4);
blockErase(g, blockx, blocky);
return false;
}
/* 対角線上での衝突 */
if ((bd == 300) || (bd == 315) || (bd == 330)){
if ((x == 10) || (x == 22) || (x == 34) || (x == 46)
|| (x == 58) || (x == 70) || (x == 82)){
blockx = (x - 10) / 12 + 1;
if (map[mapno][blocky][blockx] != 0){
bd = 45;
blockErase(g, blockx, blocky);
return false;
}
}
}else{
if ((x == 14) || (x == 26) || (x == 38) || (x == 50)
|| (x == 62) || (x == 74) || (x == 86)){
blockx = (x - 14) / 12;
if (map[mapno][blocky][blockx] != 0){
bd = 135;
blockErase(g, blockx, blocky);
return false;
}
}
}
}
}
/* ブロックの左から当たったかどうか */
if ((x == 14) || (x == 26) || (x == 38) || (x == 50)
|| (x == 62) || (x == 74) || (x == 86)){
if ((bd == 120) || (bd == 135) || (bd == 150) || (bd == 210)
|| (bd == 225) || (bd == 240)){
blockx = (x - 14) / 12;
if ((y >= 20) && (y <= 59)){
blocky = (y - 20) / 5;
if (map[mapno][blocky][blockx] != 0){
boundBall(3);
blockErase(g, blockx, blocky);
return false;
}
}
if ((y >= 17) && (y <= 56)){
blocky = (y - 17) / 5;
if (map[mapno][blocky][blockx] != 0){
boundBall(3);
blockErase(g, blockx, blocky);
return false;
}
}
}
}
/* ブロックの右から当たったかどうか */
if ((x == 10) || (x == 22) || (x == 34) || (x == 46)
|| (x == 58) || (x == 70) || (x == 82)){
if ((bd == 30) || (bd == 45) || (bd == 60) || (bd == 300)
|| (bd == 315) || (bd == 330)){
blockx = (x - 10) / 12 + 1;
if ((y >= 20) && (y <= 59)){
blocky = (y - 20) / 5;
if (map[mapno][blocky][blockx] != 0){
boundBall(1);
blockErase(g, blockx, blocky);
return false;
}
}
if ((y >= 17) && (y <= 56)){
blocky = (y - 17) / 5;
if (map[mapno][blocky][blockx] != 0){
boundBall(1);
blockErase(g, blockx, blocky);
return false;
}
}
}
}
/* 右の壁にあたったかどうか */
if (x == 94){
boundBall(1);
}
/* 上の壁にあたったかどうか */
if (y == 2){
boundBall(2);
}
/* 左の壁にあたったかどうか */
if (x == 2){
boundBall(3);
}
/* プレートにあたったかどうか */
if (y == 106){
if ((bd == 210) || (bd == 225) || (bd == 240) || (bd == 300)
|| (bd == 315) || (bd == 330)){
int tmp = checkBoundPlate();
boundBallOnPlate(tmp);
return false;
}
}
return true;
}
/* ボールとプレートの衝突判定 */
private int checkBoundPlate(){
int tmpx = bx / 10;
if ((tmpx + 1 >= platex)&&(tmpx + 1 <= platex + 1)){
return 1;
}else if ((tmpx + 1 >= platex + 2)&&(tmpx + 1 <= platex + 3)){
return 2;
}else if ((tmpx + 1 >= platex + 4)&&(tmpx + 1 <= platex + 11)){
return 3;
}else if ((tmpx + 1 >= platex + 12)&&(tmpx + 1 <= platex + 13)){
return 4;
}else if ((tmpx + 1 >= platex + 14)&&(tmpx + 1 <= platex + 15)){
return 5;
}
gameCount = 0;
game = 4;
return 0; /* アウト */
}
/* プレート上でのボールのはね方 */
private void boundBallOnPlate(int w){
if (bd == 210){
if (w == 1){
bd = randamDirect(2);
}else if (w == 2){
bd = 135;
}else if (w == 3){
bd = 150;
}else if (w == 4){
bd = 30;
}else if (w == 5){
bd = 30;
}
}else if (bd == 225){
if (w == 1){
bd = randamDirect(2);
}else if (w == 2){
bd = 120;
}else if (w == 3){
bd = 135;
}else if (w == 4){
bd = 45;
}else if (w == 5){
bd = 30;
}
}else if (bd == 240){
if (w == 1){
bd = randamDirect(2);
}else if (w == 2){
bd = 120;
}else if (w == 3){
bd = 120;
}else if (w == 4){
bd = 60;
}else if (w == 5){
bd = 45;
}
}else if (bd == 300){
if (w == 1){
bd = 135;
}else if (w == 2){
bd = 120;
}else if (w == 3){
bd = 60;
}else if (w == 4){
bd = 60;
}else if (w == 5){
bd = randamDirect(1);
}
}else if (bd == 315){
if (w == 1){
bd = 150;
}else if (w == 2){
bd = 135;
}else if (w == 3){
bd = 45;
}else if (w == 4){
bd = 60;
}else if (w == 5){
bd = randamDirect(1);
}
}else if (bd == 330){
if (w == 1){
bd = 150;
}else if (w == 2){
bd = 150;
}else if (w == 3){
bd = 30;
}else if (w == 4){
bd = 45;
}else if (w == 5){
bd = randamDirect(1);
}
}
}
private int randamDirect(int type){
int tmp = Math.abs(rand.nextInt()) % 3;
int newd = 60;
if (type == 1){
if (tmp == 0){
newd = 60;
}else if (tmp == 1){
newd = 45;
}else{
newd = 30;
}
}else{
if (tmp == 0){
newd = 120;
}else if (tmp == 1){
newd = 135;
}else{
newd = 150;
}
}
return newd;
}
private void boundBall(int d){
if (d == 1){
if (bd == 30){
bd = 150;
}else if (bd == 45){
bd = 135;
}else if (bd == 60){
bd = 120;
}else if (bd == 300){
bd = 240;
}else if (bd == 315){
bd = 225;
}else if (bd == 330){
bd = 210;
}
}else if (d == 2){
if (bd == 30){
bd = 330;
}else if (bd == 45){
bd = 315;
}else if (bd == 60){
bd = 300;
}else if (bd == 120){
bd = 240;
}else if (bd == 135){
bd = 225;
}else if (bd == 150){
bd = 210;
}
}else if (d == 3){
if (bd == 120){
bd = 60;
}else if (bd == 135){
bd = 45;
}else if (bd == 150){
bd = 30;
}else if (bd == 210){
bd = 330;
}else if (bd == 225){
bd = 315;
}else if (bd == 240){
bd = 300;
}
}else if (d == 4){
if (bd == 210){
bd = 150;
}else if (bd == 225){
bd = 135;
}else if (bd == 240){
bd = 120;
}else if (bd == 300){
bd = 60;
}else if (bd == 315){
bd = 45;
}else if (bd == 330){
bd = 30;
}
}
}
/* Ballを描く */
private void drawBall(Graphics g){
g.lock();
g.setColor(Graphics.getColorOfName(Graphics.BLACK));
g.fillRect(bx / 10, by / 10, 4, 4);
g.setColor(Graphics.getColorOfName(Graphics.WHITE));
g.fillRect(newbx / 10 + 1, newby / 10, 2, 4);
g.fillRect(newbx / 10, newby / 10 + 1, 4, 2);
g.unlock(true);
bx = newbx;
by = newby;
}
/* Ballを動かす */
private void moveBall(){
/* (2,2) - (97,117) */
if (bd == 45){
newbx = bx + 10;
newby = by - 10;
}else if (bd == 135){
newbx = bx - 10;
newby = by - 10;
}else if (bd == 225){
newbx = bx - 10;
newby = by + 10;
}else if (bd == 315){
newbx = bx + 10;
newby = by + 10;
}else if (bd == 30){
newbx = bx + 10;
newby = by - 5;
}else if (bd == 150){
newbx = bx - 10;
newby = by - 5;
}else if (bd == 210){
newbx = bx - 10;
newby = by + 5;
}else if (bd == 330){
newbx = bx + 10;
newby = by + 5;
}else if (bd == 60){
newbx = bx + 5;
newby = by - 10;
}else if (bd == 120){
newbx = bx - 5;
newby = by - 10;
}else if (bd == 240){
newbx = bx - 5;
newby = by + 10;
}else if (bd == 300){
newbx = bx + 5;
newby = by + 10;
}
}
/* プレートを動かす */
private void movePlate(int direct){
if (plateMoveFlag){
return;
}
if (direct == 1){
if (platex <= 2){
return;
}
newPlatex = platex - 4;
}else if (direct == 2){
if (platex >= 82){
return;
}
newPlatex = platex + 4;
}
plateMoveFlag = true;
}
public void processEvent(int type, int param){
if(type == Display.KEY_PRESSED_EVENT){
if (param == Display.KEY_SOFT1){
game = 0;
}else if (param == Display.KEY_SOFT2){
IApplication.getCurrentApp().terminate();
}
if (param == Display.KEY_LEFT){
movePlate(1);
}else if(param == Display.KEY_RIGHT){
movePlate(2);
}
}
}
}
|