时间:2022-10-25 09:31:57 | 栏目:C代码 | 点击:次
我们今天就来创造出属于我们自己的《推箱子》,GOGOGO!!!
首先是我们用二维数组特定的数字描绘出这个地图
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 };
然后来绘制我们的推箱子地图
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"); } }
之后就是我们的游戏函数,怎样去用什么按键去控制我们的角色
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; } }
再然后就是我们如何去判断游戏的结果
//胜负的判断: 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; }
最后运行我们的主函数就行啦
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主还会发布更多的项目源码以及学习资料,有什么问题可以回帖留言,我尽量回答。希望和大家一起学习进步!!!