// // シューテングゲーム第一回 // // 1998/ 1/ 3,2002/ 8/ 9 宍戸 輝光 // import java.awt.*; import java.util.Random; public class sgame1 extends java.applet.Applet implements Runnable { Image screen,bk; Graphics g_screen,g_bk; Thread runner=null; Random rnd=new Random(); int x,mx; 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(); makeBackground(); /* 背景作成 */ x=160; /* 自機の初期位置 */ drawScreen(); /* 自機描画 */ repaint(); if (runner==null) { runner=new Thread(this); /*スレッド生成*/ runner.start(); } } public void stop() { /*終了時にスレッド破棄*/ if (runner!=null) { runner.stop(); runner=null; } } public void makeBackground() { /* 背景作成 */ 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()) % 256; /* 乱数で色を作る */ g=Math.abs(rnd.nextInt()) % 256; b=Math.abs(rnd.nextInt()) % 256; g_bk.setColor(new Color(r,g,b)); g_bk.fillRect(x,y,1,1); } } public void run() { /* スレッド本体 */ while (runner!=null) { if (mx>0) /* マウスカーソルが範囲内 */ if (mxx+4) /* 自機がカーソルの右側 */ x=x+4; /* 自機を右に移動 */ drawScreen(); /* 自機描画 */ try { Thread.sleep(25); } /*ウエイト*/ catch (InterruptedException e) {} } } public void drawScreen() { /* 自機描画 */ g_screen.drawImage(bk,0,0,this); /* 画面用イメージに背景を描く */ g_screen.setColor(Color.cyan); /* 画面用イメージに自機を描く */ g_screen.fillRect(x,280,32,16); repaint(); /* 画面に画面用イメージを表示 */ } public boolean mouseMove(Event evt,int wx,int wy){ /*マウス移動*/ mx=-1; if (wy<270 || wy>310 || wx<10 || wx>310) return true; mx=wx-10; /* ゲーム画面上のマウスカーソルの座標 */ 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); } }