C语言 完整游戏项目推箱子详细代码

时间:2022-06-15 07:16:37

C语言 完整游戏项目推箱子详细代码

话不多说

我们今天就来创造出属于我们自己的《推箱子》,GOGOGO!!!

直接开始吧

首先是我们用二维数组特定的数字描绘出这个地图

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
int cas = 0;
int map[3][8][8] =
{
    1,1,1,1,1,1,1,1,
    1,3,4,0,0,4,3,1,
    1,0,1,3,0,1,0,1,
    1,0,1,4,0,1,0,1,
    1,0,0,5,0,0,0,1,
    1,0,1,0,0,1,0,1,
    1,3,4,0,0,4,3,1,
    1,1,1,1,1,1,1,1,
 
    1,1,1,1,1,1,1,1,
    1,3,4,0,0,4,3,1,
    1,0,1,3,0,1,0,1,
    1,0,1,4,0,1,0,1,
    1,3,4,5,0,0,0,1,
    1,0,1,0,0,1,0,1,
    1,3,4,0,0,4,3,1,
    1,1,1,1,1,1,1,1,
 
    1,1,1,1,1,1,1,1,
    1,3,4,0,0,4,3,1,
    1,0,1,3,0,1,0,1,
    1,0,1,4,0,1,0,1,
    1,3,4,5,0,4,3,1,
    1,0,1,0,0,1,0,1,
    1,3,4,0,0,4,3,1,
    1,1,1,1,1,1,1,1
};

然后来绘制我们的推箱子地图

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
void  drawGraph()
{
    for (int i = 0; i < 8; i++)
    {
        for (int j = 0; j < 8; j++)
        {
            //算贴图的坐标
            int x = 50 * j;
            int y = 50 * i;
            switch (map[cas][i][j])
            {
            case 0:
                //一个汉字符号占用两个位置
                //printf("  ");
                putimage(x, y, img + 0);
                break;
            case 1:
                putimage(x, y, img + 1);
                //printf("■");
                break;
            case 3:
                putimage(x, y, img + 2);
                //printf("☆");
                break;
            case 4:
                putimage(x, y, img + 3);
                //printf("★");
                break;
            case 5:
            case 8:
                putimage(x, y, img + 4);
                //printf("人");
                break;
            case 7:
                putimage(x, y, img + 5);
                //printf("●");
                break;
            }
        }
        //printf("\n");
    }
}

之后就是我们的游戏函数,怎样去用什么按键去控制我们的角色

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
void keyDown()
{
    int userKey = _getch(); //不可见输入
    //定位:找到人的位置
    int i = 0;
    int j = 0;
    for (i = 1; i < 8; i++)
    {
        for (j = 1; j < 8; j++)
        {
            if (map[cas][i][j] == 5 || map[cas][i][j] == 8)
            {
                goto NEXT;
            }
        }
    }
    NEXT:
 
    //我们这个游戏用什么按键去玩
    switch (userKey)
    {
    case 'W':
    case 'w':
    case 72:
        if (map[cas][i - 1][j] == 0 || map[cas][i - 1][j] == 3)
        {
            map[cas][i][j] -= 5;
            map[cas][i - 1][j] += 5;
        }
        if (map[cas][i - 1][j] == 4 || map[cas][i - 1][j] == 7)
        {
            if (map[cas][i - 2][j] == 0 || map[cas][i - 2][j] == 3)
            {
                map[cas][i][j] -= 5;
                map[cas][i - 1][j] += 1;
                map[cas][i - 2][j] += 4;
            }
        }
        break;
    case 's':
    case 'S':
    case 80:
        if (map[cas][i + 1][j] == 0 || map[cas][i + 1][j] == 3)
        {
            map[cas][i][j] -= 5;
            map[cas][i + 1][j] += 5;
        }
        if (map[cas][i + 1][j] == 4 || map[cas][i + 1][j] == 7)
        {
            if (map[cas][i + 2][j] == 0 || map[cas][i +2][j] == 3)
            {
                map[cas][i][j] -= 5;
                map[cas][i + 1][j] += 1;
                map[cas][i + 2][j] += 4;
            }
        }
        break;
    case 'a':
    case 'A':
    case 75:
        if (map[cas][i][j - 1] == 0 || map[cas][i][j - 1] == 3)
        {
            //a+=1  a=a+1 复合赋值运算符
            map[cas][i][j] -= 5;
            map[cas][i][j - 1] += 5;
        }
        if (map[cas][i][j - 1] == 4 || map[cas][i][j - 1] == 7)
        {
            if (map[cas][i][j - 2] == 0 || map[cas][i][j - 2] == 3)
            {
                map[cas][i][j] -= 5;
                map[cas][i][j - 1] += 1;
                map[cas][i][j - 2] += 4;
            }
        }
        break;
    case 'd':
    case 'D':
    case 77:
        if (map[cas][i][j + 1] == 0 || map[cas][i][j + 1] == 3)
        {
            map[cas][i][j] -= 5;
            map[cas][i][j + 1] += 5;
        }
        if (map[cas][i][j + 1] == 4 || map[cas][i][j + 1] == 7)
        {
            if (map[cas][i][j + 2] == 0 || map[cas][i][j + 2] == 3)
            {
                map[cas][i][j] -= 5;
                map[cas][i][j + 1] += 1;
                map[cas][i][j + 2] += 4;
            }
        }
        break;
    }
}

再然后就是我们如何去判断游戏的结果

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//胜负的判断:
int gameOver()
{
    //地图上没有箱子就可以结束
    for (int i = 0; i < 8; i++)
    {
        for (int j = 0; j < 8; j++)
        {
            if (map[cas][i][j] == 4)
            {
                return 0;
            }
        }
    }
    return 1;
}

最后运行我们的主函数就行啦

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{
    loadResource();
    mciSendString("open 1.mp3", 0, 0, 0);
    mciSendString("play 1.mp3 repeat", 0, 0, 0);
    initgraph(50 * 8, 50 * 8);
    while (1)
    {
        drawGraph();
        if (gameOver())
        {
            cas++;          //变换关卡
            if(cas==3)
                break;
        }
        keyDown();
        //system("cls");
    }
    closegraph();
    //printf("GameOver!\n");
    return 0;
}

其实代码并不是很多,当然啦,如果同学们想更加完善,可以增加关卡设定,再优化一下我们的开始界面以及游戏界面也是可以的,大家快去尝试吧!!! 希望看完了的同学可以获得自己想要的知识,也感谢大家的耐心观看,在这里想得到大家一波关注,后续UP主还会发布更多的项目源码以及学习资料,有什么问题可以回帖留言,我尽量回答。希望和大家一起学习进步!!!

C语言 完整游戏项目推箱子详细代码

到此这篇关于C语言 完整游戏项目推箱子详细代码的文章就介绍到这了,更多相关C语言 推箱子内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_45713725/article/details/121098938