游戏-迷宫

新建筛选器game,下面新建项maze.c
将筛选器file下面的项io.c的main方法的名改为main60

1.复制筛选器combination下面的项loopBranch.c的基础框架到这里

#include <stdio.h>

int main(void) {
	printf("");
	return 0;
}

2.创建地图

#include <stdio.h>

int map[10][10] = 
{
    {1, 0, 1, 1, 1, 1, 1, 1, 1, 1},
    {1, 0, 0, 0, 1, 0, 0, 0, 0, 1},
    {1, 0, 1, 0, 1, 0, 1, 1, 0, 1},
    {1, 0, 1, 0, 0, 0, 0, 1, 0, 1},
    {1, 0, 1, 1, 1, 1, 0, 1, 0, 1},
    {1, 0, 0, 0, 0, 1, 0, 1, 0, 1},
    {1, 1, 1, 1, 0, 1, 0, 1, 0, 1},
    {1, 0, 0, 0, 0, 1, 0, 0, 0, 1},
    {1, 0, 1, 1, 0, 0, 0, 1, 0, 1}, 
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}   
};

int main(void) {
	printf("");
	return 0;
}

3.打印地图

#include <stdio.h>

int map[10][10] = 
{
    {1, 0, 1, 1, 1, 1, 1, 1, 1, 1},
    {1, 0, 0, 0, 1, 0, 0, 0, 0, 1},
    {1, 0, 1, 0, 1, 0, 1, 1, 0, 1},
    {1, 0, 1, 0, 0, 0, 0, 1, 0, 1},
    {1, 0, 1, 1, 1, 1, 0, 1, 0, 1},
    {1, 0, 0, 0, 0, 1, 0, 1, 0, 1},
    {1, 1, 1, 1, 0, 1, 0, 1, 0, 1},
    {1, 0, 0, 0, 0, 1, 0, 0, 0, 1},
    {1, 0, 1, 1, 0, 0, 0, 1, 0, 1}, 
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}   
};

int main(void) {
   for (int i = 0; i < 10; i++)
   {
       for (int j = 0; j < 10; j++)
       {
           if (map[i][j] == 1)
           {
               printf("■");
           }
           else if (map[i][j] == 0)
           {
               printf("  ");
           }
       }
       printf("\n");
   }
	printf("");
	return 0;
}

4.创建角色

#include <stdio.h>

struct Role
{
	int x;
	int y;
};

int map[10][10] = 
{
    {1, 0, 1, 1, 1, 1, 1, 1, 1, 1},  
    {1, 0, 0, 0, 1, 0, 0, 0, 0, 1},
    {1, 0, 1, 0, 1, 0, 1, 1, 0, 1},
    {1, 0, 1, 0, 0, 0, 0, 1, 0, 1},
    {1, 0, 1, 1, 1, 1, 0, 1, 0, 1},
    {1, 0, 0, 0, 0, 1, 0, 1, 0, 1},
    {1, 1, 1, 1, 0, 1, 0, 1, 0, 1},
    {1, 0, 0, 0, 0, 1, 0, 0, 0, 1},
    {1, 0, 1, 1, 0, 0, 0, 1, 0, 1}, 
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}   
};

int main(void) {
   for (int i = 0; i < 10; i++)
   {
       for (int j = 0; j < 10; j++)
       {
           if (map[i][j] == 1)
           {
               printf("■");
           }
           else if (map[i][j] == 0)
           {
               printf("  ");
           }
       }
       printf("\n");
   }
	printf("");
	return 0;
}

5.初始化角色位置

#include <stdio.h>

struct Role
{
	int x;
	int y;
};

struct Role role = { 0, 1 };

int map[10][10] = 
{
    {1, 0, 1, 1, 1, 1, 1, 1, 1, 1},  
    {1, 0, 0, 0, 1, 0, 0, 0, 0, 1},
    {1, 0, 1, 0, 1, 0, 1, 1, 0, 1},
    {1, 0, 1, 0, 0, 0, 0, 1, 0, 1},
    {1, 0, 1, 1, 1, 1, 0, 1, 0, 1},
    {1, 0, 0, 0, 0, 1, 0, 1, 0, 1},
    {1, 1, 1, 1, 0, 1, 0, 1, 0, 1},
    {1, 0, 0, 0, 0, 1, 0, 0, 0, 1},
    {1, 0, 1, 1, 0, 0, 0, 1, 0, 1}, 
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}   
};

