JAVA SOURCE






import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

class Ball{
  int ball_y=100,ball_vx=16,ball_vy=10,ch=0,cch=0; 
  public boolean collide=false,breakup=false;
  public int ball_x,score=0,number;
  public int width=400,height1=600;
  public String c;
  Random r=new Random();
  int ball_size=20;
  public Ball(int x1) {
     ball_x=x1;
  }
  public void ballTick(int bar_x,int bar_y,int bar_width,int goal,int goal_width){
      // 速度に従って,次の座標を決定
    ball_x=ball_x+ball_vx;
    ball_y=ball_y+ball_vy;
      // 左端から飛び出そうになったら,反射させる
    if(ball_x<0){
       ball_x= -ball_x;
       ball_vx= -ball_vx;
       ch=1;      
    }
      // 右端から飛び出そうになったら,反射させる
    else if(ball_x >width-ball_size*2){
      
      ball_x=(width-ball_size*2)-(ball_x-(width-ball_size*2));
      ball_vx= -ball_vx;
      ch=1;      
    }
      // 上端から飛び出そうになったら,反射させる
    else if(ball_y==100)
       {    
           if( ball_x>0 && ball_x+ball_sizegoal+goal_width ){ball_vy= -ball_vy;
             collide=true;
             cch=2;}
          
       }
     else if(ball_y+ball_size*2<100) {
                       System.out.println("ball y la  "+ball_y);
                       ball_y=100;ball_vy=-ball_vy;ball_size=20;
                       score+=Integer.parseInt(c);
                       //後に衝突の時ボールの番号を変えるため 
                       cch=2;
                       }
      // バーのある線を通過しそうになったら,
    else if(ball_y+ball_size*2 ==bar_y)
      {
       if( bar_x <= (ball_x+ball_size) && (ball_x+ball_size) <= (bar_x+bar_width)) {
          ball_vy=- ball_vy;
          //バーの左側に衝突すると番号は変わる
          collide=true;
       }
       else if((ball_x+ball_size) > (bar_x+bar_width) && ball_x+ball_size <= bar_x+bar_width*2) {
           ball_vy=- ball_vy;
           //バーの右側に衝突すると番号は上限に衝突するまで変わらない
          cch=3;}
      
       }
 
      // 下端から飛び出そうになったら,反射させる
    else if(ball_y+ball_size*2 > height1){
      
      breakup=true;ball_vx=0;
      if(cch!=4) {breakball();
                  number++;//落ちたボールの数
                  }
      cch=4;
      for(int i=0;i<40;i++) { 
         if(x3[i]>ball_size && y3[i]ball_size && y3[i]>ball_size)
           {
            x3[i]+=10;
            y3[i]+=10;
           }
         if(x3[i]ball_size)
           {
            x3[i]-=10;
            y3[i]+=10;
           }
         if(x3[i]=100) {
   g1.setColor(Color.white);
   g1.fillOval(ball_x,ball_y,ball_size*2,ball_size*2);
   g1.setColor(Color.black);
   g1.setFont(new Font("Courier",Font.BOLD,30));
   g1.drawString(c,ball_x+10,ball_y+34);
   }
   else if(ball_y<100) {g1.setColor(Color.white);
   g1.fillOval(ball_x,ball_y,ball_size*2,ball_size*2);
   ball_size-=5;}
   else {for(int i=0;i<40;i++) { //ボールが割れるのため
                                 g1.setColor(Color.white);
                                 g1.fillOval(ball_x+x3[i],ball_y+y3[i],5,5);}
        }
  }
} 

//KeyListenerの機能をAppletのp中に起こすため
class Rectangle extends Canvas implements KeyListener{
   public int d;   
   public void keyPressed(KeyEvent e){
	System.out.println("keyPressed("+e+")");
	int key=e.getKeyChar();
        if(key=='h') d=0;
	  else if(key=='l') d=1;
	    else if(key=='j') d=2;
	      else if(key=='u') d=3;
                else if(key=='b') d=4;
      }
	// その他Keyイベント
	public void keyReleased(KeyEvent e){}
	public void keyTyped(KeyEvent e){}

 }

class Kadai1217 extends Frame implements ItemListener,ActionListener,Runnable{
  Ball[] ball=new Ball[3];
    // 画面の幅,高さ
  int width=400, height=600;
  int[] number1=new int[3];
  Random r=new Random();
    // バーの幅,高さ
  int bar_width=40,bar_height=10;
    // バーの座標
  int bar_x=0, bar_y=550;
  // バーの速度
  int bar_vx,level,sum,t,sum1;
  boolean pause=true;
  Image offScreenImage;
  Graphics offScreenGraphics;
  int[] x0=new int[100];
  int[] y0=new int[100];
  Box bigBox;
  Choice choice;
  Button restartbutton,pausebutton;
  Rectangle frame;
  public Kadai1217(){
    super();
    frame=new Rectangle();
    ball[0]=new Ball(20);   
    bigBox=new Box(BoxLayout.Y_AXIS);
    setLayout(new BorderLayout());
    restartbutton=new Button("restart");
    pausebutton=new Button("start");
    bigBox.add(restartbutton);
    restartbutton.addActionListener(this);
    restartbutton.addKeyListener(frame);
    bigBox.add(pausebutton);
    pausebutton.addActionListener(this);
    pausebutton.addKeyListener(frame);
    choice=new Choice();
    choice.addItem("Level 初級");
    choice.addItem("Level  中級");
    choice.addItem("Level  上級");
    bigBox.add(choice);
    choice.addItemListener(this);
    add(bigBox,BorderLayout.EAST);
    makerandom();
    setSize(600,700);
    setVisible(true);
  }
   // 上のstartメソッドの中で,th.start()が呼ばれるとスレッドからこの
    // メソッドが呼ばれる.
  public void run(){
   
    for(;;){
        // 0.1秒(100ミリ秒)スリープする
      try { Thread.sleep(80); }
      catch(Exception e){}
      //勝利の時画像はきらきらするように
      t++;
      if(!pause) barTick();
         }
  }
  void barTick(){
      // バーの移動
    bar_x=bar_x+bar_vx;
    //キーを入力に応じてバーを移動させる
    if(frame.d==0)  bar_vx= -10;
	else if(frame.d==1) bar_vx=10;
	 else if(frame.d==2) bar_vx=0;
	   else if(frame.d==3) {bar_y-=20;frame.d=5;}
             else if(frame.d==4) {bar_y+=20;frame.d=5;}
      // 左端から飛び出そうになったら左端に合わせる
    if(bar_x<0) bar_x=0;
      // 右端から飛び出そうになったら右端に合わせる
    else if(bar_x+bar_width*2 >width) bar_x=width-bar_width*2;
    ball[0].ballTick(bar_x,bar_y,bar_width,a,b);
   //レベルを選ぶに応じる
    switch(level) {
     case 2:
     ball[1].ballTick(bar_x,bar_y,bar_width,a,b);
     break;
     case 3:
     for(int i=1;i<3;i++) ball[i].ballTick(bar_x,bar_y,bar_width,a,b);
     break;
    }
    
     repaint();
  }
  
  public void update(Graphics g){
    if(offScreenImage==null){
      offScreenImage=createImage(getWidth(),getHeight()); // オフスクリーンイメージを600x400のサイズで作成
      offScreenGraphics=offScreenImage.getGraphics(); // オフスクリーンイメージに描画するための Graphics オブジェクト
    }
    paint(offScreenGraphics); // 次の画面のイメージを作る.
    g.drawImage(offScreenImage,0,0,this); // イメージを本物のスクリーンに書き込む
  }
  
   //勝利の時画像を描くため
   void makerandom(){for(int i=0;i<100;i++) {x0[i]=r.nextInt(399)+1;
                                            y0[i]=r.nextInt(599)+1;}         
                     }
  int a=125,b=150;
  Color wordcolor;
  public void paint(Graphics g){
    g.setColor(Color.pink);
    g.fillRect(0,0,720,700);
    g.setColor(Color.yellow);
    g.fill3DRect(0,0,425,620,true); 
    g.setColor(Color.black);
    g.drawLine(405,600,425,620);
    g.fillRect(0,0,405,600);
    g.setFont(new Font("Courier",Font.PLAIN,20));
    //コントロールの案内を描く
    g.drawString("U",210,640);
    g.drawString("B",210,690);
    g.drawString("H ",180,665);
    g.drawString("Control Key",50,665);
    g.drawString("L",240,665);
    g.drawLine(215,642,215,667);
    g.drawLine(190,657,240,657);
    int x6[]={210,215,220};
    int y6[]={652,642,652};
    g.fillPolygon(x6,y6,3);
    int y7[]={662,672,662};
    g.fillPolygon(x6,y7,3);
    int x8[]={200,200,190};
    int y8[]={652,662,657};
    g.fillPolygon(x8,y8,3);
    int x9[]={230,230,240};
    g.fillPolygon(x9,y8,3);
    if(sum==0 || (sum<30 && sum%10 !=0 && sum1 !=5)) {
      // バーの左側をを描く
    g.setColor(Color.blue);
    g.fillRect(bar_x,bar_y,bar_width,bar_height);
    //バーの右側をを描く
    g.setColor(Color.green);
    g.fillRect(bar_x+bar_width,bar_y,bar_width,bar_height);
    g.setColor(Color.yellow);
    //ゴールを描く
    g.fillRect(0,90,a,10);
    g.fillRect(a-10,50,b+20,50);
    g.fillRect(a+b,90,width-a-b+5,10);
    g.setColor(Color.black);
    g.fillRect(a,60,b,40);
    
    if(ball[0].collide) number1[0]=r.nextInt(8)+1;//衝突するとボールの番号が変わる
       ball[0].draw(g,number1[0]);
       //得た点
       sum=ball[0].score;
       //落ちたボール
       sum1=ball[0].number;
     switch(level) {
     case 2:
       if(ball[1].collide) number1[1]=r.nextInt(8)+1;
       ball[1].draw(g,number1[1]);
       sum=ball[0].score+ball[1].score;
       sum1=ball[0].number+ball[1].number;
       break;
     case 3:
      for(int i=1;i<3;i++) 
      {
       if(ball[i].collide) number1[i]=r.nextInt(8)+1;
       ball[i].draw(g,number1[i]);
      }
      sum=ball[0].score+ball[1].score+ball[2].score;
      sum1=ball[0].number+ball[1].number+ball[2].number;
      break;    
     }
    g.setColor(Color.white);
    g.drawString("Score",150,50);
    g.drawString(Integer.toString(sum),280,50);
    }
    if(sum<=30 && sum % 10==0 && sum>0 && sum1 !=5) {  g.setFont(new Font("Courier",Font.BOLD,30));
                              g.setColor(Color.white);     
                              g.drawString("Congratulation",100,300);
                              g.drawString("You have won ",100,400);
                              g.setFont(new Font("Courier",Font.PLAIN,10));
                              if(t%2==0) {wordcolor=Color.black ;}
                              else if(t%2!=0) {wordcolor=Color.yellow;}
                              g.setColor(wordcolor);
                              for(int i=0;i<100;i++) 
                             g.drawString("*",x0[i],y0[i]);
                            }
      if(sum > 30 || sum1==5) {
                             g.setFont(new Font("Courier",Font.BOLD,50));
                             g.drawString("GAME OVER",50,300);pause=true;}
   
    
    }
  public void actionPerformed(ActionEvent e){
  Object source=e.getSource();
   if(source.equals(restartbutton))
     {sum=0;
      bar_x=0;bar_y=550;
       pause=false;
       pausebutton.setLabel("pause");
     ball[0]=new Ball(20);ball[0].height1=600;
     if (level==2) {ball[1]=new Ball(150);}
     else if(level== 3) {ball[1]=new Ball(150);
                          ball[2]=new Ball(350);ball[2].ball_vx=32;ball[2].ball_vy=10;}
     }
   else if(source.equals(pausebutton)) {
     if(pausebutton.getLabel().equals("pause") )
     {  
        if(!pause) pausebutton.setLabel("resume");
         pause=true;}
        else if(pausebutton.getLabel().equals("resume") || pausebutton.getLabel().equals("start") ) {pause=false;  pausebutton.setLabel("pause");}
     }
   if(pause) pausebutton.removeKeyListener(frame);
   else if(!pause) {pausebutton.addKeyListener(frame);}
    }
   public void itemStateChanged(ItemEvent ie){
   Object source=ie.getSource();
   if(source.equals(choice)){
     String s= choice.getSelectedItem();
     if(s.equals("Level  中級")) {level=2;pause=true;System.out.println("ban da chon");}
        else if(s.equals("Level  上級")) {level=3;pause=true;}
          else level=0;
     }
   }

  
}
public class Kadai1216 extends Applet {
    Kadai1217 kadai;
    public Thread th=null;
    public void init(){
    kadai=new Kadai1217();
    }
    public void start(){
      if (th==null) th=new Thread(kadai);
      th.start();
    }
    public void stop(){
	th=null;
  }
}