您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本篇内容主要讲解“怎么用C语言实现游戏项目推箱子效果”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么用C语言实现游戏项目推箱子效果”吧!
首先是我们用二维数组特定的数字描绘出这个地图
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; }
到此,相信大家对“怎么用C语言实现游戏项目推箱子效果”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。