モグラクラスの動作検証

リスト1

// モグラクラス
//
class MoguraBody implements TimerListener {
    
    public final int DEFAULT_TIME=2000; // 2秒間出現する
    public final int DEFAULT_BUSY=8;    // 8回に1回出現する
    
    static Random rnd = new Random();
    protected MoguraListener moguralisten = null;
    
    protected ImageLabel moguraimage;
    protected Timer timer;
    protected Image [] img;
    
    protected boolean flagVisible = false;
    
    protected int busy;
    protected int time;
    protected int timeresolution;
    protected int score;
    protected int miss;
    
    protected Image blank;
    
    // コンストラクタ
    MoguraBody( Image [] image )
     throws Exception {
        
        // 絵が0個ではモグラは成立しない
        if( image.length == 0 ) {
            throw new IllegalArgumentException("Error");
        }
        
        // 空の絵をロードする
        MediaImage mi = MediaManager.getImage("resource:///blank.gif");
        try {
            mi.use();
        }
        catch(Exception e) {
            throw e;
        }
        
        blank = mi.getImage();
        
        
        // モグラの絵を保存 // 配列の代入ができないVMがあるらしい
        img = new Image[image.length];
        for(int i=0 ; i 5 ) {
                sendScore(false);
                miss = 0;
            }
        }
        
        return retval;
    }
    
    
    // リスナーに結果を報告
    //
    protected void sendScore(boolean f) {
        if(f)
            score++;
        else
            score--;
            
        if(moguralisten != null ) {
            moguralisten.scoreCounter(f);
        }
    }
    
    // モグラリスナー登録
    //
    public void setMoguraListener(MoguraListener m) {
        moguralisten = m;
    }
    
    
    // モグラ難易度評価/設定
    //
    protected void checkScore() {
        // 5点ごとに難易度評価を行う
        if( score % 5 == 0 ) {
            time = DEFAULT_TIME - score * 80;
            if( time < timeresolution )
                time = timeresolution;
            setInterval(time);
        }
        busy = time / 222;
        if( busy < 2 )
            busy = 2;
        
    }
    
    // タイマー受信
    //
    public void timerExpired(Timer src) {
        if(flagVisible) {
            // ミスヒットである
            moguraimage.setImage(blank);
            flagVisible = false;
            sendScore(false);
        }
        else {
            int i = Math.abs( rnd.nextInt() % busy );
            if(i == 1) {
                // 出現 絵もランダムに切り替える
                i = Math.abs( rnd.nextInt() % img.length );
                moguraimage.setImage(img[i]);
                moguraimage.setVisible(true);
                flagVisible = true;
            }
        }
        checkScore();
        
        timer.stop();
        timer.start();
    }
}

本文に戻る



モバイルショップ

最新CPU搭載パソコンはドスパラで!!
第3世代インテルCoreプロセッサー搭載PC ドスパラはスピード出荷でお届けします!!

最新スペック搭載ゲームパソコン
高性能でゲームが快適なのは
ドスパラゲームパソコンガレリア!