• 来自童晶老师的游戏开发课程作业-贪吃蛇


    此作业的要求:https://edu.cnblogs.com/campus/nenu/2020Fall/homework/11577

    一、代码及git

    版本控制:https://github.com/18244088809/tanchishe.git

    二、功能和截图,讲解关键技术和代码片断。

    功能1:每次展示页面光标移动到(0,0)坐标。

    截图:

    代码:

    其中GetStdHandle:获取指定的标准设备的句柄,COORD:表示一个字符在控制台屏幕上的坐标,SetConsoleCursorPosition:设置控制台光标位置。

    void gotoxy(int x, int y)
    {
        HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
        COORD pos;
        pos.X = x;
        pos.Y = y;
        SetConsoleCursorPosition(handle, pos);
    }
    

    功能2:通过方向控制小蛇移动。

    截图:

    代码:

    通过找到小蛇的旧蛇头坐标以及下一步的方向确定新蛇头的位置。
    通过判定新蛇头的位置是否是食物的位置来决定是否将蛇尾的数值设置为0.

    void show()
    {
        gotoxy(0, 0);
        int i, j;
        for (i = 0; i < High; i++)
        {
            for (j = 0; j < Width; j++)
            {
                if (canvas[i][j] == canvasBorder)
                {
                    printf("#");
                }
                if (canvas[i][j] == canvasBlank)
                {
                    printf(" ");
                }
                if (canvas[i][j] == snakeHead)
                {
                    printf("@");
                }
                if (canvas[i][j] > snakeHead)
                {
                    printf("*");
                }
                if (canvas[i][j] == Food)
                {
                    printf("F");
                }
            }
            printf("
    ");
        }
        Sleep(100);
    }
    
    void moveSnakeByDirection()
    {
        int i, j;
        int max = 0;
        int oldTail_i, oldTail_j;
        int oldHead_i, oldHead_j;
        int newHead_i, newHead_j;
        for (i = 1; i < High - 1; i++)
        {
            for (j = 1; j < Width - 1; j++)
            {
                if (canvas[i][j] >= snakeHead)
                {
                    canvas[i][j]++;
                }
                if (canvas[i][j] > max)
                {
                    max = canvas[i][j];
                    oldTail_i = i;
                    oldTail_j = j;
                }
                if (canvas[i][j] == snakeHead+1)
                {
                    oldHead_i = i;
                    oldHead_j = j;
                }
            }
        }
    
        if (moveDirection == UP)
        {
            newHead_i = oldHead_i - 1;
            newHead_j = oldHead_j;
        }
        if (moveDirection == DOWN)
        {
            newHead_i = oldHead_i + 1;
            newHead_j = oldHead_j;
        }
        if (moveDirection == LEFT)
        {
            newHead_i = oldHead_i;
            newHead_j = oldHead_j - 1;
        }
        if (moveDirection == RIGHT)
        {
            newHead_i = oldHead_i;
            newHead_j = oldHead_j + 1;
        }
        if (canvas[newHead_i][newHead_j] == Food)
        { //新蛇头吃到食物
            food_x = rand() % (High - 5) + 2;
            food_y = rand() % (Width - 5) + 2;
            canvas[food_x][food_y] = Food;
        }
        else
        { //不吃 尾巴变零
            canvas[oldTail_i][oldTail_j] = canvasBlank;
        }
    
        if (canvas[newHead_i][newHead_j] == canvasBorder || canvas[newHead_i][newHead_j] > snakeHead)
        {
            printf("失败
    ");
            exit(0);
        }
        else
        {
            canvas[newHead_i][newHead_j] = snakeHead;
        }
    }
    

    三、WBS:

    估算时间(分钟) 实际耗时(分钟)
    分析 30 53
    代码 60 62
    调试 30 72

    四、PSP:

    任务 开始时间 结束时间 中断时间(分钟) delta时间(分钟)
    分析 11.28 9:30 11.28 10:23 0 53
    代码 11.29 9:49 11.29 10:58 7 62
    调试 11.29 12:28 11.29 13:45 5 72
    优化 11.30 8:20 11.30 8:54 0 34
  • 相关阅读:
    PAT顶级 1024 Currency Exchange Centers (35分)(最小生成树)
    Codeforces 1282B2 K for the Price of One (Hard Version)
    1023 Have Fun with Numbers (20)
    1005 Spell It Right (20)
    1092 To Buy or Not to Buy (20)
    1118 Birds in Forest (25)
    1130 Infix Expression (25)
    1085 Perfect Sequence (25)
    1109 Group Photo (25)
    1073 Scientific Notation (20)
  • 原文地址:https://www.cnblogs.com/dul843/p/14050403.html
Copyright © 2020-2023  润新知