// // シューテングゲーム第三回 // // 1998/ 1/10,2002/ 8/ 9 宍戸 輝光 // import java.awt.*; import java.util.Random; public class sgame3 extends java.applet.Applet implements Runnable { Image screen,bk,exp; Graphics g_screen,g_bk,g_exp; Thread runner=null; Random rnd=new Random(); String s; FontMetrics fm; int x,mx,lx,ly,ex,elx,ely,st,xx,wait; boolean ls,els,end; public void start(){ resize(320,320); setBackground(Color.green); /*アプレットの背景を緑にする*/ screen=createImage(300,300); /* 画面用イメージ */ g_screen=screen.getGraphics(); bk=createImage(300,300); /* 背景用イメージ */ g_bk=bk.getGraphics(); exp=createImage(48,48); /* 爆発用イメージ */ g_exp=exp.getGraphics(); g_screen.setFont(new Font("TimesRoman",Font.BOLD,18)); fm=g_screen.getFontMetrics(new Font("TimesRoman",Font.BOLD,18)); s=new String("Click Mouse Button!!"); /* 表示文字列 */ xx=(300-fm.stringWidth(s))/2;/* センタリングマージン */ makeImages(); /* 背景・爆発イメージ作成 */ clearGame(); /* 初期化 */ drawScreen(); /* 自機と敵機描画 */ if (runner==null) { runner=new Thread(this); /*スレッド生成*/ runner.start(); } } public void stop() { /*終了時にスレッド破棄*/ if (runner!=null) { runner.stop(); runner=null; } } public void makeImages() { /* 背景・爆発イメージ作成 */ int i,x,y,r,g,b; g_bk.setColor(Color.black); /* 黒く塗りつぶす */ g_bk.fillRect(0,0,300,300); for (i=0;i<1000;i++) { /* 乱数で星を描く */ x=Math.abs(rnd.nextInt()) % 300; /* 乱数で座標を決定 */ y=Math.abs(rnd.nextInt()) % 300; r=Math.abs(rnd.nextInt()) % 255; /* 乱数で色を作る */ g=Math.abs(rnd.nextInt()) % 255; b=Math.abs(rnd.nextInt()) % 255; g_bk.setColor(new Color(r,g,b)); g_bk.fillRect(x,y,1,1); } g_exp.setColor(Color.black); /* 爆発イメージ作成 */ g_exp.fillRect(0,0,48,48); for (i=0;i<1024;i++) { x=Math.abs(rnd.nextInt() % 48); /* 乱数で座標を決定 */ y=Math.abs(rnd.nextInt() % 48); r=(Math.abs(rnd.nextInt()) % 192)+64; /* 乱数で色を作る */ g=(Math.abs(rnd.nextInt()) % 64)+128; b=Math.abs(rnd.nextInt()) % 144; g_exp.setColor(new Color(r,g,b)); g_exp.fillRect(x,y,1,1); } g_exp.setColor(Color.orange); g_exp.fillOval(8,8,32,32); g_exp.setColor(Color.yellow); g_exp.fillOval(12,12,24,24); g_exp.setColor(Color.white); g_exp.fillOval(18,18,12,12); } public void clearGame() { /* ゲーム初期化 */ x=160; /* 自機の初期位置 */ ex=120; /* 敵の初期位置 */ wait=100; /* スレッドのウエイト100ms */ ls=false; /* 弾の発射フラグリセット */ els=false; st=0; /* ゲーム状態初期化 */ } public void run() { /* スレッド本体 */ int r,g,b; while (runner!=null) { if (st==0) { /* ゲーム中でなければ */ r=Math.abs(rnd.nextInt() % 255); /* 乱数で色を作る */ g=Math.abs(rnd.nextInt() % 255); b=Math.abs(rnd.nextInt() % 255); g_screen.setColor(new Color(r,g,b)); g_screen.drawString(s,xx,140); } if (st==1) { /* ゲーム中なら */ moveChr(); /* 自機と敵を移動 */ drawScreen(); /* 自機と敵を描画 */ drawMissile(); /* 弾の移動と表示 */ hantei(); /* 当たり判定とゲームオーバー処理 */ } repaint(); /* 画面に画面用イメージを表示 */ try { Thread.sleep(wait); } /*ウエイト*/ catch (InterruptedException e) {} } } public void moveChr() { /* 自機と敵移動 */ if (mx>0) /* マウスカーソルが範囲内 */ if (mxx+20) /* カーソルが自機の右側 */ x=x+4; /* 自機を右に移動 */ ex=ex+(rnd.nextInt() % 3)*4; /* 敵を乱数で移動 */ if (ex+16lx && ls) /* 弾から逃げる */ ex=ex-1; else if (ex+16>lx && ex-32268) ex=268; } public void drawScreen() { /* 自機と敵機描画 */ g_screen.drawImage(bk,0,0,this); /* 画面用イメージに背景を描く */ g_screen.setColor(Color.cyan); /* 画面用イメージに自機を描く */ g_screen.fillRect(x,280,32,16); g_screen.setColor(Color.yellow); /* 画面用イメージに敵機を描く */ g_screen.fillRect(ex,8,32,16); } public void drawMissile() { /* 弾の移動と表示 */ if (ls) { /* 自機の弾表示中 */ if (ly<0) /* 画面の上端まで来たら弾を消す */ ls=false; /* 発射フラグリセット */ g_screen.setColor(Color.red); g_screen.fillRect(lx,ly,2,16); ly=ly-12; /* 弾を12ドット上へ */ } if (!els) { /* 敵が弾を打ってなければ */ if (rnd.nextDouble()<0.3) { els=true; /* ある確率で弾を発射 */ elx=ex+16; /* 弾の初期位置 */ ely=16; } } else { /* 弾表示中なら */ ely=ely+8; /* 弾を下に移動 */ if (elxx+(Math.abs(rnd.nextInt()) % 24)-16) elx=elx-(6+Math.abs(rnd.nextInt()) % 3); if (ely>308) /* 画面下端に来たら消去 */ els=false; /* 発射フラグリセット */ g_screen.setColor(Color.orange); g_screen.fillRect(elx,ely,4,16); } } public void hantei() { /* 当たり判定とゲームオーバー */ if (elx>x && elx280 && els) { /* 敵弾と自機 */ g_screen.drawImage(exp,x,260,this); clearGame(); } if (lx>ex && lx310 || wx<10 || wx>310) return true; mx=wx-10; /* ゲーム画面上のマウスカーソルの座標 */ return true; } public boolean mouseDown(Event evt,int wx,int wy) { /* 弾発射 */ if (st==0) { /* ゲーム中でなければゲーム開始 */ wait=25; /* ウエイトを25msに */ st=1; /* ゲーム状態をゲーム中に設定 */ } if (mx<0 || ls) /* 範囲外かすでに弾が表示中なら戻る */ return false; lx=x+16; /* 弾の初期位置 */ ly=255; ls=true; /* 発射フラグセット */ return true; } public boolean mouseExit(Event evt,int x,int y) { mx=-1; /* アプレットの外に出たら移動しない */ return true; } public void paint(Graphics g){ g.drawImage(screen,10,10,this); /* 画面用イメージ表示 */ } public void update(Graphics g){ paint(g); } }