を作ろうとしています。
do~while文を使い無限ループさせて描写を行いたいのですが、何故かキー入力をしない限り
ループが行われません。
何故自動でループされないのでしょうか?
見づらいコードかと思いますが、よろしくお願いします。
OS:Windows10
コンパイラ名:Visual Studio 2017
エラー:無し
C言語の理解度は非常に少ないです。
使用したライブラリ:分からず調べたのですがよく分かりませんでした...
stdafx.hの中身は"stdio.h" <string> <random>です。
#include "stdafx.h"
#include "conio.h"
#include "Windows.h"
const int map_x = 10;
const int map_y = 10;
int P1_pos[2] = { 1,1 };
int P1_vec[2] = { 0,0 };
int P1Beam_pos[2] = { -1,-1 };
int P1Beam_vec[2] = { 0,0 };
int turn = 0;
std::string map[map_y][map_x] = { {"壁","壁","壁","壁","壁","壁","壁","壁","壁","壁"},
{"壁","■","■","■","■","■","■","■","■","壁"},
{"壁","■","■","■","■","■","■","■","■","壁"},
{"壁","■","■","■","■","■","■","■","■","壁"},
{"壁","■","■","■","■","■","■","■","■","壁"},
{"壁","■","■","■","■","■","■","■","■","壁"},
{"壁","■","■","■","■","■","■","■","■","壁"},
{"壁","■","■","■","■","■","■","■","■","壁"},
{"壁","■","■","■","■","■","■","■","■","壁"},
{"壁","壁","壁","壁","壁","壁","壁","壁","壁","壁"} };
std::string player[4] = { "P1","P2","●","▲" };
int main()
{
do {
turn++;
/*操作*/
if (_kbhit != 0)
{
switch (_getch())
{
case 'w':P1_vec[0] = -1; P1_vec[1] = 0; break;
case 'a':P1_vec[1] = -1; P1_vec[0] = 0; break;
case 's':P1_vec[0] = 1; P1_vec[1] = 0; break;
case 'd':P1_vec[1] = 1; P1_vec[0] = 0; break;
case 'f':if (P1Beam_pos[0] == -1 && P1Beam_pos[1] == -1 )
{
P1Beam_pos[0] = P1_pos[0]; P1Beam_pos[1] = P1_pos[1];
P1Beam_vec[0] = P1_vec[0]; P1Beam_vec[1] = P1_vec[1]; break;
}
}
}
system("cls");
/*プレーヤー移動*/
if (map[P1_pos[0] + P1_vec[0]][P1_pos[1] + P1_vec[1]] != "壁") { P1_pos[0] += P1_vec[0]; P1_pos[1] += P1_vec[1]; }
P1Beam_pos[0] += P1Beam_vec[0]; P1Beam_pos[1] += P1Beam_vec[1];
if (map[P1Beam_pos[0]][P1Beam_pos[1]] == "壁") { P1Beam_pos[0] = -1; P1Beam_pos[1] = -1;
P1Beam_vec[0] = 0; P1Beam_vec[1] = 0; }
/*ステージ描画*/
for (int x = 0; x < map_x; x++)
{
for (int y = 0; y < map_y; y++)
{
if (P1_pos[0] == x && P1_pos[1] == y) printf("%s", player[0].c_str());
else if (P1Beam_pos[0] == x && P1Beam_pos[1] == y) printf("%s", player[3].c_str());
else printf("%s", map[x][y].c_str());
}
printf("\n");
}
/*パラメーター画面*/
printf("\nP1操作 W:↑ A:← S:↓ D:→ F:ビーム\n");
printf("\nP1_pos P1_vec P1Beam_pos P1Beam_vec\nX %d X %02d X %02d X %02d\nY %d Y %02d Y %02d Y %02d\n"
, P1_pos[0], P1_vec[0],P1Beam_pos[0],P1Beam_vec[0],P1_pos[1], P1_vec[1],P1Beam_pos[1],P1Beam_vec[1]);
printf("経過ターン:%d\n", turn);
Sleep(32);
} while (1);
return 0;
}