// モグラパネル // class MoguraPanel extends Panel implements KeyListener,MoguraListener,SoftKeyListener,Runnable { public final int TIMEUP = 60; int score = 0; MoguraBody [] mogu = new MoguraBody[9]; Image [] img; IApplication imaster; Label lbl; ImageLabel imglabel; int time; // 経過時間 Thread gogo; boolean gamestart = false; MoguraPanel(IApplication iapp) { super(); imaster = iapp; // レイアウトマネージャを無効にしておく this.setLayoutManager(null); // イメージの取得 MediaImage mi1 = MediaManager.getImage("resource:///dog1.gif"); MediaImage mi2 = MediaManager.getImage("resource:///dog2.gif"); MediaImage mi3 = MediaManager.getImage("resource:///dog3.gif"); try { mi1.use(); mi2.use(); mi3.use(); } catch(Exception e) { } // モグラの絵を用意する img = new Image[3]; img[0] = mi1.getImage(); img[1] = mi2.getImage(); img[2] = mi3.getImage(); // 9匹のモグラを格子状に並べる int t = 0; for(int i=0; i < 3 ; i++ ) { for(int j=0 ; j < 3; j++) { try { mogu[t] = new MoguraBody(img); } catch(Exception e) { imaster.terminate(); } mogu[t].addMogura(this); mogu[t].setLocation( j * mogu[t].getWidth(), i * mogu[t].getHeight() ); mogu[t].setMoguraListener(this); t++; } } // タイトル画面用コンポーネントの配置 lbl = new Label( "もぐら叩き" ); lbl.setLocation(10, 0); add(lbl); imglabel = new ImageLabel(img[0]); imglabel.setLocation( 50,50 ); add(imglabel); // ゲームタイマー用スレッドの準備 gogo = new Thread(this); gogo.start(); setKeyListener(this); setSoftKeyListener(this); setSoftLabel(Frame.SOFT_KEY_1,"Start" ); setSoftLabel(Frame.SOFT_KEY_2,"Stop" ); } // スタートスクリーン表示/非表示 public void displayStartupScreen(boolean b) { lbl.setVisible(b); imglabel.setVisible(b); setSoftLabel(Frame.SOFT_KEY_1,"Start" ); } // Runnableインタフェース public void run() { while(true) { try { Thread.sleep(500); } catch(Exception e){} if( gamestart ) { for( time=0 ; time < TIMEUP ; time++ ) { if(!gamestart) break; setSoftLabel(Frame.SOFT_KEY_1, "" + score + "/" + time ); try { Thread.sleep(1000); } catch(Exception e){} } for(int i=0 ; i < 9; i++ ) { mogu[i].stopMogura(); } displayStartupScreen(true); gamestart = false; } } } // モグラリスナメソッド public void scoreCounter(boolean hit) { if(hit) { setSoftLabel(Frame.SOFT_KEY_1,"" + (++score) + "/" + time ); } else { setSoftLabel(Frame.SOFT_KEY_1,"" + (--score) + "/" + time ); } } public void keyPressed(Panel p, int param) { if( param > 0 && param < 10 ) { mogu[param-1].bang(); } } public void keyReleased(Panel p, int param) { // } public void softKeyPressed(int key) { // } public void softKeyReleased(int key) { if(key == Frame.SOFT_KEY_1 && !gamestart) { score = 0; displayStartupScreen(false); for(int i=0 ; i < 9; i++ ) { mogu[i].startMogura(); } gamestart = true; } if(key == Frame.SOFT_KEY_2 ) { if( gamestart ) { gamestart = false; } else { imaster.terminate(); } } } } public class mogura extends IApplication { public void start() { MoguraPanel mp = new MoguraPanel(this); Display.setCurrent(mp); } }