运动问题,跳过字段

时间:2019-05-29 13:54:16

标签: c windows game-physics

我正在制作一个游戏,其中您(玩家)由2D矩阵中的数字表示。问题是,当我移动播放器时,它会跳过字段,并且反应不佳。运动非常不准确。如果可以运行该程序,则可以更好地理解该问题。我该如何解决这个问题?

#include <stdio.h>
#include <windows.h>
#include <time.h>

void delay(unsigned int mseconds)
{
    clock_t goal = mseconds + clock();
    while (goal > clock())
        ;
}

int map[5][5] = {{0, 0, 0, 0, 0},
                 {0, 0, 0, 0, 0},
                 {0, 0, 0, 0, 0},
                 {0, 0, 0, 0, 0},
                 {0, 0, 0, 0, 0}};

int mUpdate(int x)
{
    int i, j;
    for (i = 0; i < 5; i++)
    {
        for (j = 0; j < 5; j++)
        {
            printf("%d     ", map[i][j]);
        }
        printf("\n");
    }
}

int mRefresh(int x)
{
    int i, j;
    for (i = 0; i < 5; i++)
    {
        for (j = 0; j < 5; j++)
        {
            if (map[i][j] > 0)
            {
                map[i][j] = 0;
            }
        }
    }
}

int main()
{
    int c;
    int i, j;
    int p_num;
    printf("Hey! Choose a number from 1-9 that will represent you!: ");
    scanf("%d", &p_num);
    printf("Choose a starting position by inputting two coordinates from 1-4!:  ");
    scanf("%d %d", &i, &j);
    printf("\nVery well, this is your starting position!\n");
    map[i][j] = p_num;
    mUpdate(map[i][j]);
    printf("\nYou are now the number %d !\n", p_num);
    printf("\nPlease wait for the map to load!");
    printf("\nMap loading will start soon!");
    delay(3000);
    for (c = 0; c < 5; c++)
    {
        printf("\nLoading.");
        delay(500);
        system("cls");
        printf("\nLoading..");
        delay(500);
        system("cls");
        printf("\nLoading...");
        delay(500);
        system("cls");
    }
    printf("Map loaded!\n");
    printf("\nUse arrow keys to move thru the matrix!\n");
    mUpdate(map[i][j]);

    int game_running = true;
    while (game_running = true)
    {
        if (GetAsyncKeyState(VK_DOWN))
        {
            // delay(200);
            map[i++][j] = p_num;
            system("cls");
            mUpdate(map[i][j]);
            mRefresh(map[i][j]);
        }
        else if (GetAsyncKeyState(VK_UP))
        {
            // delay(200);
            map[i--][j] = p_num;
            system("cls");
            mUpdate(map[i][j]);
            mRefresh(map[i][j]);
        }
        else if (GetAsyncKeyState(VK_RIGHT))
        {
            // delay(1000);
            map[i][j++] = p_num;
            system("cls");
            mUpdate(map[i][j]);
            mRefresh(map[i][j]);
        }
        else if (GetAsyncKeyState(VK_LEFT))
        {
            // delay(200);
            map[i][j--] = p_num;
            system("cls");
            mUpdate(map[i][j]);
            mRefresh(map[i][j]);
        }
    }
    system("cls");
}

0 个答案:

没有答案