//S02137 参考作品 import jp.ac.tuis.basic.*; public class Hit2{ public static void main(String[] args){ //初期化 BasicGraphics g = new BasicGraphics(); int WIDTH=40; int HEIGHT=20; int TOTALTARGET = 10; //標的の捕獲目標回数 int computer_x; int computer_y; int player_x=WIDTH/2; int player_y=HEIGHT-2; char player_dir=0; //プレイヤーの進行方向 int score = 0; //現在の標的の捕獲回数 int time = 0; //現在の時間(ループ数) double tmp; long SLEEP=100; char pl='A'; //プレイヤー表示 g.locate(player_x,player_y); g.color(g.CYAN); g.print('A'); //標的表示 computer_x = (int)(Math.random()*(WIDTH-2)); computer_y = (int)(Math.random()*(HEIGHT-2)); g.locate(computer_x, computer_y); g.color(g.GREEN); g.print('X'); while(g.inkey() == 0){} // 何かキーを押したらゲーム開始 //アニメーション開始 while(true){ time++; //ループ回数を追加 if ((time%4==0)||((score >= TOTALTARGET/2)&&(time%2==0))||(score >= TOTALTARGET*.75)){ //標的移動 g.locate(computer_x,computer_y); g.print((char)0); //前の座標のプレイヤーを消す tmp=Math.random(); if(tmp < .25 && computer_x > 0){ computer_x--; } if(tmp >= .25 && tmp <.5 && computer_x < WIDTH-2){ computer_x++; } if(tmp >=.5 && tmp<.75 && computer_y > 0){ computer_y--; } if(tmp >=.75 && computer_y < HEIGHT-2){ computer_y++; } g.locate(computer_x,computer_y); if (score >= TOTALTARGET/2){g.color(g.YELLOW); if (score >= TOTALTARGET*.75){g.color(g.RED);} }else{ g.color(g.GREEN); } g.print('X'); if((player_x == computer_x) && (player_y == computer_y)){ //当り score++; if(score > TOTALTARGET){ //標的を目標回数捕らえた break; //終了 } //新しい標的表示 computer_x = (int)(Math.random()*(WIDTH-2)); computer_y = (int)(Math.random()*(HEIGHT-1)); g.locate(computer_x, computer_y); if (score >= TOTALTARGET/2){g.color(g.YELLOW); if (score >= TOTALTARGET*.75){g.color(g.RED);} }else{ g.color(g.GREEN); } g.print('X'); } } //プレイヤー移動 g.locate(player_x,player_y); g.print((char)0); //前の座標のプレイヤーを消す char key = g.inkey(); if(key == 'j' || key =='l' || key == 'i' || key == 'k'){ player_dir=key; } if(player_dir == 'j' && player_x > 0){ player_x--; pl='<'; } if(player_dir == 'l' && player_x < WIDTH-2){ player_x++; pl='>'; } if(player_dir == 'i' && player_y > 0){ player_y--; pl='A'; } if(player_dir == 'k' && player_y < HEIGHT-1){ player_y++; pl='V'; } g.locate(player_x,player_y); g.color(g.CYAN); g.print(pl); if((player_x == computer_x) && (player_y == computer_y)){ //当り score++; if(score > TOTALTARGET){ //標的を目標回数捕らえた break; //終了 } //新しい標的表示 computer_x = (int)(Math.random()*(WIDTH-2)); computer_y = (int)(Math.random()*(HEIGHT-2)); g.locate(computer_x, computer_y); g.locate(computer_x,computer_y); if (score >= TOTALTARGET/2){g.color(g.YELLOW); if (score >= TOTALTARGET*.75){g.color(g.RED);} }else{ g.color(g.GREEN); } g.print('X'); } g.sleep(SLEEP); }//end while(true) g.locate(WIDTH/2, HEIGHT/2); g.color(g.WHITE); g.print("TIME:"); g.print(time); }//end main }//end class Hit2