Home>

I want to make a counter using the Dx library.
First display 100, press the up and down keys to +1 and down to -1. The range is 0-200.
If i press the top in the 0 state and the down in the 200 state, I want to get a warning in some way.

#include "DxLib.h"
int WINAPI WinMain (HINSTANCE hI, HINSTANCE hP, LPSTR lpC, int nC)
{
    char keyBuf [200];
    int color;
    int countUP = 100, countDOWN = 100, xd = 1, yd = 1;
    if (DxLib_Init () == -1) {
        return -1;
    }
    SetDrawScreen (DX_SCREEN_BACK);
    SetFontSize (100);
    color = GetColor (0, 255, 0);
    while (1)
    {
        GetHitKeyStateAll (keyBuf);
        if (keyBuf [KEY_INPUT_UP])
            countUP + = xd;
        if (keyBuf [KEY_INPUT_DOWN])
            countDOWN + = yd;
        DrawFormatString (0, 0, color, "100");
        ScreenFlip ();
        if (ProcessMessage () == -1)
            break;
        if (CheckHitKey (KEY_INPUT_ESCAPE)! = 0)
            break;
    }
    DxLib_End;
    return 0;
}


I wrote the above source. I'm just starting to study the Dx library, and it's unclear how to write it, and it's the source above.

If i don't know, how can you change the number by pressing the up and down keys?
Also, I don't know how to issue a warning (I want to make a warning such as making a sound).

I would appreciate it if you could tell me the above two points. Also, if there are any other mistakes, I would appreciate it if you could point out.

  • Answer # 1

    Will it be helpful?

    #include "DxLib.h"
    int WINAPI WinMain (HINSTANCE hI, HINSTANCE hP, LPSTR lpC, int nC)
    {
        ChangeWindowMode (TRUE);
        if (DxLib_Init () == -1) return -1;
        SetDrawScreen (DX_SCREEN_BACK);
        SetFontSize (100);
        int color = GetColor (0, 255, 0);
        int count = 100;
        int beep = 0;
        while (ProcessMessage () == 0) {
            char keyBuf [256];
            GetHitKeyStateAll (keyBuf);
            if (keyBuf [KEY_INPUT_ESCAPE]) break;
            if (keyBuf [KEY_INPUT_UP]&&++ count>200) {
                PlayBeep ();beep = 5;count = 200;
            }
            if (keyBuf [KEY_INPUT_DOWN]&&--count<0) {
                PlayBeep ();beep = 5;count = 0;
            }
            if (beep>0&&--beep == 0) StopBeep ();
            ClearDrawScreen ();
            DrawFormatString (50, 50, color, "% d", count);
            ScreenFlip ();
        }
        DxLib_End ();
        return 0;
    }


    Postscript
    Even if you think you pressed the key for a moment, it will be entered multiple times.
    If you want to count up and down one by one, you can do as follows.

    #include "DxLib.h"
    int WINAPI WinMain (HINSTANCE hI, HINSTANCE hP, LPSTR lpC, int nC)
    {
        ChangeWindowMode (TRUE);
        if (DxLib_Init () == -1) return -1;
        SetDrawScreen (DX_SCREEN_BACK);
        SetFontSize (100);
        int color = GetColor (0, 255, 0);
        int count = 100;
        int beep = 0;
        int keyCount [256] = {0};
        while (ProcessMessage () == 0) {
            char keyBuf [256];
            GetHitKeyStateAll (keyBuf);
            for (int i = 0;i<256;i ++)! keyBuf [i]? keyCount [i] = 0: keyCount [i] ++;
            if (keyCount [KEY_INPUT_ESCAPE]) break;
            if (keyCount [KEY_INPUT_UP] == 1&&++ count>200) {
                PlayBeep ();beep = 5;count = 200;
            }
            if (keyCount [KEY_INPUT_DOWN] == 1&&--count<0) {
                PlayBeep ();beep = 5;count = 0;
            }
            if (beep>0&&--beep == 0) StopBeep ();
            ClearDrawScreen ();
            DrawFormatString (50, 50, color, "% d", count);
            ScreenFlip ();
        }
        DxLib_End ();
        return 0;
    }


    Addendum 2
    If you want to count up and down one by one, and also want to add a repeat function,

          if (keyCount [KEY_INPUT_ESCAPE]) break;
            int k = keyCount [KEY_INPUT_UP];
            if ((k == 1 || k>30)&&++ count>200) {
                PlayBeep ();beep = 5;count = 200;
            }
            k = keyCount [KEY_INPUT_DOWN];
            if ((k == 1 || k>30)&&--count<0) {
                PlayBeep ();beep = 5;count = 0;
            }
            if (beep>0&&--beep == 0) StopBeep ();