import java.util.Random; import com.nttdocomo.io.ConnectionException; import com.nttdocomo.ui.*; import com.nttdocomo.util.*; public class CrashCanvas extends Canvas { /* --- 定数 --- */ public final static int ID_TIMER=0; // 短時間タイマのID /* --- Field --- */ private static Random random=new Random(); private Ball ball,enemy; private int time,gameover; private Image background; private ShortTimer timer; private MediaImage media1,media2,media3; /* --- Constructor --- */ public CrashCanvas() { init(); setSoftLabel(SOFT_KEY_1,"終了"); } /* --- 初期化 --- */ private void init() { try { media1=MediaManager.getImage("resource:///back.gif"); media1.use(); background=media1.getImage(); media2=MediaManager.getImage("resource:///ball1.gif"); media2.use(); ball=new Ball(media2.getImage()); media3=MediaManager.getImage("resource:///ball2.gif"); media3.use(); enemy=new Ball(media3.getImage()); } catch (ConnectionException e) { e.printStackTrace(); } } /* --- 開始 --- */ public void start() { timer=ShortTimer.getShortTimer(this,ID_TIMER,1000/10,true); timer.start(); fight(); } public void fight() { ball.setRange( getWidth ()-ball.getImage().getWidth(), getHeight()-ball.getImage().getHeight()-8); ball.setPosition( 16, getHeight()-ball.getImage().getHeight()-8); enemy.setRange( getWidth ()-enemy.getImage().getWidth(), getHeight()-enemy.getImage().getHeight()-8); enemy.setPosition( getWidth ()-enemy.getImage().getWidth()-16, getHeight()-enemy.getImage().getHeight()-8); time=0; gameover=0; } /* --- メイン --- */ public void mainLoop() { if (gameover==0) // ゲーム中 { controll(); checkCrash(); checkOut(); time++; } else if (gameover>10) // Gameover 表示中 { if ((getKeypadState() & (1<enemy.getX()) { enemy.right(); } if (ball.getY()0) { font=Font.getDefaultFont(); g.setColor(Graphics.getColorOfName(Graphics.BLACK)); g.drawString("Game Over", getWidth ()/2-font.stringWidth("Game Over")/2, getHeight()/2-font.getHeight()/2+font.getAscent()); } g.unlock(true); } /* --- イベント --- */ public void processEvent(int type,int param) { if ((type==Display.TIMER_EXPIRED_EVENT) && (param==ID_TIMER)) { mainLoop(); paint(getGraphics()); } if ((type==Display.KEY_PRESSED_EVENT) && (param==Display.KEY_SOFT1)) { timer.stop(); timer.dispose(); media1.unuse(); media2.unuse(); media3.unuse(); media1.dispose(); media2.dispose(); media3.dispose(); IApplication.getCurrentApp().terminate(); } } }