// Applet Source Name block.java] // 1997.08 // Author [ Yutaka Tamura ] import java.applet.*; import java.awt.*; import java.io.*; import java.net.*; public class block extends Applet implements Runnable { Dimension d; Image offs; Graphics grf; Image img_ball; MediaTracker mt; Thread kicker = null; AudioClip acp,acp1; URL iurl; DataInputStream dist; Font font,font1; int startflag=0; int counter=0; //得点 int kiroku=0; //今回の最高得点 int menber=3; //ゲームできる回数 int xpos=100; //ボールの位置 int ypos=200; //ボールの位置 int dd; //ボールの直径 int ypos_bias=30; //ブロック描画の際のy方向のインデント int xpos_bias=4; //ブロック描画の際のx方向インデント int xdir=1; //x軸の方向のフラグ int ydir=-1; //y軸の方向フラグ int raket_width=40; //ラケットの幅 int raket_ypos; //ラケットのy位置(固定) int raket_xpos; //ラケットのx方向位置 int xspeed=5; int yspeed=8; int block_width=26; //ブロックの幅 int block_height=12; //ブロックの高さ int block_xpich=28; //ブロック描画のx方向間隔  int block_ypich=14; //ブロック描画のy方向間隔 int block_xpos[]; //ブロック群のx方向位置 int block_ypos[]; //ブロック群のx方向位置 int flag[]; //ブロック描画可否のフラグ int hit_flag=0; //ブロックに当たったフラグ(2重当たり禁止) TextArea text1; TextField tf1; Button btn1,btn2; Label la1,la2; boolean mdownflag=false; //---------------------------init()------------------------------------------// public void init() { this.setLayout(null); d = size(); mt=new MediaTracker(this); offs = createImage( d.width, d.height); grf = offs.getGraphics(); font=new Font("TimesRoman",1,12); font1=new Font("TimesRoman",1,16); text1=new TextArea(9,16); text1.setEditable(false); la1=new Label("RECORD BOARD"); la2=new Label("Name:"); tf1=new TextField("GUEST"); btn1=new Button("Write Record"); btn2=new Button("Reload"); add(la1); add(text1); add(btn2); add(tf1); add(btn1); add(la2); la1.reshape(245,5,110,10); text1.reshape(245,20,110,165); btn2.reshape(245,195,110,20); tf1.reshape(245,225,110,20); btn1.reshape(245,255,110,20); //********イメージ、オーディオデーターの取得************** img_ball = getImage( getDocumentBase(),"images/maru8.gif"); mt.addImage(img_ball,0); acp=getAudioClip(getDocumentBase(),"au/tik.au"); acp1=getAudioClip(getDocumentBase(),"au/break.au"); raket_ypos=d.height-50; //ラケットy位置の設定 raket_xpos=d.width/2; //ラケットx位置の設定 //********ブロックの描画位置の設定**************** block_xpos=new int[48]; block_ypos=new int[48]; int temp=xpos_bias; for(int i=0; i<48; i++){ block_xpos[i]=temp; temp=temp+block_xpich; if((i+1==8)||(i+1==16)||(i+1==24)||(i+1==32)||(i+1==40)){ temp=xpos_bias; } } temp=ypos_bias; for(int i=0; i<48; i++){ block_ypos[i]=temp; if((i+1==8)||(i+1==16)||(i+1==24)||(i+1==32)||(i+1==40)){ temp=temp+block_ypich; } } flag=new int[48]; //ブロック群の描画フラグの初期化 for(int i=0; i<48 ; i++){ flag[i]=1; } receive(); } //-----------------------------run()---------------------------------------// public void run() { try{ mt.waitForID(0); }catch (InterruptedException e) { System.out.println("Wait Error"); return; } Thread.currentThread().setPriority(Thread.NORM_PRIORITY-3); while (kicker != null) { repaint(); try { Thread.sleep(30); hit_flag=0; } catch (InterruptedException e) {} } kicker=null; } public void update(Graphics g) { paint(g); } //-----------------描画(paint method)-------------------------------------// public void paint(Graphics g) { if(mt.isErrorID(0)){ g.drawString("Image Load Error.....",50,20); return; } if(! mt.checkID(0)){ g.drawString("Loading..........",d.width/2-30,20); return; } //******イメージー取得を待ってイメージサイズの取得 dd=img_ball.getWidth(this); //ボールの直径を取得 grf.setColor(new Color(255,255,255)); //バックカラーの描画 grf.fillRect(0,0,235,d.height); grf.setColor(Color.black); //コメント欄の描画 grf.fillRect(0,d.height-30,235,d.height); // //コメントの表示 // grf.setColor(Color.white); grf.setFont(font); grf.drawString("得点: "+counter,10,d.height-8); if(kiroku !=0 ){ grf.drawString("記録: "+kiroku,80,d.height-8); } grf.drawString("ボール数: "+menber,150,d.height-8); if(startflag==0){ grf.setColor(new Color(0,0,80)); grf.setFont(font1); grf.drawString("Click mouse to start!",25,d.height*2/3); } // //ブロックの表示 // for(int i=0 ; i<48 ; i++){ if(flag[i]==1){ grf.setColor(new Color(170,147,80)); grf.fillRect(block_xpos[i],block_ypos[i],block_width,block_height); } } // //ボールの表示 // if(mdownflag==true){ grf.drawImage(img_ball,xpos,ypos,this); xpos=xpos+xdir*xspeed; ypos=ypos+ydir*yspeed; } // //ラケットの表示 // grf.setColor(new Color(170,170,170)); grf.fillRect(raket_xpos,raket_ypos,raket_width,15); grf.setColor(new Color(192,192,192)); grf.fillRect(235,0,d.width,d.height); grf.setColor(Color.blue); g.drawImage(offs, 0, 0, this); // //ボールの方向変換判定 // if(ypos < 0){ //上の枠に当たったら ydir=ydir*-1; } if((xpos <= 0) || (xpos >= 233-dd)){ //サイドの枠に当たったら xdir=xdir*-1; } if((ypos + dd > raket_ypos) //ラケットに当たったら && (xpos+dd > raket_xpos) && (xpos < raket_xpos+raket_width) && (ypos+dd < raket_ypos+15) && (ydir==1)){ ydir=ydir*-1; } if(ypos >= d.height-30) { //ラケットに当たらなかったら menber--; acp1.stop(); acp1.play(); xpos=d.width/2; ypos=d.height-50; mdownflag=false; if(menber < 1){ startgame(); } } if(ypos > d.height){ xpos=100; ypos=raket_ypos-dd; } //ブロックの下面にあたった時の処置 hitblock1: for(int i=0 ; i < 48;i++){ if(flag[i]==1){ if(( ypos <= block_ypos[i]+block_height) &&(ypos >=block_ypos[i]+block_height-dd) &&(xpos >= block_xpos[i]-dd/2) &&(xpos <=block_xpos[i]+block_width-dd/2) &&(ydir==-1)){ ydir=ydir*-1; flag[i]=0; hit(); break hitblock1; } } } //ブロックの上面にあたった時の処置 hitblock2: for(int i=0 ; i < 48;i++){ if(flag[i]==1 && hit_flag==0){ if(( ypos >= block_ypos[i]-dd) &&(ypos <= block_ypos[i]) &&(xpos >= block_xpos[i]-dd/2) &&(xpos <=block_xpos[i]+block_width-dd/2) &&(ydir==1)){ ydir=-1; flag[i]=0; hit(); break hitblock2; } } } //ブロックの右面にあたった時の処置 hitblock3: for(int i=0 ; i < 48;i++){ if(flag[i]==1 && hit_flag==0 ){ if(( ypos >= block_ypos[i]-dd/2) &&(ypos <= block_ypos[i]+block_height-dd/2) &&(xpos <=block_xpos[i]+block_width) &&(xpos > block_xpos[i]) &&(xdir==-1)){ xdir=1; flag[i]=0; hit(); break hitblock3; } } } //ブロックの左面にあたった時の処置 hitblock4: for(int i=0 ; i < 48;i++){ if(flag[i]==1 && hit_flag==0 ){ if(( ypos >= block_ypos[i]-dd/2) &&(ypos <= block_ypos[i]+block_height-dd/2) &&(xpos >=block_xpos[i]-dd) &&(xpos < block_xpos[i]+block_width) &&(xdir==1)){ xdir=-1; flag[i]=0; hit(); break hitblock4; } } } check_clear(); } //--------------------------------------------------------// public void startgame(){ for(int i=0; i<48 ; i++){ flag[i]=1; } menber=3; if( counter > kiroku && menber==3 ){ kiroku=counter; } xspeed=5; yspeed=8; raket_width=40; counter=0; startflag=0; } //---------------------------------------------------------// public void check_clear(){ int notclear=0; for(int i=0; i<48 ; i++){ if(flag[i]==1){ notclear=1; break; } } if(notclear != 1){ xpos=100; ypos=raket_ypos-dd; mdownflag=false; for(int i=0; i<48 ; i++){ flag[i]=1; } } } //---------------------------------------------------------------// public boolean mouseMove(Event evt,int x,int y){ if(x > d.width) x=d.width; raket_xpos=x-raket_width/2; return true; } //----------------------ラケットがボールに当たったときの処理-------------------------// public void hit() { hit_flag=1; counter++; if(counter >= 40){ xspeed=7; yspeed=10; raket_width=30; } else if( counter >= 30){ xspeed=6; yspeed=9; raket_width=35; } else { xspeed=5; yspeed=8; raket_width=40; } acp.play(); } public boolean action(Event e,Object o){ if(e.target instanceof Button){ if("Write Record".equals(o) ){ if(kiroku !=0){ transmit(); text1.setText(""); receive(); } return true; } if("Reload".equals(o) ){ text1.setText(""); receive(); return true; } } return true; } //---------------------------------------------------------------// public boolean mouseDown(Event evt,int x,int y){ if((mdownflag != true)&&(x < 225)){ mdownflag=true; xpos=x; ypos=raket_ypos-dd; xdir=-1; ydir=-1; startflag=1; return true; } return false; } //---------------------------------------------------------------------------// public void start() { if (kicker == null) { kicker = new Thread(this); kicker.start(); } } //----------------------------------------------------------------------------// public void stop() { if (kicker != null) { kicker.stop(); kicker = null; } } /********** transmit **********/ void transmit() { try { // ホスト上のデータファイルに書き出す(CGI経由) URL host; // URLを作成 host = new URL(getDocumentBase(), "./block-wr.cgi"); URLConnection connection; // URLConnectionを作成 connection = host.openConnection(); connection.setDoInput(true); // 入力を許可 connection.setDoOutput(true); // 出力を許可 PrintStream outstream; // 出力ストリームを作成 outstream = new PrintStream(connection.getOutputStream()); String wrdata; // 書込データを作成 wrdata = tf1.getText()+ " ... " + kiroku + "\n"; wrdata = URLEncoder.encode(wrdata); outstream.print("data=" + wrdata); // データを出力 outstream.close(); // 出力ストリームを閉じる DataInputStream instream; // 入力ストリームを作成 instream = new DataInputStream(connection.getInputStream()); String rddata; // データを入力(ダミー) while ((rddata = instream.readLine()) != null) { } instream.close(); // 入力ストリームを閉じる } catch (MalformedURLException murle) { System.out.println("MalformedURLException -->" + murle); } catch (IOException ioe) { System.out.println("IOException -->" + ioe); } } /********** receive **********/ void receive() { try { // ホスト上のデータファイルを読み出す URL datafile; // URLを作成 datafile = new URL(getDocumentBase(), "block.dat"); InputStream is; // 入力ストリームを開く is = datafile.openStream(); DataInputStream dis; // データ入力ストリームを作成 dis = new DataInputStream(is); String rddata; // ストリームからデータを取り込む while ((rddata = dis.readLine()) != null) { rddata = rddata.replace('\u0000', '&'); // データ内の無効文字#00を"&"に置換する text1.insertText(rddata + "\n", 0); // (テキストエリアに1行毎に書き出す) } dis.close(); // ストリームを閉じる } catch (MalformedURLException murle) { System.out.println("MalformedURLException --> " + murle); } catch (IOException ioe) { System.out.println("IOException --> " + ioe); } } }