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 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 platex;
private int newPlatex;
private boolean plateMoveFlag;
private Image end;
private Thread thread;
private int[][] map ={
{0,0,0,0,0,0,0,0},
{0,1,0,0,0,0,1,0},
{0,1,0,0,0,0,1,0},
{0,1,1,1,1,1,1,0},
{0,1,0,0,0,0,1,0},
{0,1,0,0,0,0,1,0},
{0,1,0,0,0,0,1,0},
{0,0,0,0,0,0,0,0}
};
BlockMain(){
MediaImage mi;
mi = MediaManager.getImage("resource:///gif/end.gif");
try{
mi.use();
}catch(ConnectionException e){
}catch(UIException e){
}
end = 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(5);
}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[i][j] == 1){
g.setColor(Graphics.getColorOfName(Graphics.RED));
g.fillRect(2 + j * 12, 20 + i * 5, 11, 4);
}
}
}
g.unlock(true);
}
public void paint(Graphics g){
if (game == 0){
g.lock();
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);
game = 1;
}else if (game == 1){
bx = 700;
by = 500;
bd = 225;
newbx = 700;
newby = 500;
platex = 22;
newPlatex = 22;
plateMoveFlag = true;
delay = 8;
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()){
moveBall();
}else{
delayLevelup++;
if (delayLevelup >= 7){
if (delay > 1){
delay --;
}
delayLevelup = 0;
}
}
drawBall(g);
}else{
delayCount++;
}
}else if (game == 4){
/* GAME OVER */
g.lock();
g.drawImage(end, 1, 30);
g.unlock(true);
/* Startボタン待ち */
}
}
private boolean checkBlock(){
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[blocky][blockx] != 0){
boundBall(2);
return false;
}
blockx = (x + 1) / 12;
if (map[blocky][blockx] != 0){
boundBall(2);
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[blocky][blockx] != 0){
boundBall(4);
return false;
}
blockx = (x + 1) / 12;
if (map[blocky][blockx] != 0){
boundBall(4);
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[blocky][blockx] != 0){
boundBall(3);
return false;
}
}
if ((y >= 17) && (y <= 56)){
blocky = (y - 17) / 5;
if (map[blocky][blockx] != 0){
boundBall(3);
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[blocky][blockx] != 0){
boundBall(1);
return false;
}
}
if ((y >= 17) && (y <= 56)){
blocky = (y - 17) / 5;
if (map[blocky][blockx] != 0){
boundBall(1);
return false;
}
}
}
}
/* 右の壁にあたったかどうか */
if (x == 94){
boundBall(1);
}
/* 上の壁にあたったかどうか */
if (y == 2){
boundBall(2);
}
/* 左の壁にあたったかどうか */
if (x == 2){
boundBall(3);
}
/* プレートにあたったかどうか */
if (y == 106){
int tmp = checkBoundPlate();
boundBallOnPlate(tmp);
}
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;
}
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);
}
}
}
}
|