//対戦型ハイパーホッケーゲーム import java.awt.*; import java.awt.event.*; import java.util.*; class RacketPoint{//ラケットの座標を保持するクラス public int racket_x,racket_y; public RacketPoint(int x1,int y1){ racket_x=x1; racket_y=y1; } } class BallSpeed{//ボールスピードを保持するクラス public int ball_vx,ball_vy; public BallSpeed(int x2,int y2){ ball_vx=x2; ball_vy=y2; } } class HyperHockey2 extends Frame implements Runnable{ // ボールの座標,速度のx, y成分,最高速度 int b_x=300,b_y=360,b_vx=-3,b_vy=1,bv_max=30; // ボールの大きさ int b_size=30; // 画面の幅,高さ,余白 int width=400, height=720,yoko=20,shita=height-70,ue=50; int r_size=50;//ラケットサイズ int r_x=(width-r_size)/2, r_y=shita-r_size,r2_x=(width-r_size)/2,r2_y=ue;//ラケット座標 double br_size=(b_size+r_size)/2.0;//ボールとラケットの半径の和 int r_vx=0,r_vy=0,r2_vx=0,r2_vy=0,r2v_max=30;//ラケットの速度ベクトルの成分 int net_width=0;//センターラインを引くを引く演出用。 int string_x=-20;//これもオープニング演出用。 int mmx,mmy;//マウスの座標とラケットの座標間の差を表す変数 int t=0;//時間を管理する変数。 int mypoint=0,epoint=0;//得点 boolean grip=false,start=false,netmv=true,stringmv=true; Image image; Graphics graphic; Vector racketArray;//ラケット座標の配列。 public HyperHockey2(){ super(); racketArray= new Vector(); addKeyListener(new KeyAdapter(){ public void keyTyped(KeyEvent e){ System.out.println("keyTyped("+e+")"); int key=e.getKeyChar(); if(key=='s'){ start=true; } if(key=='q'){ System.exit(0); } } }); addMouseListener(new MouseAdapter(){ public void mousePressed(MouseEvent e){ int mx=e.getX(),my=e.getY(); System.out.println("mousePressed("+e+","+mx+","+my+")"); if(r_xbv_max){//ボールの速さの上限を設定 double b_vxd=(b_vx*bv_max)/bv_s,b_vyd=(b_vy*bv_max)/bv_s; b_vx=(int)b_vxd; b_vy=(int)b_vyd; } double o_length=o_Length(r_x,r_y),o2_length=o_Length(r2_x,r2_y);//この部分により、ラケットとボールが重なり合う現象を回避する。 if(o_lengthwidth-yoko-b_size){ b_x=(width-yoko-b_size)-(b_x-(width-yoko-b_size)); b_vx=-b_vx; } if(b_yshita-b_size){ if(100-b_size/2width-yoko*2-20){ netmv=false;} } void moveString(){//演出 int old_string_x=string_x; graphic.setColor(Color.yellow); graphic.drawString(mypoint+" - "+epoint,old_string_x,height-10); string_x+=20; graphic.setColor(Color.red); graphic.drawString(mypoint+" - "+epoint,string_x,height-10); if(string_x>300){ stringmv=false; } } void setRacket(){//ラケットの配置 if(r_xwidth-yoko-r_size){ r_x=width-yoko-r_size; } if(r_yshita-r_size){ r_y=shita-r_size; } graphic.setColor(Color.green); graphic.fillOval(r_x,r_y,r_size,r_size); } void setRacket2(){//CPUのラケットの動き double bv_s=length(b_vx,b_vy);//ボールの速さ if(bv_s>25){ if(b_vy<0){ r2_vx=b_vx; if(r_yr2v_max){ r2_vx=(int)(Math.sqrt(r2v_max*r2v_max-r2_vy*r2_vy)*b_vx/Math.sqrt(b_vx*b_vx)); }} else{ int ax=(width-r_size)/2-r2_x; int ay=ue-r2_y; double a_length=length(ax,ay); r2_vx=(int)(ax*r2v_max/a_length); r2_vy=(int)(ay*r2v_max/a_length); } } else{ if(b_y>height/2){ int ax=(width-r_size)/2-r2_x; int ay=ue-r2_y; double a_length=length(ax,ay); r2_vx=(int)(ax*r2v_max/a_length); r2_vy=(int)(ay*r2v_max/a_length); } else{ if(b_y>r2_y){ int bx=b_x-r2_x; int by=b_y-r2_y; double b_length=length(bx,by); r2_vx=(int)(bx*r2v_max/b_length); r2_vy=(int)(by*r2v_max/b_length); } else{ r2_vy=-10; } } } r2_x=r2_x+r2_vx; r2_y=r2_y+r2_vy; if(r2_xwidth-yoko-r_size){ r2_x=(width-yoko-r_size)-(r2_x-(width-yoko-r_size)); } if(r2_yheight/2-r_size){ r2_y=height/2-r_size; } graphic.fillOval(r2_x,r2_y,r_size,r_size); } void hitEvent(){//ヒットをチェックし、ヒットならばイベントを起こす racketArray.addElement(new RacketPoint(r_x,r_y));//自ラケット座標の配列(ラケットスピード算出のため) double o_length=o_Length(r_x,r_y),o2_length=o_Length(r2_x,r2_y); if(o_length=0){//ちゃんと衝突しているならば計算する double keisuu=2*(p-q);//ボール速度を求める式で使う係数 double a_bvx=(keisuu*ox)/(br_size*br_size),a_bvy=(keisuu*oy)/(br_size*br_size);//実数値でボールの速度変化を求める int add_bvx=(int)a_bvx; int add_bvy=(int)a_bvy; bb_vx=b_vx+add_bvx; bb_vy=b_vy+add_bvy; } else{//ボールとラケットがただ接しているだけならば速度は変化しない bb_vx=b_vx; bb_vy=b_vy; } BallSpeed bbv=new BallSpeed(bb_vx,bb_vy); return bbv; } void losePoint(){//失点イベント epoint+=1; setField(); graphic.setColor(Color.white); graphic.setFont(new Font("Impact",Font.ITALIC,60)); graphic.drawString("GOAL!!",100,200); graphic.setColor(Color.cyan); graphic.drawString("-",185,270); graphic.setColor(Color.white); graphic.drawString(mypoint+"",yoko+120,270); for(int i=0;i<130;i=i+10){//得点表示を演出。 try{Thread.sleep(10);} catch(Exception e){} graphic.setColor(Color.red); graphic.drawString(epoint+"",width-yoko-i-40,270); repaint(); if(i==120){break;} graphic.setColor(Color.blue); graphic.drawString(epoint+"",width-yoko-i-40,270); } try{Thread.sleep(2000);} catch(Exception e){} if(epoint==7&&mypoint<7){//負けたなら敗北イベントを起こす loseEvent(); } b_x=300; b_y=360; b_vx=-3; b_vy=1; r_x=(width-r_size)/2; r_y=shita-r_size; r2_x=(width-r_size)/2; r2_y=ue; r2_vx=0; r2_vy=0; setField(); graphic.setFont(new Font("Impact",Font.ITALIC,50)); graphic.setColor(Color.red); graphic.drawString("START!",80,200); repaint(); try{Thread.sleep(700);} catch(Exception e){} } void getPoint(){//得点イベント mypoint+=1; setField(); graphic.setColor(Color.white); graphic.setFont(new Font("Impact",Font.ITALIC,60)); graphic.drawString("GOAL!!",100,200); graphic.setColor(Color.cyan); graphic.drawString("-",185,270); graphic.setColor(Color.white); graphic.drawString(epoint+"",width-yoko-160,270); for(int i=0;i<130;i=i+10){ try{Thread.sleep(10);} catch(Exception e){} graphic.setColor(Color.red); graphic.drawString(mypoint+"",yoko+i,270); repaint(); if(i==120){break;} graphic.setColor(Color.blue); graphic.drawString(mypoint+"",yoko+i,270); } try{Thread.sleep(2000);} catch(Exception e){} if(mypoint==7&&epoint<7){//勝ったなら勝利イベントを起こす winEvent(); } b_x=300; b_y=320-b_size; b_vx=-3; b_vy=1; r_x=(width-r_size)/2; r_y=shita-r_size; r2_x=(width-r_size)/2; r2_y=ue; r2_vx=0; r2_vy=0; setField(); graphic.setFont(new Font("Impact",Font.ITALIC,50)); graphic.setColor(Color.red); graphic.drawString("START!",80,200); repaint(); try{Thread.sleep(700);} catch(Exception e){} } void winEvent(){//勝利イベント setField(); graphic.setColor(Color.white); graphic.setFont(new Font("Impact",Font.ITALIC,30)); graphic.drawString("Score: ",yoko+60,400); graphic.setFont(new Font("Impact",Font.ITALIC,70)); graphic.setColor(Color.red); graphic.drawString(mypoint+"",yoko+150,400); graphic.setColor(Color.cyan); graphic.drawString(" -",yoko+175,400); graphic.setColor(Color.white); graphic.drawString(epoint+"",yoko+235,400); boolean black=true; for(int i=0;i<50;i=i+1){ try{Thread.sleep(200);} catch(Exception e){} if(black){ graphic.setColor(new Color(150,150,150)); black=false; } else{ graphic.setColor(new Color(255,255,0)); black=true; } graphic.setFont(new Font("Impact",Font.PLAIN,70)); graphic.drawString("YOU WIN!",70,270); repaint(); } System.exit(0); } void loseEvent(){//敗北イベント setField(); graphic.setColor(Color.white); graphic.setFont(new Font("Impact",Font.ITALIC,30)); graphic.drawString("Score: ",yoko+60,400); graphic.setFont(new Font("Impact",Font.ITALIC,70)); graphic.setColor(Color.white); graphic.drawString(mypoint+"",yoko+150,400); graphic.setColor(Color.cyan); graphic.drawString(" -",yoko+175,400); graphic.setColor(Color.red); graphic.drawString(epoint+"",yoko+235,400); graphic.setFont(new Font("Impact",Font.PLAIN,70)); graphic.drawString("YOU LOSE ...",70,270); repaint(); try{Thread.sleep(10000);} catch(Exception e){} System.exit(0); } } //以上。