C语言如何实现拼图游戏-创新互联
本篇内容介绍了“C语言如何实现拼图游戏”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
我们提供的服务有:成都网站设计、做网站、微信公众号开发、网站优化、网站认证、平谷ssl等。为数千家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的平谷网站制作公司制作一款拼图小游戏
#include// 引用图形库头文件 #include #include typedef enum { UP, DOWN, LEFT, RIGHT, UNKOWN } DRCT; const int level = 5; DRCT getKey() { char key; DRCT ret; key = _getch(); switch (key) { case 'a': case 'A': ret = LEFT; break; case 's': case 'S': ret = DOWN; break; case 'd': case 'D': ret = RIGHT; break; case 'w': case 'W': ret = UP; break; default: ret = UNKOWN; break; } return ret; } void drawImage(int layout[level][level], IMAGE* pimg) { int w, h; w = h = pimg->getwidth() / level; int x1, y1, x2, y2; setcolor(RGB(255, 255, 255)); setfillcolor(RGB(0, 0, 0)); for (int i = 0; i < level; ++i) { for (int j = 0; j < level; ++j) { x1 = j * w; y1 = i * h; if (layout[i][j] != -1) { x2 = layout[i][j] % level * w; y2 = layout[i][j] / level * h; putimage(x1, y1, w, h, pimg, x2, y2); } else { fillrectangle(x1, y1, x1 + w, y1 + h); } } } for (int i = 1; i < level; ++i) { line(0, i*h, pimg->getwidth(), i*h); } for (int i = 1; i < level; ++i) { line(i*w, 0, i*w, pimg->getheight()); } } int isGameOver(int layout[level][level]) { for (int i = 0; i < level; i++) { for (int j = 0; j < level; j++) { if (!(i == level - 1 && j == level - 1)) { if (layout[i][j] != i*level + j) { return 0; } } } } return 1; } int updateLayout(int layout[level][level], DRCT d) { int x = 0, t = 0; int row, col, i, j, a, b; for (i = 0; i < level; i++)//先找黑框坐标 i,j { for (j = 0; j < level; j++) { if (layout[i][j] == -1) { a = i; b = j; } } } switch (d) { case UP: { row = a + 1; col = b; break; } case DOWN: { row = a - 1; col = b; break; } case LEFT: { row = a; col = b + 1; break; } case RIGHT: { row = a; col = b - 1; break; } default: {row = a, col = b; break; } } //根据d的值算出与(i,j)交换的方块的坐标(row,col) if (row<0 || row>level - 1 || col<0 || col>level - 1) { x = 0; } else { x = 1; t = layout[a][b]; layout[a][b] = layout[row][col]; layout[row][col] = t; } //坐标(row,col)合法:交换 并且ret=1 // 否则: ret=0 return x; } int main() { int layout[level][level] = { 0,1,2,3,4, 5,6,7,8,9, 10,11,12,13,14, 15,16,17,18,19, 20,21,-1,22,23 }; initgraph(850, 500); // 创建绘图窗口,大小为 640x480 像素 IMAGE img,imgTip; loadimage(&img, _T("./image/0.jpg")); //图片为自己定义的路径 loadimage(&imgTip, _T("./image/0.jpg"),200,200); drawImage(layout, &img); putimage(650, 0, &imgTip); while (!isGameOver(layout)) { DRCT d = getKey(); //根据d的值修改layout数组中的数据 if (updateLayout(layout, d)) drawImage(layout, &img); } putimage(0, 0, &img); outtextxy(100, 50, _T("牛逼")); _getch(); closegraph(); // 关闭绘图窗口 }
“C语言如何实现拼图游戏”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!
本文标题:C语言如何实现拼图游戏-创新互联
网站地址:http://pwwzsj.com/article/diesec.html