int main(void) {
   for (int i = 0; i < 10; i++)
   {
       for (int j = 0; j < 10; j++)
       {
           if (map[i][j] == 1)
           {
               printf("■");
           }
           else if (map[i][j] == 0)
           {
               printf("  ");
           }
       }
       printf("\n");
   }
	printf("");
	return 0;
}

6.打印角色
添加windows.h

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

struct Role
{
	int x;
	int y;
};

struct Role role = { 0, 1 };

int map[10][10] = 
{
    {1, 0, 1, 1, 1, 1, 1, 1, 1, 1},  
    {1, 0, 0, 0, 1, 0, 0, 0, 0, 1},
    {1, 0, 1, 0, 1, 0, 1, 1, 0, 1},
    {1, 0, 1, 0, 0, 0, 0, 1, 0, 1},
    {1, 0, 1, 1, 1, 1, 0, 1, 0, 1},
    {1, 0, 0, 0, 0, 1, 0, 1, 0, 1},
    {1, 1, 1, 1, 0, 1, 0, 1, 0, 1},
    {1, 0, 0, 0, 0, 1, 0, 0, 0, 1},
    {1, 0, 1, 1, 0, 0, 0, 1, 0, 1}, 
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}   
};

int main(void) {
   for (int i = 0; i < 10; i++)
   {
       for (int j = 0; j < 10; j++)
       {
           if (map[i][j] == 1)
           {
               printf("■");
           }
           else if (map[i][j] == 0)
           {
               printf("  ");
           }
       }
       printf("\n");
   }

    HANDLE controller = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD position = {role.x, role.y};
    SetConsoleCursorPosition(controller, position);
    printf("@ ");

	printf("");
	return 0;
}

7.移动角色
添加stdbool.h里的true
添加conio.h里的getchr()

#include <stdio.h>
#include <windows.h>
#include <stdbool.h>
#include <conio.h>

struct Role
{
	int x;
	int y;
};

struct Role role = { 0, 1 };

int map[10][10] = 
{
    {1, 0, 1, 1, 1, 1, 1, 1, 1, 1},  
    {1, 0, 0, 0, 1, 0, 0, 0, 0, 1},
    {1, 0, 1, 0, 1, 0, 1, 1, 0, 1},
    {1, 0, 1, 0, 0, 0, 0, 1, 0, 1},
    {1, 0, 1, 1, 1, 1, 0, 1, 0, 1},
    {1, 0, 0, 0, 0, 1, 0, 1, 0, 1},
    {1, 1, 1, 1, 0, 1, 0, 1, 0, 1},
    {1, 0, 0, 0, 0, 1, 0, 0, 0, 1},
    {1, 0, 1, 1, 0, 0, 0, 1, 0, 1}, 
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}   
};

int main(void) {
   for (int i = 0; i < 10; i++)
   {
       for (int j = 0; j < 10; j++)
       {
           if (map[i][j] == 1)
           {
               printf("■");
           }
           else if (map[i][j] == 0)
           {
               printf("  ");
           }
       }
       printf("\n");
   }

    HANDLE controller = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD position = {role.x, role.y};
    SetConsoleCursorPosition(controller, position);
    printf("@ ");

	while (true) {
	    char move = getch();
	    if (move == 72)
	    {
	        COORD old_position = { role.x, role.y };
	        SetConsoleCursorPosition(controller, old_position);
	        printf("  ");
	        role.y -= 1;
	        COORD position = { role.x, role.y};
	        SetConsoleCursorPosition(controller, position);
	        printf("@");
	    }
	    else if (move == 80)
	    {
	        COORD old_position = { role.x, role.y };
	        SetConsoleCursorPosition(controller, old_position);
	        printf("  ");
	        role.y += 1;
	        COORD position = { role.x, role.y};
	        SetConsoleCursorPosition(controller, position);
	        printf("@");
	    }
	    else if (move == 75) {
	        COORD old_position = { role.x, role.y };
	        SetConsoleCursorPosition(controller, old_position);
	        printf("  ");
	        role.x -= 2;
	        COORD position = { role.x, role.y};
	        SetConsoleCursorPosition(controller, position);
	        printf("@");
	    }
	    else if (move == 77)
	    {
	        COORD old_position = { role.x, role.y };
	        SetConsoleCursorPosition(controller, old_position);
	        printf("  ");
	        role.x += 2;
	        COORD position = { role.x, role.y};
	        SetConsoleCursorPosition(controller, position);
	        printf("@");
	    }
	}
	printf("");
	return 0;

}



