寂しいブロック崩しのソース
///////////////////////////////////////////////////////////////// もっと色々したいとこだが、情報ネットワーク演習の課題が・・・・・あるんだ///////////////////////////////////////////////////////////////import java.applet.Applet;import java.awt.*;import java.awt.event.*;public class Block3 extends Applet implements MouseMotionListener { // 変数の定義 double x = 150, y = 100; // ボールの位置(中心) double vx = 1.2, vy = 1.5; // ボールの速度 int bx = 200, by = 250; // ラケットの位置(中心) int l = 20, r = 15, d = 2 * r; // l:ラケットの長さの半分、r:ボールの半径、d:ボールの直径 int screen_size_x = 400, screen_size_y = 400; // スクリーンサイズ int margin = 5; // スクリーンと左右の枠の間のマージン int frame_size_x = screen_size_x - (2 * margin), frame_size_y = 300;// 枠のサイズ int[] target; // 的のステータス 的があれば1、ヒットすれば0 int[] target_pos; // 的の左端x座標 int num_of_target = 5; // 的の数 int width_of_target = 80, height_of_target = 30; // 的の幅、高さ int w = 5; // wait(msec)用 int point = 0; // 得点 // 初期化 public void init() { target = new int[5]; // 的ステータス用 target_pos = new int[5]; setSize(screen_size_x, screen_size_y);// 画面サイズ addMouseMotionListener(this); // マウスイベント Thread Ball = new Ball_thread(); // ball移動のスレッドスタート Ball.start(); Thread Racket = new Racket_thread(); Racket.start(); // ラケット移動のスレッドスタート } // マウス移動時の処理 public void mouseDragged(MouseEvent e) { } public void mouseMoved(MouseEvent e) { bx = e.getX(); // ラケット位置取得 } // ペイント動作時の処理 public void paint(Graphics g) { int x1; // 的の座標 g.drawRect(margin, 0, frame_size_x, frame_size_y); // 枠の描画 for (int i = 0; i < num_of_target; i++) { x1 = i * (width_of_target - 2) + margin; // 的の原点(左端)座標計算 g.setColor(Color.red); // 的の色は赤色 g.fillRect(x1 + 1, 3, width_of_target - 4, height_of_target);// 的の描画 target[i] = 1;// 的のステータスを初期化 1:的あり 0:的無し target_pos[i] = x1; } g.setColor(Color.black); // カラーを黒に戻す g.drawRect(margin, 0, frame_size_x, frame_size_y); // 枠の描画 g.drawString("得点" + (point), 10 + margin, 350);// 得点表示 } // ボールの移動のスレッド class Ball_thread extends Thread { public void run() { Graphics g = getGraphics(); // グラフィクスオブジェクトを取得 Font fnt = new Font("SansSerif", Font.BOLD | Font.ITALIC, 30); while (true) { // ボールの描画 g.clearRect((int) x - r, (int) y - r, d, d); // 前回のボール消去 x += vx; y += vy; g.fillOval((int) (x - r), (int) (y - r), d, d); // ボール描画 // ラケットの衝突判定 if (x < bx + (l + r) && x > bx - (l + r) && (by < (y + r + 1) && by > (y - r - 1))) { vy = -vy; }// ラケットの衝突判定 // 壁の衝突判定 if (x < r + margin + 2) { vx = -vx; } // 左の壁の衝突判定 但し壁の少し手前で反射される if (y < r + 2) { vy = -vy; } // 上の壁の衝突判定 if (x > (margin + frame_size_x - r - 2)) { vx = -vx; } // 右の壁の衝突判定 if (y > (frame_size_y - r - 2)) { // 下の壁の衝突判定 g.clearRect((int) x - r, (int) y - r, d, d);// 前回のボール消去 g.setFont(fnt); g.setColor(Color.pink); // 文字のカラーを設定 g.drawString("GAME OVER", 100 + margin, 150);// 終了文字の表示 break; } // ブロック衝突判定 for (int i = 0; i < num_of_target; i++) { if ((x > target_pos[i] + r) && (x < target_pos[i] + width_of_target - r)// 的の衝突判定 && (y < height_of_target + 5 + r) && target[i] == 1) { vy = -vy; target[i] = 0; g.clearRect(target_pos[i] + 1, 3, width_of_target - 4, height_of_target); point++; g.drawString("得点" + (point), 10 + margin, 350);// 得点表示 g.clearRect((int) 7 + margin, (int) 330, 100, 50); // 前回の得点消去 g.drawString("得点" + (point), 10 + margin, 350);// 得点表示 break; } } try { Ball_thread.sleep(w);// ボールの速度調整用 } catch (InterruptedException e) { }// エラーハンドリング // 全部ブロックを崩せたら if (point == num_of_target) { g.clearRect((int) x - r, (int) y - r, d, d);// 前回のボール消去 g.setFont(fnt); g.setColor(Color.orange); // 文字のカラーを設定 g.drawString("Game Clear!!", 100 + margin, 150);// 終了文字の表示 break; } } } } // ラケットの移動のスレッド class Racket_thread extends Thread { public void run() { Graphics g = getGraphics(); // グラフィックスオブジェクトを取得 while (true) { // ラケット処理 カラーを黒に設定しラケットを消去 if ((bx - l) > margin + 10 && (bx + l) < (margin + frame_size_x) - 10) { // ラケットが枠をはみ出さないか? g.setColor(Color.white);// カラーを白に設定 g.drawLine(margin + 1, by, frame_size_x, by);// 前回のラケットを消去 g.setColor(Color.black); g.drawLine(bx - l, by, bx + l, by); // ラケット描画 } } } }}