The third experiment course design summary

1. Experimental Project Name
combat game
2. Experimental Item Description Function
using the well array further combat game, multi enemy aircraft, other emission shotgun effect;
3. Item Module Description

//清屏功能
void HideCuresor()
{
    CONSOLE_CURSOR_INFO cursor_info = { 1,0 };
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void gotoxy(int x, int y)
{
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(handle, pos);
}
//数据初始化
void startup()
{
    pos_x = high - 1;
    pos_y = width / 2;
    canvas[pos_x][pos_y] = 1;
    int k;
    for (k = 0; k < enum; k++)
    {
        enemy_x[k] = rand() % 2;
        enemy_y[k] = rand() % width;
        canvas[enemy_x[k]][enemy_y[k]] = 3;
    }
    score = 0;
    bulwidth = 0;
    enespeed = 20;
}
//显示飞机
void show()
{
    gotoxy(0, 0);
    int i, j;
    for (i = 0; i < high; i++)
    {
        for (j = 0; j < width; j++)
        {
            if (canvas[i][j] == 0)
                printf(" ");
            else if (canvas[i][j] == 1)
                printf("*");
            else if (canvas[i][j] == 2)
                printf("|");
            else if (canvas[i][j] == 3)
                printf("@");
        }
        printf("\n");
    }
    printf("得分:%d\n", score);
    Sleep(20);
}
//击中敌机,多台敌机,击落得分,敌机刷新
void updateWithoutInput()
{
    int i, j, k;
    for (i = 0; i < high; i++)
    {
        for (j = 0; j < width; j++)
        {
            if (canvas[i][j] == 2)
            {
                for (k = 0; k < enum; k++)
                {
                    if ((i == enemy_x[k]) && (j == enemy_y[k]))
                    {
                        score++;
                        if (score % 5 == 0 && enespeed > 3)
                            enespeed--;
                        if (score % 5 == 0)
                            bulwidth++;
                        canvas[enemy_x[k]][enemy_y[k]] = 3;
                        canvas[i][j] = 0;
                    }
                }
                canvas[i][j] = 0;
                if (i > 0)
                    canvas[i - 1][j] = 2;
            }
        }
    }
    static int speed = 0;
    if (speed < enespeed)
        speed++;
    for (k = 0; k < enum; k++)
    {
        if ((pos_x == enemy_x[k]) && (pos_y == enemy_y[k]))
        {
            printf("失败!\n");
            Sleep(3000);
            system("pause");
            exit(0);
        }
        if (enemy_x[k] > high)
        {
            canvas[enemy_x[k]][enemy_y[k]] = 0;
            enemy_x[k] = rand() % 2;
            enemy_y[k] = rand() % width;
            canvas[enemy_x[k]][enemy_y[k]] = 3;
            score--;
        }
        if (speed == enespeed)
        {
            for (k = 0; k < enum; k++)
            {
                canvas[enemy_x[k]][enemy_y[k]] = 0;
                enemy_x[k]++;
                speed = 0;
                canvas[enemy_x[k]][enemy_y[k]] = 3;
            }
        }
    }
}
//控制我方移动
void updateWithInput()
{
    char input;
    if (kbhit())
    {
        input = getch();
        if (input == 'a'&&pos_y > 0)
        {
            canvas[pos_x][pos_y] = 0;
            pos_y--;
            canvas[pos_x][pos_y] = 1;
        }
        else if (input == 'd'&&pos_y < width - 1)
        {
            canvas[pos_x][pos_y] = 0;
            pos_y++;
            canvas[pos_x][pos_y] = 1;
        }

        else if (input == 'w')
        {
            canvas[pos_x][pos_y] = 0;
            pos_x--;
            canvas[pos_x][pos_y] = 1;
        }

        else if (input == 's')
        {
            canvas[pos_x][pos_y] = 0;
            pos_x++;
            canvas[pos_x][pos_y] = 1;
        }

        else if (input == ' ')
        {
            int left = pos_y - bulwidth;
            int right = pos_y + bulwidth;
            if (left < 0)
                left = 0;
            if (right > width - 1)
                right = width - 1;
            int k;
            for (k = left; k <= right; k++)
                canvas[pos_x - 1][k] = 2;
        }
    }
}

4. Implement interface display

5. code hosting links
https://gitee.com/scfyer/first
6. experiments summarized
combat game become more interesting than the original, and further deepen the understanding and application of the array.

Guess you like

Origin www.cnblogs.com/scafer/p/10990441.html