8.限制控制台边界

#include <stdio.h>
#include <windows.h>
#include <stdbool.h>
#include <conio.h>

struct Role
{
	int x;
	int y;
};

struct Role role = { 0, 1 };

int map[10][10] = 
{
    {1, 0, 1, 1, 1, 1, 1, 1, 1, 1},  
    {1, 0, 0, 0, 1, 0, 0, 0, 0, 1},
    {1, 0, 1, 0, 1, 0, 1, 1, 0, 1},
    {1, 0, 1, 0, 0, 0, 0, 1, 0, 1},
    {1, 0, 1, 1, 1, 1, 0, 1, 0, 1},
    {1, 0, 0, 0, 0, 1, 0, 1, 0, 1},
    {1, 1, 1, 1, 0, 1, 0, 1, 0, 1},
    {1, 0, 0, 0, 0, 1, 0, 0, 0, 1},
    {1, 0, 1, 1, 0, 0, 0, 1, 0, 1}, 
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}   
};

int main(void) {
   for (int i = 0; i < 10; i++)
   {
       for (int j = 0; j < 10; j++)
       {
           if (map[i][j] == 1)
           {
               printf("■");
           }
           else if (map[i][j] == 0)
           {
               printf("  ");
           }
       }
       printf("\n");
   }

    HANDLE controller = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD position = {role.x, role.y};
    SetConsoleCursorPosition(controller, position);
    printf("@ ");

    while (true) {
        char move = getch();
        if (move == 72)
        {
            if (role.y - 1 < 0)
            {
                continue;
            }
            COORD old_position = { role.x, role.y };
            SetConsoleCursorPosition(controller, old_position);
            printf("  ");
            role.y -= 1;
            COORD position = { role.x, role.y};
            SetConsoleCursorPosition(controller, position);
            printf("@");
        }
        else if (move == 80)
        {
            COORD old_position = { role.x, role.y };
            SetConsoleCursorPosition(controller, old_position);
            printf("  ");
            role.y += 1;
            COORD position = { role.x, role.y};
            SetConsoleCursorPosition(controller, position);
            printf("@");
        }
        else if (move == 75) {
            if (role.x - 2 < 0)
            {
                continue;
            } 
            COORD old_position = { role.x, role.y };
            SetConsoleCursorPosition(controller, old_position);
            printf("  ");
            role.x -= 2;
            COORD position = { role.x, role.y};
            SetConsoleCursorPosition(controller, position);
            printf("@");
        }
        else if (move == 77)
        {
            COORD old_position = { role.x, role.y };
            SetConsoleCursorPosition(controller, old_position);
            printf("  ");
            role.x += 2;
            COORD position = { role.x, role.y};
            SetConsoleCursorPosition(controller, position);
            printf("@");
        }
    }


	printf("");
	return 0;
}


9.限制墙壁边界

#include <stdio.h>
#include <windows.h>
#include <stdbool.h>
#include <conio.h>

struct Role
{
	int x;
	int y;
};

struct Role role = { 0, 1 };

int map[10][10] = 
{
    {1, 0, 1, 1, 1, 1, 1, 1, 1, 1},  
    {1, 0, 0, 0, 1, 0, 0, 0, 0, 1},
    {1, 0, 1, 0, 1, 0, 1, 1, 0, 1},
    {1, 0, 1, 0, 0, 0, 0, 1, 0, 1},
    {1, 0, 1, 1, 1, 1, 0, 1, 0, 1},
    {1, 0, 0, 0, 0, 1, 0, 1, 0, 1},
    {1, 1, 1, 1, 0, 1, 0, 1, 0, 1},
    {1, 0, 0, 0, 0, 1, 0, 0, 0, 1},
    {1, 0, 1, 1, 0, 0, 0, 1, 0, 1}, 
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}   
};

