import java.io.*; import java.util.Random; import javax.microedition.io.Connector; import com.nttdocomo.io.ConnectionException; import com.nttdocomo.io.HttpConnection; 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 boolean effect; private ShortTimer timer; private MediaImage media1,media2,media3; /* --- Constructor --- */ public CrashCanvas() { init(); setSoftLabel(SOFT_KEY_1,"終了"); } /* --- 初期化 --- */ private void init() { MediaImage media; InputStream in; try { in=Connector.openInputStream("scratchpad:///0;pos=0"); if (in.read()<=0) { in.close(); cacheImage(); } else { in.close(); } media1=MediaManager.getImage("scratchpad:///0;pos=0"); 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 (IOException e) { e.printStackTrace(); } } private void cacheImage() { HttpConnection connection; InputStream in; OutputStream out; byte[] data; int length; try { connection=(HttpConnection)(Connector.open( IApplication.getCurrentApp().getSourceURL()+"back.gif", Connector.READ)); connection.setRequestMethod(HttpConnection.GET); connection.setRequestProperty("Content-type","image/gif"); connection.connect(); in=connection.openInputStream(); out=Connector.openOutputStream("scratchpad:///0;pos=0"); data=new byte[128]; while (true) { length=in.read(data); if (length==-1) break; out.write(data,0,length); } in.close(); out.close(); } catch (IOException 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; effect=false; } /* --- メイン --- */ 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); } private void paintEffect(Graphics g) { int x0,y0,dx,dy; if (effect) { x0=(ball.getX()+enemy.getX())/2; y0=(ball.getY()+enemy.getY())/2; for (int i=1;i<=5;i++) { dx=random.nextInt()%8; dy=random.nextInt()%8; g.setColor(Graphics.getColorOfName(Graphics.YELLOW)); g.drawLine(x0+dx,y0+dy,x0+dx*3,y0+dy*3); } effect=false; } } /* --- イベント --- */ 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(); } } }