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 bx; /* Ballの位置と角度 */
private int by;
private int bd;
private int platex;
private int newPlatex;
private boolean plateMoveFlag;
private Image end;
private Thread thread;
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(10);
}catch (InterruptedException e){
break;
}
repaint();
}
}
public void paint(Graphics g){
if (game == 0){
bx = 700;
by = 500;
bd = 45;
platex = 22;
newPlatex = 22;
plateMoveFlag = true;;
game = 1;
}else if (game == 1){
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);
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;
}
drawBall(g);
}else if (game == 4){
/* GAME OVER */
g.lock();
g.drawImage(end, 1, 30);
g.unlock(true);
/* Startボタン待ち */
}
}
/* Ballを描く */
private void drawBall(Graphics g){
g.lock();
g.setColor(Graphics.getColorOfName(Graphics.BLACK));
g.fillRect(bx / 10, by / 10, 4, 4);
moveBall();
g.setColor(Graphics.getColorOfName(Graphics.WHITE));
g.fillRect(bx / 10 + 1, by / 10, 2, 4);
g.fillRect(bx / 10, by / 10 + 1, 4, 2);
g.unlock(true);
}
private void randamDirect(int type){
int tmp = Math.abs(rand.nextInt()) % 3;
if (type == 1){
if (tmp == 0){
bd = 60;
}else if (tmp == 1){
bd = 45;
}else{
bd = 30;
}
}else{
if (tmp == 0){
bd = 120;
}else if (tmp == 1){
bd = 135;
}else{
bd = 150;
}
}
}
/* Ballを動かす */
private void moveBall(){
int tmp;
/* (2,2) - (97,117) */
if (bd == 45){
bx += 10;
by -= 10;
if (bx >= 940){
bd = 135;
}
if (by <= 20){
bd = 315;
}
}else if (bd == 135){
bx -= 10;
by -= 10;
if (bx <= 20){
bd = 45;
}
if (by <= 20){
bd = 225;
}
}else if (bd == 225){
bx -= 10;
by += 10;
if (bx <= 20){
bd = 315;
}
if (by >= 1060){
tmp = checkBound();
if (tmp == 1){
randamDirect(2);
}else if (tmp == 2){
bd = 120;
}else if (tmp == 3){
bd = 135;
}else if (tmp == 4){
bd = 45;
}else if (tmp == 5){
bd = 30;
}
}
}else if (bd == 315){
bx += 10;
by += 10;
if (bx >= 940){
bd = 225;
}
if (by >= 1060){
tmp = checkBound();
if (tmp == 1){
bd = 150;
}else if (tmp == 2){
bd = 135;
}else if (tmp == 3){
bd = 45;
}else if (tmp == 4){
bd = 60;
}else if (tmp == 5){
randamDirect(1);
}
}
}else if (bd == 30){
bx += 10;
by -= 5;
if (bx >= 940){
bd = 150;
}
if (by <= 20){
bd = 330;
}
}else if (bd == 150){
bx -= 10;
by -= 5;
if (bx <= 20){
bd = 30;
}
if (by <= 20){
bd = 210;
}
}else if (bd == 210){
bx -= 10;
by += 5;
if (bx <= 20){
bd = 330;
}
if (by >= 1060){
tmp = checkBound();
if (tmp == 1){
bd = 30;
}else if (tmp == 2){
bd = 30;
}else if (tmp == 3){
bd = 150;
}else if (tmp == 4){
bd = 135;
}else if (tmp == 5){
randamDirect(2);
}
}
}else if (bd == 330){
bx += 10;
by += 5;
if (bx >= 940){
bd = 210;
}
if (by >= 1060){
tmp = checkBound();
if (tmp == 1){
bd = 150;
}else if (tmp == 2){
bd = 150;
}else if (tmp == 3){
bd = 30;
}else if (tmp == 4){
bd = 60;
}else if (tmp == 5){
randamDirect(1);
}
}
}else if (bd == 60){
bx += 5;
by -= 10;
if (bx >= 940){
bd = 120;
}
if (by <= 20){
bd = 300;
}
}else if (bd == 120){
bx -= 5;
by -= 10;
if (bx <= 20){
bd = 60;
}
if (by <= 20){
bd = 240;
}
}else if (bd == 240){
bx -= 5;
by += 10;
if (bx <= 20){
bd = 300;
}
if (by >= 1060){
tmp = checkBound();
if (tmp == 1){
randamDirect(1);
}else if (tmp == 2){
bd = 120;
}else if (tmp == 3){
bd = 120;
}else if (tmp == 4){
bd = 60;
}else if (tmp == 5){
bd = 45;
}
}
}else if (bd == 300){
bx += 5;
by += 10;
if (bx >= 940){
bd = 240;
}
if (by >= 1060){
tmp = checkBound();
if (tmp == 1){
bd = 135;
}else if (tmp == 2){
bd = 120;
}else if (tmp == 3){
bd = 60;
}else if (tmp == 4){
bd = 60;
}else if (tmp == 5){
randamDirect(2);
}
}
}
}
/* ボールとプレートの衝突判定 */
private int checkBound(){
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 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);
}
}
}
}
|