int main(void) {
   for (int i = 0; i < 10; i++)
   {
       for (int j = 0; j < 10; j++)
       {
           if (map[i][j] == 1)
           {
               printf("■");
           }
           else if (map[i][j] == 0)
           {
               printf("  ");
           }
       }
       printf("\n");
   }

    HANDLE controller = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD position = {role.x, role.y};
    SetConsoleCursorPosition(controller, position);
    printf("@ ");

    while (true) {
        char move = getch();
        if (move == 72)
        {
            if (role.y - 1 < 0)
            {
                continue;
            }
            else if (map[role.y - 1][role.x / 2] == 1)
            {
                continue;
            }
            COORD old_position = { role.x, role.y };
            SetConsoleCursorPosition(controller, old_position);
            printf("  ");
            role.y -= 1;
            COORD position = { role.x, role.y};
            SetConsoleCursorPosition(controller, position);
            printf("@");
        }
        else if (move == 80)
        {
            if (map[role.y + 1][role.x / 2] == 1)
            {
                continue;
            }
            COORD old_position = { role.x, role.y };
            SetConsoleCursorPosition(controller, old_position);
            printf("  ");
            role.y += 1;
            COORD position = { role.x, role.y};
            SetConsoleCursorPosition(controller, position);
            printf("@");
        }
        else if (move == 75) {
            if (role.x - 2 < 0)
            {
                continue;
            } else if (map[role.y][(role.x - 2) / 2] == 1)
            {
                continue;
            }
            COORD old_position = { role.x, role.y };
            SetConsoleCursorPosition(controller, old_position);
            printf("  ");
            role.x -= 2;
            COORD position = { role.x, role.y};
            SetConsoleCursorPosition(controller, position);
            printf("@");
        }
        else if (move == 77)
        {
            if (map[role.y][(role.x + 2) / 2] == 1)
            {
                continue;
            }
            COORD old_position = { role.x, role.y };
            SetConsoleCursorPosition(controller, old_position);
            printf("  ");
            role.x += 2;
            COORD position = { role.x, role.y};
            SetConsoleCursorPosition(controller, position);
            printf("@");
        }
    }
	printf("");
	return 0;
}

10.添加胜利条件

    map[9][8] = 2;
    while (true) {
        char move = getch();
        if (move == 72)
        {
            if (role.y - 1 < 0)
            {
                continue;
            }
            else if (map[role.y - 1][role.x / 2] == 1)
            {
                continue;
            }
            COORD old_position = { role.x, role.y };
            SetConsoleCursorPosition(controller, old_position);
            printf("  ");
            role.y -= 1;
            COORD position = { role.x, role.y};
            SetConsoleCursorPosition(controller, position);
            printf("@");
            if (map[role.y][role.x / 2] == 2)
            {
                return 0;
            }
        }
        else if (move == 80)
        {

            if (map[role.y + 1][role.x / 2] == 1)
            {
                continue;
            }
            COORD old_position = { role.x, role.y };
            SetConsoleCursorPosition(controller, old_position);
            printf("  ");
            role.y += 1;
            COORD position = { role.x, role.y};
            SetConsoleCursorPosition(controller, position);
            printf("@");
            if (map[role.y][role.x / 2] == 2)
            {
                return 0;
            }
        }
        else if (move == 75) {

            if (role.x - 2 < 0)
            {
                continue;
            } else if (map[role.y][(role.x - 2) / 2] == 1)
            {
                continue;
            }
            COORD old_position = { role.x, role.y };
            SetConsoleCursorPosition(controller, old_position);
            printf("  ");
            role.x -= 2;
            COORD position = { role.x, role.y};
            SetConsoleCursorPosition(controller, position);
            printf("@");
            if (map[role.y][role.x / 2] == 2)
            {
                return 0;
            }
        }
        else if (move == 77)
        {
            if (map[role.y][(role.x + 2) / 2] == 1)
            {
                continue;
            }
            COORD old_position = { role.x, role.y };
            SetConsoleCursorPosition(controller, old_position);
            printf("  ");
            role.x += 2;
            COORD position = { role.x, role.y};
            SetConsoleCursorPosition(controller, position);
            printf("@");
            if (map[role.y][role.x / 2] == 2)
            {
                return 0;
            }
        }
    }


	printf("");
	return 0;
}

posted @ 2025-07-31 20:38  基础狗  阅读(4)  评论(0)    收藏  举报