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

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

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

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

解決済

1回答

314閲覧

DirectXでなんフレーム経過などという処理の書き方が正しいのか知りたい

退会済みユーザー

退会済みユーザー

総合スコア0

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

0クリップ

投稿2019/06/14 19:47

別のファイルで毎秒60フレームに設定しています。
////コメント部のmb++ を使ってそのブロックが落下してからのフレームを加算してそれが60を超えたら
その処理をするというプログラムを作っているのですがこれはやり方として正しいのでしょうか?
このような処理を書くの初めてなので質問しました。

Player.cpp

1 2 3 4/*ブロック 種類、回転 x、y*/ 5static const int gblock[8][4][4][4] 6{//文字数の関係で省略以下例 7 8 { 9 {0,1,0,0}, 10 {1,1,0,0}, 11 {0,1,0,0}, 12 {0,0,0,0}, 13 14 }, 15 16 }, 17 18}; 19 20/* 21画面サイズ: 1280,720 22ステージサイズ: 288,504 23*/ 24struct block_pos 25{ 26public: 27 28 int x; 29 int y; 30}; 31 32enum key_state 33{ 34 Left, 35 Right, 36 Down, 37 Rotation, 38 eHold, 39 Invalid, 40}; 41 42 43 44static int stage[STAGE_X][STAGE_Y]; //ブロック記録 45 46 47static int downspeed = DOWN_SPEED; //落下スピード 48static int block_hold = -1; //ホールドしてるブロックを記録 49static int nexts[4] = {-1}; //ブロックのネクストを表示 50static int nowblock = 0; 51 52static key_state key = Invalid; //キー入力管理 53static block_pos bpos; //ブロック座標 54static int block_r = 0; //回転 55 56static bool isdown = false; //落下して止まったかどうか 57static uint cidx[7] = { 5,3,4,1,6,2,7 }; //カラーインデックス 58 59#define NEXT_LAST 3 60#define NEXT 1 61 62/*一個詰めて最後の値を設定*/ 63void randb_last() 64{ 65 int la = rand() % 6; 66 for (int i = 0; i < 4; i++) 67 { 68 if (i == NEXT_LAST) 69 { 70 nexts[i] = la; 71 break; 72 } 73 int a = nexts[i + 1]; 74 nexts[i] = a; 75 } 76} 77 78 79/*初期化*/ 80void Player::Initialize() 81{ 82 frame_hendle = LoadGraph("Asset/Frame_Block_24px.png"); 83 LoadDivGraph("Asset/Block_tmp_24px.png", 8, 4, 2, 24, 24, block_tmp); 84 back_ground = LoadGraph("Asset/BG.png"); 85 86 87 88 bpos.x = 4; 89 bpos.y = 0; 90 91 /*-1ですべての盤面を初期化*/ 92 for (int y = 0; y < STAGE_Y; y++) 93 { 94 for (int x = 0; x < STAGE_X; x++) 95 { 96 stage[x][y] = STAGE_EMPTY; 97 } 98 } 99 100 /*盤面初期化*/ 101 102 srand((unsigned int)time(NULL)); 103 104 /************ブロック乱数記録配列の初期化***************/ 105 nowblock = rand() % 6; 106 for (int i = 0; i < 4; i++) 107 { 108 nexts[i] = rand() % 6; 109 } 110 111 112 113} 114 115 116 117// ------------------------------------------------------------------------------- 118// 計算 119// ------------------------------------------------------------------------------- 120 121static int mb = 0;//固定時の直前移動 122 123static int t = 0;//4ラインのテトリスの数 124static int c3 = 0;//3ラインの数 125static int c2 = 0;//2ラインの数 126static int c1 = 0;//1ラインの数 127 128 129static bool d = true;//isdown中でも落下する場合 130 131 132/*ブロックを生成*/ 133//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 134void Player::Create_block() 135{ 136 137 //落下中のブロックが落ちてないときはfalse 落ちたらtrue 138 //落下したとき 139 if (isdown == true) { 140 mb++; 141 if (mb > 60)//固定までの待ち時間 142 { 143 /*ブロック座標を盤面配列に値を設定*/ 144 for (int dy = 0; dy < 4; dy++) 145 { 146 for (int dx = 0; dx < 4; dx++) 147 { 148 149 if (gblock[nowblock][block_r][dx][dy] != 0) 150 { 151 stage[bpos.x + dx][bpos.y + dy] = nowblock; 152 } 153 } 154 } 155 156 /*新しいブロックの乱数を再設定してブロック座標を初期*/ 157 //randb();//新しいブロック乱数を再設定 158 bpos.x = 4; 159 bpos.y = 0; 160 isdown = false; 161 mb = 0; 162 /*ネクスト設定*/ 163 nowblock = nexts[0]; 164 randb_last(); 165 166 } 167////////////////////////////////////////////////////////////////////////////////////////////////////////////// 168 /// 169 //Block_Down(); 170 for (int y = 0; y < 4; y++) 171 { 172 for (int x = 0; x < 4; x++) 173 { 174 if (gblock[nowblock][block_r][x][y] == 1) 175 { 176 if (//stage[bpos.x + x][bpos.y + y + 1] == STAGE_FRAME || stage[bpos.x + x][bpos.y + y + 1] == STAGE_LOWER 177 stage[bpos.x + x][bpos.y + y + 1] == STAGE_EMPTY) 178 { 179 isdown = false; 180 } 181 } 182 } 183 } 184 185 186 } 187 188 189} 190 191/*ブロック削除*/ 192void Player::Block_Clear() 193{ 194 bool clear = false; 195 int c = 0; 196 int cy[20] = { 99 }; 197 int num = 0;//削除ライン 198 int line[22] = { 0 };//上から4ラインあったらテトリス 199 200 /*ライン削除できるかどうか判定*/ 201 for (int y = 0; y < STAGE_Y - 1; y++) 202 { 203 for (int x = 1; x <= STAGE_X - 2; x++) 204 { 205 if (stage[x][y] != -1) 206 { 207 c++; 208 } 209 210 if (c == 10) 211 { 212 line[y] = 1;//削除ライン 213 num = y; 214 clear = true; 215 } 216 } 217 218 /**/ 219 if (clear == true)//ラインを消す場合 220 { 221 int dy = 0;//一行前のデータ 222 int dyy = 0; 223 int tmp[13] = { -10 }; 224 225 for (int y = num; y > 0; y--) 226 { 227 dy = y - 1; 228 229 /*一つ上の行の配列をコピー*/ 230 for (int x = 1; x <= STAGE_X - 2; x++) 231 { 232 tmp[x] = stage[x][dy]; 233 } 234 235 /*コピー*/ 236 for (int x = 1; x <= STAGE_X - 2; x++) 237 { 238 stage[x][y] = tmp[x]; 239 } 240 241 /*配列再初期化*/ 242 for (int i = 0; i < 11; i++) 243 { 244 tmp[i] = -10; 245 } 246 /*ずらした行をすべてゼロ*/ 247 for (int x = 1; x <= STAGE_X - 2; x++) 248 { 249 stage[x][dy] = -1; 250 } 251 252 } 253 } 254 clear = false; 255 c = 0; 256 } 257 /*要修正    ライン処理ばバグってる*/ 258 /************スコア計算******************/ 259 int t = 0; 260 int i = 0; 261 //for (int i = 0; i < 22; i++) 262 263 264 /***************************************/ 265 266 267} 268 269 270 271 272/*ブロック落下 273ブロックが落下できな場合 isdown = true; 274*/ 275void Player::Block_Down() 276{ 277 278 279 for (int y = 0; y < 4; y++) 280 { 281 for (int x = 0; x < 4; x++) 282 { 283 if (gblock[nowblock][block_r][x][y] == 1) 284 { 285 if (stage[bpos.x + x][bpos.y + y + 1] == STAGE_FRAME || stage[bpos.x + x][bpos.y + y + 1] == STAGE_LOWER 286 || stage[bpos.x + x][bpos.y + y + 1] != STAGE_EMPTY) 287 { 288 isdown = true; 289 290 } 291 } 292 } 293 } 294 295 /*落下中のみ*/ 296 if (isdown == false) 297 { 298 /*落下速度*/ 299 if (fCount % downspeed == 0) 300 { 301 bpos.y += 1; 302 } 303 } 304 305} 306 307/*操作*/ 308void Player::Move() 309{ 310 311} 312 313 314/*当たり判定 ぶつかったらtrue →方向*/ 315bool Player::Right_Collision() 316{ 317 for (int y = 0; y < 4; y++) 318 { 319 for (int x = 0; x < 4; x++) 320 { 321 if (gblock[nowblock][block_r][x][y] == 1) 322 { 323 if ( (stage[bpos.x + x + 1][bpos.y + y] == STAGE_FRAME //|| stage[bpos.x + x + 1][bpos.y + y + 1] == STAGE_LOWER 324 || stage[bpos.x + x + 1][bpos.y + y] != STAGE_EMPTY ) ) 325 { 326 return true;//ぶつかったらtrue 327 } 328 } 329 } 330 } 331 332 return false; 333} 334 335/*当たり判定 ぶつかったらtrue ←方向*/ 336bool Player::Left_Collision() 337{ 338 for (int y = 0; y < 4; y++) 339 { 340 for (int x = 0; x < 4; x++) 341 { 342 if (gblock[nowblock][block_r][x][y] == 1) 343 { 344 if ( (stage[bpos.x + x -1][bpos.y + y] == STAGE_FRAME //stage[bpos.x + x -1][bpos.y + y + 1] == STAGE_LOWER 345 || stage[bpos.x + x - 1][bpos.y + y] != STAGE_EMPTY) ) 346 { 347 return true;//ぶつかったらtrue 348 } 349 } 350 } 351 } 352 353 return false; 354 355} 356 357 358static long f = 0;//長押し管理変数 359 360/*キー入力受付処理*/ 361void Player::Control() 362{ 363 364} 365 366/*回転処理*/ 367void Player::Rotate() 368{ 369 int tr = block_r; 370 tr++; 371 if (tr > 3) 372 { 373 tr = 0; 374 } 375 int c = 0; 376 int cb = 0; 377 int bc[4] = { 0 }; 378 379 for (int y = 0; y < 4; y++) 380 { 381 for (int x = 0; x < 4; x++) 382 { 383 if (gblock[nowblock][tr][x][y] == 1) 384 { 385 if (stage[bpos.x + x][bpos.y + y] == STAGE_FRAME || stage[bpos.x + x][bpos.y + y] == STAGE_LOWER 386 || stage[bpos.x + x][bpos.y + y] != STAGE_EMPTY) 387 { 388 bpos.x--; 389 } 390 } 391 } 392 } 393 394 block_r++; 395 if (block_r > 3) 396 { 397 block_r = 0; 398 } 399 400} 401 402 403//*********************************************** 404void Player::Update() 405{ 406 for (int i = 0; i < 4; i++) 407 { 408 DrawFormatString(0, 100 * i,Color,"%d",nexts[i]); 409 } 410 411 412 Create_block(); 413 Block_Down(); 414 Move(); 415 Control(); 416 Block_Clear(); 417} 418//*********************************************** 419 420 421 422 423 424 425// ------------------------------------------------------------------------------- 426// 描画 427// ------------------------------------------------------------------------------- 428 429 430 431/*ブロック描画 (落下中)*/ 432void Player::Draw_Block() 433{ 434 int x = bpos.x; 435 int y = bpos.y; 436 437 for (int dy = 0; dy < 4; dy++) 438 { 439 for (int dx = 0; dx < 4; dx++) 440 { 441 if (gblock[nowblock][block_r][dx][dy] != 0) 442 { 443 DrawGraph( (dx + x) * block + DRAW_AREA_X , (dy + y) * block + DRAW_AREA_Y, block_tmp[cidx[nowblock]] , true); 444 } 445 } 446 } 447} 448 449/*ブロック固定描画*/ 450void Player::Draw_PutBlock() 451{ 452 for (int y = 0; y < STAGE_Y; y++) 453 { 454 for (int x = 0; x < STAGE_X; x++) 455 { 456 if ( (stage[x][y] != STAGE_FRAME) && (stage[x][y] != -1) && (stage[x][y] != STAGE_LOWER) ) 457 { 458 DrawGraph(x * block + DRAW_AREA_X, y * block + DRAW_AREA_Y,block_tmp[ cidx[stage[x][y]] ],true); 459 } 460 } 461 } 462} 463 464void Player::Draw_Score() 465{ 466 long score = (t * LINE_4) + (c3 * LINE_3) + (c2 * LINE_2) + (c1 * LINE_1); 467 468 DrawFormatString(50, 100, Color, "4ライン: %d", t); 469 DrawFormatString(50, 200, Color, "3ライン: %d", c3); 470 DrawFormatString(50, 300, Color, "2ライン: %d", c2); 471 DrawFormatString(50, 400, Color, "1ライン: %d", c1); 472 473 DrawFormatString(800, 720 / 3, Color, "SCORE: %ld ", score); 474 475 476} 477/*ホールド描画*/ 478void Player::Draw_Hold() 479{ 480 const int pos_x = 100; 481 const int pos_y = 100; 482 483 for (int y = 0; y < 4; y++) 484 { 485 for (int x = 0; x < 4; x++) 486 { 487 if (block_hold != -1) 488 { 489 if (gblock[block_hold][0][x][y] != 0) 490 { 491 DrawGraph((block * x) + pos_x, (block * y) + pos_y, block_tmp[cidx[block_hold]], true); 492 } 493 } 494 } 495 } 496} 497 498void Player::Draw_Next() 499{ 500 int cvy = 0; 501 int addx = DRAW_AREA_X + block * 14; 502 int addy = DRAW_AREA_Y + block * 2; 503 504 for (int i = 0; i < 4; i++) { 505 for (int y = 0; y < 4; y++) { 506 for (int x = 0; x < 4; x++) { 507 if (gblock[nexts[i]][0][x][y] != 0) 508 { 509 DrawGraph((block * x) + addx, (block * y) + (cvy)+addy, block_tmp[cidx[nexts[i]]], true); 510 } 511 } 512 } 513 cvy += 100; 514 } 515} 516 517//*********************************************** 518void Player::DrawUpdate() 519{ 520 //DrawGraph(0,0,back_ground,true); 521 Draw_Hold(); 522 Draw_Score(); 523 Draw_PutBlock(); 524 frame(); 525 Draw_Block(); 526 Draw_Next(); 527 528} 529 530//*********************************************** 531 532 533/*枠組み*/ 534void Player::frame() 535{ 536 537 /*枠の表示*/ 538 for (uint y = 0; y < STAGE_Y; y++) 539 { 540 for (uint x = 0; x < STAGE_X; x++) 541 { 542 if (stage[x][y] == STAGE_FRAME || stage[x][y] == STAGE_LOWER) 543 { 544 DrawGraph(x * block + DRAW_AREA_X , y * block + DRAW_AREA_Y, frame_hendle, true); 545 } 546 } 547 } 548} 549 550 551 552 553/*終了*/ 554void Player::EndProcess() 555{ 556 557} 558 559

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

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

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

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

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

guest

回答1

0

ベストアンサー

「正しい」かどうかはプログラム全体としてちゃんと期待した動作をするかどうか、という話になるのでなんとも言えませんが、手法としてはありでしょう。

画面のリフレッシュは、ハード的に発生する定時呼び出しになるでしょう。その回数を数えるのですから、計時の手法としてはありです。
また、画面の表示を行うプログラムですから、画面の更新より細かい時間には意味はありません。次の画面更新までに次の画面の描画が出来ていればいいわけですから、それは必要にして十分な更新周期、と言えるでしょう。

投稿2019/06/15 05:12

thkana

総合スコア7629

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

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

退会済みユーザー

退会済みユーザー

2019/06/16 18:49

いろんなものがアニメーションする場合はゲーム起動時からのフレーム数を数えてそれを % 60 == 0 するなりすればいいのでしょうか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問