簡単な脳トレゲームが作りたくて奮闘しております。
今日は、ランダムな計算問題を作って、計算問題を表示した後、それに対してキーボード入力で答えを入力し、
〇か×を出力するプログラムを作っておりました。
ところが、計算問題を表示した後、すぐに〇が表示されてしまいます。
例えば、プレイヤーがキーボードで「0」と入力することを考えた場合、switch文でcase0として
switch("問題の答え"){
if (KEY_INPUT_0)
DrawFormatString(200, 300, Green, "〇 ");
else
DrawFormatString(200, 300, Green, "× ");
}
ソースコードは、
#include "DxLib.h"
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
int Key[256]; // キーが押されているフレーム数を格納する
// キーの入力状態を更新する
ChangeWindowMode(TRUE); // ウインドウモードに変更
if (DxLib_Init() == -1) return -1; //DXライブラリ初期化 エラーが起きたら終了
int Random[2]; //乱数の配列を二つ用意
int Green = GetColor(0, 255, 0);
int x;//二つの乱数を合計した数の変数を用意
SRand(10); //乱数の初期値を10で設定。
Random[0] = GetRand(5);
Random[1] = GetRand(4);
int StartTime; //今の時間
ChangeWindowMode(TRUE), DxLib_Init(), SetDrawScreen(DX_SCREEN_BACK);
bool displayText = true;//計算問題の文字を表示するか
bool displayText2 = false;//回答の文字を表示するか
// 文字を表示するか
StartTime = GetNowCount(); // プログラムの開始時刻
while (ScreenFlip() == 0 && ProcessMessage() == 0 && ClearDrawScreen() == 0) {
if (displayText)
{
DrawFormatString(200, 200, Green, "%d + %d = ", Random[0], Random[1]);
if (GetNowCount() - StartTime >= 6000)
{
// 6秒経ったら文字を表示しなくする
displayText = false;
}
}
if (displayText == 0) { //乱数の足し算の表示が消えた後
displayText2 = true;
if (displayText2) {
DrawFormatString(200, 200, Green, "計算の答えを入力しなさい。");
x = Random[0] + Random[1];
switch (x) {
case 0: if (KEY_INPUT_0)
DrawFormatString(200, 300, Green, "〇 ");
else
DrawFormatString(200, 300, Green, "× ");
case 1:if (KEY_INPUT_1)
DrawFormatString(200, 200, Green, "〇");
else
DrawFormatString(200, 300, Green, "× ");
break;
case 2:if (KEY_INPUT_2)
DrawFormatString(200, 300, Green, "〇 ");
else
DrawFormatString(200, 300, Green, "× ");
break;
case 3:if (KEY_INPUT_3)
DrawFormatString(200, 300, Green, "〇 ");
else
DrawFormatString(200, 300, Green, "×");
break;
case 4:if (KEY_INPUT_4)
DrawFormatString(200, 300, Green, "〇");
else
DrawFormatString(200, 300, Green, "×");
break;
case 5:if (KEY_INPUT_5)
DrawFormatString(200, 300, Green, "〇 ");
else
DrawFormatString(200, 300, Green, "× ");
break;
case 6:if (KEY_INPUT_6)
DrawFormatString(200, 300, Green, "〇 ");
else
DrawFormatString(200, 300, Green, "×");
break;
case 7:if (KEY_INPUT_7)
DrawFormatString(200, 300, Green, "〇");
else
DrawFormatString(200, 300, Green, "× ");
break;
case 8:if (KEY_INPUT_8)
DrawFormatString(200, 300, Green, "〇 ");
else
DrawFormatString(200, 300, Green, "× ");
break;
case 9:if (KEY_INPUT_9)
DrawFormatString(200, 300, Green, "〇 ");
else
DrawFormatString(200, 300, Green, "×");
break;
}
}
}
}
WaitKey();
DxLib_End(); // DXライブラリ使用の終了処理
return 0; // ソフトの終了r
}