一个简单的推箱子小游戏

单昊穹
2023-12-01

推箱子小游戏

一个很简单的推箱子小游戏,增加了退回一步的功能(实现方式很传统,通过标记上一个按键的方向,逆向将地图变换)

#include <stdio.h>
#include <conio.h>//输入
#include <windows.h>//清屏的头文件

int flag;
int old = 0;//记录上一次的方向
int tui;
int s;
int sum=0;
int map[9][11] = {
    {0,1,1,1,1,1,1,1,1,1,0},
    {0,1,0,0,0,1,0,0,0,1,0},
    {0,1,0,0,3,3,3,3,0,1,0},
    {0,1,0,3,0,3,0,3,0,1,1},
    {0,1,0,0,0,2,0,0,3,0,1},
    {1,1,0,1,1,1,1,0,3,0,1},
    {1,0,4,4,4,4,4,0,0,0,1},
    {1,0,4,4,4,4,4,0,0,1,1},
    {1,1,1,1,1,1,1,1,1,1,0}
};


void winlose()//判断输赢
{
    int i, j;
    int k = 0;
    for (i = 0; i < 9; i++)
    {
        for (j = 0; j < 11; j++)
        {
            if (map[i][j] == 3)
                k++;
        }
    }
    if (k == 0)
    {
        printf("恭喜你获得了胜利\n");
        printf("一共走了 %d 步",sum/2);
        flag = 0;
    }

}

void draw_map()
{
    int i, j;
    winlose();
    for (i = 0; i < 9; i++)
    {
        for (j = 0; j < 11; j++)
        {
            switch (map[i][j])
            {
            case 0:
                printf("  ");//空地
                break;
            case 1:
                printf("■");//箱子
                break;
            case 2:
                printf("人");
                break;
            case 3:
                printf("※");//箱子
                break;
            case 4:
                printf("终");
                break;
            case 6:
                printf("♀");//人加终点
                break;
            case 7:
                printf("★");//箱子加终点
                break;
            }
        }
        printf("\n");
    }
}

void move()
{
    int i, j,row , lie;
    for (row = 0; row < 9; row++)
    {
        for (lie = 0; lie < 11; lie++)
        {
            if (map[row][lie] == 2||map[row][lie]==6)
            {
                i = row;
                j = lie;
            }
        }
    }
    tui = _getch();
    if (tui == '1')
    {
        if (old == 1)
        {
            if (s == 0)
            {
                if (map[i + 1][j] == 0 || map[i + 1][j] == 4)
                {
                    map[i + 1][j] += 2;
                    map[i][j] -= 2;
                }

            }
            else if (s == 1)
            {
                map[i - 1][j] -= 3;
                map[i][j] += 1;
                map[i + 1][j] += 2;
            }
        }
        else if (old == 2)
        {
            if (s == 0)
            {
                if (map[i - 1][j] == 0 || map[i - 1][j] == 4)
                {
                    map[i - 1][j] += 2;
                    map[i][j] -= 2;
                }
            }
            else
                {
                    map[i + 1][j] -= 3;
                    map[i][j] += 1;
                    map[i - 1][j] += 2;
                }
        }
        else if (old == 3)
        {
            if (s == 0)
            {
                if (map[i][j + 1] == 0 || map[i][j + 1] == 4)
                {
                    map[i][j + 1] += 2;
                    map[i][j] -= 2;
                }
            }
            else
            {
                map[i][j - 1] -= 3;
                map[i][j] += 1;
                map[i][j + 1] += 2;
            }
        }
        else if (old == 4)
        {
            if (s == 0)
            {
                if (map[i][j - 1] == 0 || map[i][j - 1] == 4)
                {
                    map[i][j - 1] += 2;
                    map[i][j] -= 2;
                }
            }
            else
            {
                map[i][j + 1] -= 3;
                map[i][j] += 1;
                map[i][j - 1] += 2;
            }
        }
    }
    else
    {//与getchar()有区别的是:getchar()输入一个字符后需要回车来进行下一个字符的输入,
	                //比较麻烦 ,getch()则不需要回车就能连续输入多个字符。
        switch (tui) {
        case 'w'://分析所有可以移动的情况:(以上移为例)

			// 1.人的上面是空地;

		    // 2.人的上面是终点位置;

			// 3.人的上面是箱子
                //3.1.箱子的上面是空地;
                //3.2.箱子的上面是终点位置。
        case 'W':
        case 72:
            old = 1;
            if (map[i - 1][j] == 0 || map[i - 1][j] == 4)
            {
                s = 0;
                map[i - 1][j] += 2;
                map[i][j] -= 2;
            }
            else if (map[i - 1][j] == 3 || map[i - 1][j] == 7)
            {
                 s = 1;
                if (map[i - 2][j] == 0 || map[i - 2][j] == 4)
                {
                    map[i - 2][j] += 3;
                    map[i - 1][j] -= 1;
                    map[i][j] -= 2;
                }
            }
            break;
        case 's':
        case 'S':
        case 80:
            old = 2;
            if (map[i + 1][j] == 0 || map[i + 1][j] == 4)
            {
                s = 0;
                map[i + 1][j] += 2;
                map[i][j] -= 2;
            }
            else if (map[i + 1][j] == 3 || map[i + 1][j] == 7)
            {
                 s = 1;
                if (map[i + 2][j] == 0 || map[i + 2][j] == 4)
                {
                    map[i + 2][j] += 3;
                    map[i + 1][j] -= 1;
                    map[i][j] -= 2;
                }
            }break;
        case 'A':
        case 'a':
        case 75:
            old = 3;
            if (map[i][j - 1] == 0 || map[i][j - 1] == 4)
            {
                s = 0;
                map[i][j - 1] += 2;
                map[i][j] -= 2;
            }
            else if (map[i][j - 1] == 3 || map[i][j - 1] == 7)
            {
                 s = 1;
                if (map[i][j - 2] == 0 || map[i][j - 2] == 4)
                {
                    map[i][j - 2] += 3;
                    map[i][j - 1] -= 1;
                    map[i][j] -= 2;
                }
            }break;
        case 'd':
        case 'D':
        case 77:
            old = 4;
            if (map[i][j + 1] == 0 || map[i][j + 1] == 4)
            {
                s = 0;
                map[i][j + 1] += 2;
                map[i][j] -= 2;
            }
            else if (map[i][j + 1] == 3 || map[i][j + 1] == 7)
            {
                s = 1;
                if (map[i][j + 2] == 0 || map[i][j + 2] == 4)
                {
                    map[i][j + 2] += 3;
                    map[i][j + 1] -= 1;
                    map[i][j] -= 2;
                }
            }break;
        }
    }
}
int main(void)
{
    flag = 1;
    while (flag)
    {
        system("cls");
        printf("开始游戏\n\n");
        printf("按数字 1 键可以退回,但只能退回一步\n\n");
        if(tui=='1')
            printf("下次小心啊,(^_^)\n\n");
        draw_map();
        move();
        sum++;
    }
    return 0;
}
 类似资料: