用C语言的数组弄个推箱子小游戏

        当时上课听老师说了可以用数组弄个推箱子小游戏,就感到挺有兴趣,然后就看着老师的大致框架码了下来,主要内容倒是自己写。一开始碰到不少难题,几次碰壁,磕磕碰碰了几个小时,差点想要放弃,幸好坚持了下来,感觉收获了不少。特别是如何寻找方法,解决问题,老师是很好,但使用AI帮忙纠正一些语法,结构的问题真的很高效率。同时一些细节上的错误也能够很快发现并解决。

        虽然这个小游戏很简陋,但是它作为我的第一个学习C语言的成果,我真的又兴奋有激动。话不多说,源代码如下:

#include<stdio.h>
#include<Windows.h>
#include<conio.h>

int main()
{      int arr[6][6] = {
            1,1,1,1,1,1,
            1,3,0,0,0,1,
            1,0,2,3,0,1,
            1,0,4,0,0,1,
            1,0,0,0,0,1,
            1,1,1,1,1,1,
        };//0是空格,1是墙,2是人,3是金币,4是箱子

        int x = 0, y = 0;//表示人的位置
        while (1)
        {
            system("cls");//清屏
    
            for (int i = 0; i < 6; i++)
            {
                for (int k = 0; k < 6; k++)
                {
                    if (arr[i][k] == 2)
                    {
                        x = i;
                        y = k;
                    }
                }
            }
            for (int i = 0; i < 6; i++)
            {
                for (int k = 0; k < 6; k++)
                {
                    printf("%d\t", arr[i][k]);
                }
                printf("\n");
            }
    
            switch (_getch())//获取一个字符
            {
            case 's':
                    if (x + 1 < 6 && arr[x + 1][y] == 0)//在空格移动
                    {
                    arr[x + 1][y] += 2;
                    arr[x][y] -= 2;
                    x++;
                    }
                    else if (x + 1 < 6 && arr[x + 1][y] == 3)//吃掉金币
                    {
                        arr[x + 1][y] -= 1;
                        arr[x][y] -= 2;
                        x++;
                    }
                    else if (x + 1 < 6 && arr[x + 1][y] == 4 && arr[x + 2][y] == 0)//推箱子
                    {
                        arr[x + 1][y] -= 2;
                        arr[x][y] -= 2;
                        arr[x + 2][y] += 4;
                        x++;
                    }
                break;
            case 'w':
                    if (x - 1 < 6 && arr[x - 1][y] == 0)
                    {
                    arr[x - 1][y] += 2;
                    arr[x][y] -= 2;
                    x++;
                    }
                    else if (x - 1 < 6 && arr[x - 1][y] == 3)
                    {
                        arr[x - 1][y] -= 1;
                        arr[x][y] -= 2;
                        x++;
                    }
                    else if (x - 1 < 6 && arr[x - 1][y] == 4 && arr[x - 2][y] == 0)
                    {
                        arr[x - 1][y] -= 2;
                        arr[x][y] -= 2;
                        arr[x - 2][y] += 4;
                        x++;
                    }
                break;
            case 'a':
                    if (y - 1  < 6 && arr[x ][y - 1] == 0)
                    {
                    arr[x][y - 1] += 2;
                    arr[x][y] -= 2;
                    y++;
                    }
                    else if (y - 1  < 6 && arr[x ][y - 1] == 3)
                    {
                    arr[x][y - 1] -= 1;
                    arr[x][y] -= 2;
                    y++;
                    }
                    else if (y - 1  < 6 && arr[x ][y - 1] == 4 && arr[x][y - 2] == 0)
                    {
                    arr[x][y - 1] -= 2;
                    arr[x][y] -= 2;
                    arr[x][y - 2] += 4;
                    y++;
                    }
                break;
            case 'd':
                    if (y + 1  < 6 && arr[x ][y + 1] == 0)
                    {
                    arr[x][y + 1] += 2;
                    arr[x][y] -= 2;
                    y++;
                    }
                    else if (y + 1  < 6 && arr[x ][y + 1] == 3)
                    {
                    arr[x][y + 1] -= 1;
                    arr[x][y] -= 2;
                    y++;
                    }
                    else if (y + 1 < 6 && arr[x][y + 1] == 4 && arr[x][y + 2] == 0)
                    {
                        arr[x][y + 1] -= 2;
                        arr[x][y] -= 2;
                        arr[x][y + 2] += 4;
                        y++;
                    }
                break;
            }
        }
        return 0;
    }
    

  • 11
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值