easyX 进行的贴图界面,迷宫依然是那个迷宫,但是此次却是图形界面
2019-08-15
一张图片先镇博客
其实代码呢,和之前上一篇帖子是大差不差:http://www.anyuer.club/?id=41
只不过这次用上了图形界面库easyX,总的来说,在打印数组的地方贴上图片就可以了。。。
不过需要注意的是你必须先按照easyx库。不会安装请访问:http://www.anyuer.club/?id=86
当然咯,加载图片肯定需要你把图片放到和程序在一起的目录,在编译的时候如果是VS2013就按照如图操作
先右键解决方案,然后再点击在文件资源管理器中打开文件夹 ,你把图片资源放到这个文件就可以了
代码如下
#include <stdio.h> #include <graphics.h> #include <windows.h> #include <stdio.h> #include <conio.h> #include <windows.h> #define N 15 //生成地图数组 int map[N][N] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, }; //申明全局变量 int i; int j; IMAGE pic[N * N ];//申明一个图片数组 //打印地图程序 void print() { int x = 0, y = 0; for (x = 0; x<N; x++) { for (y = 0; y<N; y++) { if (map[x][y] == 0) { printf("■"); loadimage(&pic[x*15 +y ], "obstacle.jpg", 50, 50);//载入图片 putimage(y * 50, x * 50, &pic[x * 15 + y]);//显示图片 } else if (map[x][y] == 1) { printf(" "); loadimage(&pic[x * 15 + y], "road.jpg", 50, 50); putimage(y * 50, x * 50, &pic[x * 15 + y]); } else if (map[x][y] == 2) { printf("♂"); loadimage(&pic[x * 15 + y], "role.jpg", 50, 50); putimage(y * 50, x * 50, &pic[x * 15 + y]); } else if (map[x][y] == 3) { printf("☆"); loadimage(&pic[x * 15 + y], "aim.jpg", 50, 50); putimage(y * 50, x * 50, &pic[x * 15 + y]); } } printf("\n"); } } //找到移动的正 void find() { int x = 0, y = 0; for (x = 0; x<N; x++) { for (y = 0; y<N; y++) { if (map[x][y] == 2) { i = x; j = y; break; } } } } void down() { if (map[i + 1][j] == 0) { } else { map[i + 1][j] = 2; map[i][j] = 1; system("cls"); print(); } } void up() { if (map[i - 1][j] == 0) { } else { map[i - 1][j] = 2; map[i][j] = 1; system("cls"); print(); } } void left() { if (map[i][j - 1] == 0) { } else { map[i][j - 1] = 2; map[i][j] = 1; system("cls"); print(); } } void right() { if (map[i][j + 1] == 0) { } else if (map[i][j + 1] == 3) { system("cls"); printf("你已经获胜"); } else { map[i][j + 1] = 2; map[i][j] = 1; system("cls"); print(); } } int main() { initgraph(750, 750, SHOWCONSOLE); print(); char choice;//移动 while (1) { choice = getch(); find(); switch (choice) { case 'w': up(); break; case 's': down(); break; case 'a': left(); break; case 'd': right(); break; } } system("pause"); }
发表评论: