前提
論プロエディタを利用してゲームを作っています。ネズミがプレイヤーでチーズに当たると得点が増えて、猫に当たると増えない、という風にしています。
実現したいこと
コード自体はすべて完成してコンパイルにも成功しています。このコードを可能な限りメソッドに分けて視覚的に簡潔にしたいです。
ウィンドウのサイズとワープのメソッド化。BGM作成のメソッド化。
上の二つについてはメソッドに分けられました。(以下、ここまでのコード)
java
1public class CatAndRat extends Turtle { 2 public static void main( String[] args){ 3 Turtle.startTurtle(new CatAndRat(),args); 4 } 5 public void start() { 6 System.out.println("CatAndRat: Version 8"); 7 hide(); 8//得点版生成 9 int score = 0; 10 TextTurtle scoreBoard = new TextTurtle(""); 11 scoreBoard.warp(600,50); 12//画面サイズ設定 13 window(); 14//キャラ生成 15 ImageTurtle nezumi = new ImageTurtle("nezumi.png"); 16 ImageTurtle cat1 = new ImageTurtle("cat.png"); 17 ImageTurtle cat2 = new ImageTurtle("cat.png"); 18 ImageTurtle cat3 = new ImageTurtle("cat.png"); 19 ImageTurtle cat4 = new ImageTurtle("cat.png"); 20 ImageTurtle cheese1 = new ImageTurtle("cheese.png"); 21 ImageTurtle cheese2 = new ImageTurtle("cheese.png"); 22 ImageTurtle cheese3 = new ImageTurtle("cheese.png"); 23 ImageTurtle cheese4 = new ImageTurtle("cheese.png"); 24 ImageTurtle cheese5 = new ImageTurtle("cheese.png"); 25 ImageTurtle cheese6 = new ImageTurtle("cheese.png"); 26 ImageTurtle cheese7 = new ImageTurtle("cheese.png"); 27//音楽 28 makesound(); 29 SoundTurtle hitc = new SoundTurtle("hitc.mp3"); 30 hitc.loadOnMemory(); 31 SoundTurtle hitcat = new SoundTurtle("hitcat.mp3"); 32 hitcat.loadOnMemory(); 33//初期値のワープ 34 cat1.warp(random(640),0); 35 cat2.warp(random(640),0); 36 cat3.warp(random(640),0); 37 cat4.warp(random(640),0); 38 cheese1.warp(250,0); 39 cheese2.warp(320,0); 40 cheese3.warp(420,0); 41 cheese4.warp(520,0); 42 cheese5.warp(150,0); 43 cheese6.warp(80,0); 44 cheese7.warp(600,0); 45 nezumi.warp(320,350); 46 47 while(true){ 48 49 if(key() == 37){ 50 nezumi.warp(nezumi.x() - 5,nezumi.y()); 51 }else if(key() == 39){ 52 nezumi.warp(nezumi.x() + 5,nezumi.y()); 53 } 54//待機 55 sleep(0.025); 56//ワープ先の設定 57 cat1.warp(cat1.x(),cat1.y() + 5); 58 cat2.warp(cat2.x(),cat2.y() + 5); 59 cat3.warp(cat3.x(),cat3.y() + 5); 60 cat4.warp(cat4.x(),cat4.y() + 5); 61 cheese1.warp(cheese1.x(),cheese1.y() + 5); 62 cheese2.warp(cheese2.x(),cheese2.y() + 5); 63 cheese3.warp(cheese3.x(),cheese3.y() + 5); 64 cheese4.warp(cheese4.x(),cheese4.y() + 5); 65 cheese5.warp(cheese5.x(),cheese5.y() + 5); 66 cheese6.warp(cheese6.x(),cheese6.y() + 5); 67 cheese7.warp(cheese7.x(),cheese7.y() + 5); 68//下まで行ったら上に行く 69 if(cat1.y() > 480){ 70 cat1.warp(cat1.x(),0); 71 } 72 if(cat2.y() > 600){ 73 cat2.warp(cat2.x(),0); 74 } 75 if(cat3.y() > 500){ 76 cat3.warp(cat3.x(),0); 77 } 78 if(cat4.y() > 700){ 79 cat4.warp(cat4.x(),0); 80 } 81 if(cheese1.y() > 600){ 82 cheese1.warp(cheese1.x(),0); 83 } 84 if(cheese2.y() > 500){ 85 cheese2.warp(cheese2.x(),0); 86 } 87 if(cheese3.y() > 700){ 88 cheese3.warp(cheese3.x(),0); 89 } 90 if(cheese4.y() > 800){ 91 cheese4.warp(cheese4.x(),0); 92 } 93 if(cheese5.y() > 480){ 94 cheese5.warp(cheese5.x(),0); 95 } 96 if(cheese6.y() > 900){ 97 cheese6.warp(cheese6.x(),0); 98 } 99 if(cheese7.y() > 550){ 100 cheese7.warp(cheese7.x(),0); 101 } 102 103//ネズミが当たった時 104 if(cat1.intersects(nezumi)){ 105 hitcat.play(); 106 } 107 if(cat2.intersects(nezumi)){ 108 hitcat.play(); 109 } 110 if(cat3.intersects(nezumi)){ 111 hitcat.play(); 112 } 113 if(cat4.intersects(nezumi)){ 114 hitcat.play(); 115 } 116 if(cheese1.intersects(nezumi)){ 117 score++; 118 hitc.play(); 119 } 120 if(cheese2.intersects(nezumi)){ 121 score++; 122 hitc.play(); 123 } 124 if(cheese3.intersects(nezumi)){ 125 score++; 126 hitc.play(); 127 } 128 if(cheese4.intersects(nezumi)){ 129 score++; 130 hitc.play(); 131 } 132 if(cheese5.intersects(nezumi)){ 133 score++; 134 hitc.play(); 135 } 136 if(cheese6.intersects(nezumi)){ 137 score++; 138 hitc.play(); 139 } 140 if(cheese7.intersects(nezumi)){ 141 score++; 142 hitc.play(); 143 } 144 scoreBoard.text(score); 145 update(); 146 } 147 } 148 public void window() { 149 window.size(640,480); 150 window.warp(100,100); 151 } 152 void makesound() { 153 SoundTurtle bgm = new SoundTurtle("pakkuwoman.mp3"); 154 bgm.play(); 155 } 156 157}
これ以外のコードをメソッドにできるだけ分けたいのですが、とりあえず以下のコードをメソッドに分けたくてやってみたところエラーが出ました。変数がうまくメソッドに渡せていないのだろうと思い、調べると「変数のデータ型 変数名」と書いてあったのでやってみたのですがエラーになりました。
この解決方法と、ほかにもメソッドに分けられる部分があれば教えていただきたいです。何か追記しなければならない部分等あればよろしくお願いします。
論プロエディタの物を質問されている方を見つけられなかったのですが、教えていただけると幸いです。初心者でわけわからないようなコードかもしれませんがよろしくお願いします。
該当のソースコード
メソッドに分けたい部分↓
java
1 2if(key() == 37){ 3 nezumi.warp(nezumi.x() - 5,nezumi.y()); 4}else if(key() == 39){ 5 nezumi.warp(nezumi.x() + 5,nezumi.y()); 6}
変更したもの↓
java
1public class CatAndRat extends Turtle { 2 public static void main( String[] args){ 3 Turtle.startTurtle(new CatAndRat(),args); 4 } 5 public void start() { 6 System.out.println("CatAndRat: Version 8"); 7 hide(); 8//得点版生成 9 int score = 0; 10 TextTurtle scoreBoard = new TextTurtle(""); 11 scoreBoard.warp(600,50); 12//画面サイズ設定 13 window(); 14//キャラ生成 15 ImageTurtle nezumi = new ImageTurtle("nezumi.png"); 16 ImageTurtle cat1 = new ImageTurtle("cat.png"); 17 ImageTurtle cat2 = new ImageTurtle("cat.png"); 18 ImageTurtle cat3 = new ImageTurtle("cat.png"); 19 ImageTurtle cat4 = new ImageTurtle("cat.png"); 20 ImageTurtle cheese1 = new ImageTurtle("cheese.png"); 21 ImageTurtle cheese2 = new ImageTurtle("cheese.png"); 22 ImageTurtle cheese3 = new ImageTurtle("cheese.png"); 23 ImageTurtle cheese4 = new ImageTurtle("cheese.png"); 24 ImageTurtle cheese5 = new ImageTurtle("cheese.png"); 25 ImageTurtle cheese6 = new ImageTurtle("cheese.png"); 26 ImageTurtle cheese7 = new ImageTurtle("cheese.png"); 27//音楽 28 makesound(); 29 SoundTurtle hitc = new SoundTurtle("hitc.mp3"); 30 hitc.loadOnMemory(); 31 SoundTurtle hitcat = new SoundTurtle("hitcat.mp3"); 32 hitcat.loadOnMemory(); 33//初期値のワープ 34 cat1.warp(random(640),0); 35 cat2.warp(random(640),0); 36 cat3.warp(random(640),0); 37 cat4.warp(random(640),0); 38 cheese1.warp(250,0); 39 cheese2.warp(320,0); 40 cheese3.warp(420,0); 41 cheese4.warp(520,0); 42 cheese5.warp(150,0); 43 cheese6.warp(80,0); 44 cheese7.warp(600,0); 45 nezumi.warp(320,350); 46 47 while(true){ 48 ke(ImageTurtle nezumi); 49 //if(key() == 37){ 50 //nezumi.warp(nezumi.x() - 5,nezumi.y()); 51 //}else if(key() == 39){ 52 //nezumi.warp(nezumi.x() + 5,nezumi.y()); 53 //} 54//待機 55 sleep(0.025); 56//ワープ先の設定 57 cat1.warp(cat1.x(),cat1.y() + 5); 58 cat2.warp(cat2.x(),cat2.y() + 5); 59 cat3.warp(cat3.x(),cat3.y() + 5); 60 cat4.warp(cat4.x(),cat4.y() + 5); 61 cheese1.warp(cheese1.x(),cheese1.y() + 5); 62 cheese2.warp(cheese2.x(),cheese2.y() + 5); 63 cheese3.warp(cheese3.x(),cheese3.y() + 5); 64 cheese4.warp(cheese4.x(),cheese4.y() + 5); 65 cheese5.warp(cheese5.x(),cheese5.y() + 5); 66 cheese6.warp(cheese6.x(),cheese6.y() + 5); 67 cheese7.warp(cheese7.x(),cheese7.y() + 5); 68//下まで行ったら上に行く 69 if(cat1.y() > 480){ 70 cat1.warp(cat1.x(),0); 71 } 72 if(cat2.y() > 600){ 73 cat2.warp(cat2.x(),0); 74 } 75 if(cat3.y() > 500){ 76 cat3.warp(cat3.x(),0); 77 } 78 if(cat4.y() > 700){ 79 cat4.warp(cat4.x(),0); 80 } 81 if(cheese1.y() > 600){ 82 cheese1.warp(cheese1.x(),0); 83 } 84 if(cheese2.y() > 500){ 85 cheese2.warp(cheese2.x(),0); 86 } 87 if(cheese3.y() > 700){ 88 cheese3.warp(cheese3.x(),0); 89 } 90 if(cheese4.y() > 800){ 91 cheese4.warp(cheese4.x(),0); 92 } 93 if(cheese5.y() > 480){ 94 cheese5.warp(cheese5.x(),0); 95 } 96 if(cheese6.y() > 900){ 97 cheese6.warp(cheese6.x(),0); 98 } 99 if(cheese7.y() > 550){ 100 cheese7.warp(cheese7.x(),0); 101 } 102 103//ネズミが当たった時 104 if(cat1.intersects(nezumi)){ 105 hitcat.play(); 106 } 107 if(cat2.intersects(nezumi)){ 108 hitcat.play(); 109 } 110 if(cat3.intersects(nezumi)){ 111 hitcat.play(); 112 } 113 if(cat4.intersects(nezumi)){ 114 hitcat.play(); 115 } 116 if(cheese1.intersects(nezumi)){ 117 score++; 118 hitc.play(); 119 } 120 if(cheese2.intersects(nezumi)){ 121 score++; 122 hitc.play(); 123 } 124 if(cheese3.intersects(nezumi)){ 125 score++; 126 hitc.play(); 127 } 128 if(cheese4.intersects(nezumi)){ 129 score++; 130 hitc.play(); 131 } 132 if(cheese5.intersects(nezumi)){ 133 score++; 134 hitc.play(); 135 } 136 if(cheese6.intersects(nezumi)){ 137 score++; 138 hitc.play(); 139 } 140 if(cheese7.intersects(nezumi)){ 141 score++; 142 hitc.play(); 143 } 144 scoreBoard.text(score); 145 update(); 146 } 147 } 148 public void window() { 149 window.size(640,480); 150 window.warp(100,100); 151 } 152 void makesound() { 153 SoundTurtle bgm = new SoundTurtle("pakkuwoman.mp3"); 154 bgm.play(); 155 } 156 157 void ke(ImageTurtle nezumi) { 158 if(key() == 37){ 159 nezumi.warp(nezumi.x() - 5,nezumi.y()); 160 }else if(key() == 39){ 161 nezumi.warp(nezumi.x() + 5,nezumi.y()); 162 } 163 } 164}
以下、エラーメッセージ
CatAndRat.java:53: エラー: ')'がありません
ke(ImageTurtle nezumi);
^
CatAndRat.java:53: エラー: 文ではありません
ke(ImageTurtle nezumi);
^
CatAndRat.java:53: エラー: ';'がありません
ke(ImageTurtle nezumi);
^
エラー3個
回答1件