質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

Q&A

解決済

1回答

1388閲覧

ぷよぷよを作っています

ringo08

総合スコア8

C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

0グッド

1クリップ

投稿2018/07/09 20:25

前提・実現したいこと

C言語でぷよぷよを作っているのですが、
ぷよが綺麗に落ちてくれません。

発生している問題・エラーメッセージ

ぷよが落ちると何マスか上に配置されます

該当のソースコード

C言語

1ソースコード 2 3#include <stdio.h> 4#include <handy.h> 5#include <stdlib.h> 6#include <time.h> 7 8#define WINDOW_X 600 9#define WINDOW_Y 700 10#define FIELD_H 14 11#define FIELD_W 8 12#define PLAYMAT_H 13 13#define PLAYMAT_W 6 14#define PEACE_H 3 15#define PEACE_W 3 16#define SIZE 50 17#define SPEED 50 18 19int main(){ 20 int puyofield[FIELD_W][FIELD_H] = {0}; // 画面のピクセル 21 int puyopeace[PEACE_W][PEACE_H] = {0}; // 予備ぷよのフィールド 22 int puyomath[FIELD_W][FIELD_H] = {0}; // プレイ画面のフィールド 23 int radius = SIZE/2; // ぷよの半径 24 int random_wide, random_color1, random_color2, pre_random_color3, pre_random_color4; // ランダム系変数 25 int main_x, main_y, sub_x, sub_y, move_wide; // 移動系変数 26 int i, j, p, q, s, t, a, b; // カウント変数 27 int speed, accel, sx, sy, pre_main, pre_sub, color, window, score; // その他変数 28 29 score = 0; 30 window = HgOpen(WINDOW_X, WINDOW_Y); 31 32 33 int layer1 = HgWAddLayer(0); 34 int layer2 = HgWAddLayer(0); 35 int field_layer[PLAYMAT_W+1][PLAYMAT_H+1]; // ぷよ用レイヤ 36 37 for(i=1; i<PLAYMAT_W+1; i++){ 38 for(j=1; j<PLAYMAT_H+1; j++){ 39 field_layer[i][j] = HgWAddLayer(0); // ぷよ用レイヤの作成 40 } 41 } 42 43 // field start 44 45 46 HgWSetFillColor(0, HG_BLACK); 47 HgWBoxFill(0, 0, 0, WINDOW_X, WINDOW_Y, 1); 48 49 for(i=1; i<FIELD_W; i++){ 50 for(j=0; j<FIELD_H; j++){ 51 puyofield[i][j] = j*SIZE; 52 } 53 } 54 55 HgWSetFillColor(0, HG_WHITE); 56 HgWSetWidth(0, 0.3); 57 for(i=1; i<=PLAYMAT_W; i++){ 58 for(j=1; j<PLAYMAT_H; j++){ 59 HgWBoxFill(0, i*SIZE, puyofield[i][j], SIZE, SIZE, 1); 60 } 61 } 62 63 for(i=0; i<PEACE_W; i++){ 64 for(j=0; j<PEACE_H; j++){ 65 puyopeace[i][j] = (j+10)*SIZE; 66 HgWBoxFill(0, (i+8)*SIZE, puyopeace[i][j], SIZE, SIZE, 1); 67 } 68 } 69 70 // field end 71 72 // contents start 73 74 HgWSetEventMask(layer1, HG_KEY_DOWN); 75 76 for(i=0; i<FIELD_W; i++){ 77 puyomath[i][0] = 1; 78 } 79 80 for(j=0; j<FIELD_H; j++){ 81 puyomath[0][j] = 1; 82 puyomath[7][j] = 1; 83 } 84 85 srand(time(NULL)); 86 87 random_color1 = rand()%4+1; // 最初の mainぷよ の色 88 random_color2 = rand()%4+1; // 最初の subぷよ の色 89 90 pre_main = (random_wide*SIZE+move_wide)/SIZE; // 初期のmainぷよの横軸 91 pre_sub = (random_wide*SIZE+move_wide)/SIZE; // 初期のsubぷよの横軸 92 93 for(;;){ 94 pre_random_color3 = rand()%4+1; // 次の mainぷよ の色 95 pre_random_color4 = rand()%4+1; // 次の subぷよ の色 96 97 random_wide = rand()%6+1; // ぷよの最初の横座標 98 99 move_wide = 0; 100 sx = 0; 101 sy = -1; 102 main_x = random_wide*SIZE + radius; // mainぷよのx座標 103 sub_x = random_wide*SIZE + radius; // subぷよのx座標 104 105 106// 次のぷよ表示 107 if(pre_random_color3==1) { 108 HgWSetFillColor(layer2, HG_RED); 109 }s 110 else if(pre_random_color3==2) { 111 HgWSetFillColor(layer2, HG_BLUE); 112 } 113 else if(pre_random_color3==3) { 114 HgWSetFillColor(layer2, HG_YELLOW); 115 } 116 else if(pre_random_color3==4) { 117 HgWSetFillColor(layer2, HG_GREEN); 118 } 119 HgWCircleFill(layer2, 9*SIZE+radius, 10*SIZE+radius, radius, 0); 120 121 122// 次のぷよ表示 123 if(pre_random_color4==1) { 124 HgWSetFillColor(layer2, HG_RED); 125 } 126 else if(pre_random_color4==2) { 127 HgWSetFillColor(layer2, HG_BLUE); 128 } 129 else if(pre_random_color4==3) { 130 HgWSetFillColor(layer2, HG_YELLOW); 131 } 132 else if(pre_random_color4==4) { 133 HgWSetFillColor(layer2, HG_GREEN); 134 } 135 HgWCircleFill(layer2, 9*SIZE+radius, 11*SIZE+radius, radius, 0); 136 137 138 pre_main = (random_wide*SIZE+move_wide)/SIZE; // mainぷよの横軸 139 pre_sub = (random_wide*SIZE+move_wide)/SIZE; // subぷよの横軸 140 141 p = 13; 142 q = 13; 143 t = 0; 144 145 for(;((puyomath[pre_main][p] == 0)||(puyomath[pre_sub+sx][q+sy] == 0));){ 146 147 hgevent *event = HgEventNonBlocking(); 148 if(event != NULL){ 149 if(event->type == HG_KEY_DOWN){ 150 switch(event->ch){ 151 case HG_D_ARROW : 152 if(puyomath[pre_main][p-2] == 0 && puyomath[pre_sub+sx][q+sy-2] == 0){ 153 p--; 154 q--; 155 } 156 break; 157 158 case HG_L_ARROW : 159 if((puyomath[pre_main-1][p-1]==0) || (puyomath[pre_sub-1][q-1]==0)) { 160 move_wide += -SIZE; 161 } 162 break; 163 164 case HG_R_ARROW : 165 if((puyomath[pre_main+1][p-1]==0) || (puyomath[pre_sub+1][q-1]==0)) { 166 move_wide += SIZE; 167 } 168 break; 169 170 case HG_U_ARROW : 171 t += 1; 172 if(t >= 4) t = 0; 173 break; 174 } 175 } 176 } 177 178 179 180 181 182 if(t==0) { 183 sx = 0; 184 sy = -1; 185 } 186 else if(t==1) { 187 sx = -1; 188 sy = 0; 189 } 190 else if(t==2) { 191 sx = 0; 192 sy = 1; 193 } 194 else if(t==3) { 195 sx = 1; 196 sy = 0; 197 } 198 199 sub_x = random_wide*SIZE + radius + SIZE*sx; 200 201 main_y = puyofield[pre_main][p] + radius; 202 sub_y = puyofield[pre_sub+sx][q+sy] + radius; 203 204 205 if(puyomath[pre_main][p-1] == 0) { 206 p--; 207 } 208 209 if(puyomath[pre_sub+sx][q+sy-1] == 0) { 210 q--; 211 } 212 213 214 if(random_color1==1) { 215 HgWSetFillColor(layer1, HG_RED); 216 } 217 else if(random_color1==2) { 218 HgWSetFillColor(layer1, HG_BLUE); 219 } 220 else if(random_color1==3) { 221 HgWSetFillColor(layer1, HG_YELLOW); 222 } 223 else if(random_color1==4) { 224 HgWSetFillColor(layer1, HG_GREEN); 225 } 226 HgWCircleFill(layer1, main_x+move_wide, main_y, radius, 0); 227 228 229 if(random_color2==1) { 230 HgWSetFillColor(layer1, HG_RED); 231 } 232 else if(random_color2==2) { 233 HgWSetFillColor(layer1, HG_BLUE); 234 } 235 else if(random_color2==3) { 236 HgWSetFillColor(layer1, HG_YELLOW); 237 } 238 else if(random_color2==4) { 239 HgWSetFillColor(layer1, HG_GREEN); 240 } 241 HgWCircleFill(layer1, sub_x+move_wide, sub_y, radius, 0); 242 243 HgSleep(0.50); 244 245 246 if(sub_y >= main_y){ 247 if(puyomath[pre_main][p-1] != 0) { 248 puyomath[pre_main][p] = 2; 249 } 250 if(puyomath[pre_sub+sx][q+sy-1] != 0) { 251 puyomath[pre_sub+sx][q+sy] = 2; 252 } 253 } 254 else if(main_y > sub_y){ 255 if(puyomath[pre_sub+sx][q+sy-1] != 0) { 256 puyomath[pre_sub+sx][q+sy] = 2; 257 } 258 if(puyomath[pre_main][p-1] != 0) { 259 puyomath[pre_main][p] = 2; 260 } 261 } 262 263 HgLClear(layer1); 264 265 a = p; 266 b = q; 267 } 268 269 main_y = puyofield[pre_main][a] + radius; 270 sub_y = puyofield[pre_sub+sx][b+sy] + radius; 271 272 if(random_color1==1) { 273 HgWSetFillColor(field_layer[pre_main][a], HG_RED); 274 color = 1; 275 } 276 else if(random_color1==2) { 277 HgWSetFillColor(field_layer[pre_main][a], HG_BLUE); 278 color = 2; 279 } 280 else if(random_color1==3) { 281 HgWSetFillColor(field_layer[pre_main][a], HG_YELLOW); 282 color = 3; 283 } 284 else if(random_color1==4) { 285 HgWSetFillColor(field_layer[pre_main][a], HG_GREEN); 286 color = 4; 287 } 288 HgWCircleFill(field_layer[pre_main][a], main_x+move_wide, main_x, radius, 0); 289 290 291 292 if(random_color2==1) { 293 HgWSetFillColor(field_layer[pre_sub+sx][b+sy], HG_RED); 294 color = 1; 295 } 296 else if(random_color2==2) { 297 HgWSetFillColor(field_layer[pre_sub+sx][b+sy], HG_BLUE); 298 color = 2; 299 } 300 else if(random_color2==3) { 301 HgWSetFillColor(field_layer[pre_sub+sx][b+sy], HG_YELLOW); 302 color = 3; 303 } 304 else if(random_color2==4) { 305 HgWSetFillColor(field_layer[pre_sub+sx][b+sy], HG_GREEN); 306 color = 4; 307 } 308 HgWCircleFill(field_layer[pre_sub+sx][b+sy], sub_x+move_wide, sub_y, radius, 0); 309 310 311 score += 0; 312 HgLClear(layer2); 313 314 HgWSetFont(0, HG_M, 35); 315 HgWSetColor(0, HG_WHITE); 316 HgWText(0, 415, 320, "SCORE\n \t\t%d", score); 317 318 319 random_color1 = pre_random_color3; 320 random_color2 = pre_random_color4; 321 322 } 323 324 325 326 HgClose(); 327 328 return 0; 329} 330 331```![![イメージ説明](d151e4f7fab3434e0c3196763bd89ec9.png)](1b7bf0c37a57916dec922377fce1a424.png) 332 333### 補足情報(FW/ツールのバージョンなど) 334 335Xcode、handy graphicを使用しています。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

私ならですが、ぷよ1個置くたびに
フィールドが(見た目ではなく、内部的に)どうなっているか、
確認してバグを追っていきます。

置き場所が確定した後に、おかしくなれば設置の処理が、
置く前におかしければ、落下の処理に問題がありそうです。

投稿2018/07/09 23:54

torisan

総合スコア678

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ringo08

2018/07/10 01:49

具体的には、ソースコードを部分的に抜粋し、他のファイルに貼り付けて、実行してみたらいいでしょうか?
torisan

2018/07/10 04:39

質問者様のコメントの意図はよくわかりませんが、私は、 ぷよ設置毎にフィールドの内容を保存している変数の内容を確認すると、 何か分る可能性が高いのではないか、と言う意味で書きました。
ringo08

2018/07/10 05:15

ぷよを1つずつにしてみると間違っているところがわかりました! ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問