ぷよぷよのコードを改造しています。
配列の一番下に行くと、降ってくるブロックが消えるというものに挑戦しています。
以下にコードを貼ります。
java
1import java.awt.*; 2import java.awt.event.*; 3import javax.swing.ImageIcon; 4import javax.swing.*; 5import javax.swing.JButton; 6import javax.swing.JLabel; 7import javax.swing.JFrame; 8 9class YPanel extends Panel { 10 //Panel innerPanel3; 11 //Panel innerPanel4; 12 Color bgcolor; 13 static int puyomatrix[][]; 14 // static Panel mypanels[][]; 15 //int i,Y; 16 public YPanel() { 17 18 bgcolor = super.getBackground();//親クラスのBackgroundを参照しているらしい 19 } 20 public void setBounds(int x, int y, int width, int height) { 21 super.setBounds(x,y,width,height); 22 // innerPanel3.setBounds(4,height-4,width-8,8); 23 // innerPanel4.setBounds(width-8,4,8,height-8); 24 } 25 public Color getBackground() { 26 return bgcolor; 27 } 28 public void setBackground(Color color) { 29 bgcolor = color; 30 super.setBackground(color);//一連の動き確認 31 } 32} 33 34public class shooting2 { 35 static JLabel mylabel; 36 static Panel mypanels[][]; 37 static int puyomatrix[][]; 38 static Color colorList[]; 39 static int score = 0; 40 static int puyoX = 3, puyoY = 1;//上の方のブロックのx座標とY座標 41 //static int puyoX2 = 3, puyoY2 = 2; 42 //static int puyoX = 9, puyoY = 2; 43 //static int puyoX2 = 9, puyoY2 = 3; 44 static int color3 = (int)(Math.random()*3)+2; 45 static int color4 = (int)(Math.random()*3)+2; 46 static int color1 = 0, color2 = 0; 47 //static int rotate = 0; 48 static boolean lock = false; 49 static boolean lock2 = false; 50 51 static JButton[][] Btn;//ボタン 52 53 54 /* static int puyoX3=9; 55 static int puyoY3=2; 56 static int puyoX4=9; 57 static int puyoY4=3;*/ 58 59 public static void main(String args[]) { 60 int i,x,y,j,q=0; 61 final int puyoSize = 32; 62 63 64 JFrame myframe = new JFrame("ウンチ撃退ゲーム"); 65 myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 66 myframe.setSize(470, 580); 67 myframe.setLocationRelativeTo(null); 68 JLabel background = new JLabel(new ImageIcon("./png/6.png")); 69 myframe.add(background); 70 myframe.setVisible(true); 71 72 73 74 ImageIcon labelicon = new ImageIcon("./png/4.png");//背景? 75 mylabel = new JLabel(labelicon); 76 mylabel.setLayout(null); // does not use layout manager 77 mylabel.setSize(400,600); // window size : width = 400, height = 600 78 mylabel.setVisible(true); // make the window visible 79 //mylabel.addWindowListener(new MyWindowAdapter()); 80 81 82 ImageIcon buttonicon = new ImageIcon("./png/1.png");//配列上に出る、透明のやつ 83 Btn = new JButton[13][6];//真ん中 84 for( j = 0; j < 13; j ++) { 85 for( q = 0; q < 6; q ++) { 86 Btn[j][q] = new JButton(buttonicon); 87 myframe.add(Btn[j][q]);//myframeにBtnを加える 88 Color transparent = new Color(0, 0, 0, 0); 89 Btn[j][q].setBackground(transparent); 90 Btn[j][q].setBounds(80+(32*q),200+(32*j),32,32); 91 System.out.print(Btn[j][q]); 92 } 93 } 94 95 96 97 98 mypanels = new Panel[13][6]; 99 puyomatrix = new int[13][6]; 100 colorList = new Color[5]; 101 colorList[0] = Color.gray; 102 colorList[1] = Color.white; 103 colorList[2] = Color.yellow; 104 colorList[3] = Color.red; 105 colorList[4] = Color.green; 106 107 for( x = 0; x < 6; x++ ) { 108 for( y = 0; y < 13; y++ ) { 109 Panel p = new YPanel(); 110 mypanels[y][x] = p; 111 myframe.add(p); 112 p.setBounds(80+puyoSize*x,200+puyoSize*y,puyoSize,puyoSize); 113 setpuyo(x,y,0); // no puyo is here 114 } 115 } 116 for( x = 0; x < 6; x++ ) { 117 setpuyo(x,12,1); // ground is here、 この値を221行目の仮引数に代入 118 } 119 120 121 122 boolean firstPlacing = true; 123 boolean gameIsOver = false; 124 int fallCount = 0; 125 126 while( gameIsOver != true) { 127 lock = false; 128 sleep(100);//降りてくるのが遅くなる 129 lock = true; 130 while( lock2 ) { sleep(10); } 131 if( firstPlacing ) { 132 firstPlacing = false; 133 color1 = (int)(Math.random()*3)+2; 134 //color2 = (int)(Math.random()*3)+2; 135 puyoX = 3; puyoY = 1; // initialPlace 136 137 if( puyomatrix[puyoY][puyoX] == 0//上のぷよの座標が0になる=上に当たってしまった 138 ) {//下の‥↑ 139 setpuyo(puyoX,puyoY,0); 140 //setpuyo(puyoX2,puyoY2,color2); 141 //System.out.print("next puyo is here.\n"); 142 } else { 143 // GAME OVER 144 // ImageIcon icon7 = new ImageIcon("./img/7.png"); 145 setpuyo(puyoX,puyoY,0); 146 //setpuyo(puyoX2,puyoY2,color2); 147 System.out.print("batan kyu-\n"); 148 gameIsOver = true; 149 System.exit(0); 150 } 151 } 152 if( fallCount > 5 ) { 153 fallCount = 0; // counter reset; 154 puyoY++; // fall it for one block 155 //puyoY2++; 156 if( puyomatrix[puyoY][puyoX] == 0) 157 { 158 setpuyo(puyoX,puyoY-1,0); 159 //setpuyo(puyoX2,puyoY2-1,0); 160 setpuyo(puyoX,puyoY,color1); 161 //setpuyo(puyoX2,puyoY2,color2); 162 } else { 163 firstPlacing = true; 164 // undo falling 165 puyoY--; 166 // puyoY2--; 167 // fall each puyo 168 while(puyomatrix[puyoY+1][puyoX] == 0) { 169 setpuyo(puyoX,puyoY,0); 170 puyoY++; 171 setpuyo(puyoX,puyoY,0); 172 } 173 /*while(puyomatrix[puyoY2+1][puyoX2] == 0) { 174 setpuyo(puyoX2,puyoY2,0); 175 puyoY2++; 176 setpuyo(puyoX2,puyoY2,color2); 177 } 178 */ 179 // clear connected puyos and falldown other puyos... 180 // !!--check here--!! 181 182 // !!--check here--!! 183 } 184 } else { 185 fallCount++; 186 } 187 } 188 189} 190 191 192static void setpuyo(int x, int y, int color) { 193 puyomatrix[y][x] = color; 194 mypanels[y][x].setBackground(colorList[color]); 195} 196static int getpuyo(int x, int y) { // why don't you use this!? 197// System.out.print(puyomatrix[y][x]); 198return puyomatrix[y][x]; 199} 200 201static void fallPuyos() { 202 int fallcount; 203 int nx,ny,nr; 204 205 do { 206 fallcount = 0; 207 int x,y; 208 for( y = 12; y > 0; y-- ) { 209 for( x = 1; x <= 6; x++ ) { 210 if( puyomatrix[y][x] != 0 && puyomatrix[y+1][x] == 0 ) { 211 setpuyo(x,y+1,puyomatrix[y][x]); // fall the puyo 212 setpuyo(x,y,0); // the last position should be empty 213 fallcount++; // count-up fallout counter 214 } 215 } 216 } 217 } while( fallcount != 0 ); 218 219} 220 221 222static void sleep(long msec) { 223 try{ 224 Thread.sleep(msec);//指定した時間の長さにわたって現在のスレッドを中断 225 }catch(InterruptedException ie) { 226 } 227} 228} 229
自分は
java
1setpuyo(puyoX,puyoY,0);
も用いることで出来ると考えていました。ですが、ぷよぷよのように降ってきているブロックが積み重なってしまいます。どうすれば思うように動くでしょうか。よろしくお願いします。
追記
自分の力不足なんですけど、その場しのぎでコードを書いていたために、背景などに画像を挿入しないと動きません。ごめんなさい。
読み解くのが手間だったので説明が欲しいのですが、
puyomatrix[][]と、colorの役割について説明願えますか?
現状、該当ブロックの色をなくすことで「空きブロック」を表現してるのかな?
て感じで見てますが、そこらへんの説明があったほうが回答付きやすいので。
その通りです。puyomatrix[][]は、ブロックの色情報を格納していて、指定のブロックの色がない=空ブロックちうことです。 よろしくお願いします。
配列の一番下に行くと、降ってくるブロックが消える、とありますが
私の想像している「同色を揃えたら消える落ちゲー」とかではなくて、
「最下段に落ちたらそれ以上降ってこなくなる」と読み取れるのですが、合ってます?
ああ、僕の日本語がダメでした。
もともと降ってきているブロックをクリックして消す動作がほしいんですけど、クリックできずに配列の一番下にブロックが当たってしまったらそのブロックも同様に消して、当たってしまった分をカウントするという形を目指しています。
なので、配列の一番下にブロックが当たってしまっても、まだブロックは上から降ってきます。
手間かけて申し訳ありません。 もうちょっと確認を。
現状、動作させると
①1個目のぷよAが降ってくる
②ぷよAが最下段に到達する (←消したいけど消えない)
③2個目のぷよBが降ってくる
④ぷよBがぷよAの頭上に乗っかる
ということでしょうか?
いやいや、こちらこそすいません。
その通りです。
なるほど, 「降ってきているブロックをクリックして消す動作」ということでボタンの2次元配列があるんですね. ボタンの配列(Btn[][])も使われていないので, なんだろうと思っていました.
分かりにくくてすいません。。。ボタンを配列にすればクリックしたところで消す動作が実現できると思いました。
回答2件
あなたの回答
tips
プレビュー