自己去我的资源下载 ege娘,免费的,有教程的
主要代码:
有一个bug额,没有改,w a s d 是 上 左 下 右
// 走迷宫_图形版.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" // maze_v0.2.cpp : 定义控制台应用程序的入口点。 // #include <graphics.h> #include "stdafx.h" #include <iostream> #include <cstdlib> #include <conio.h> #include "resource.h" using namespace std; #define WIDTH 10 #define LENGTH 20 //函数声明// void draw(void); void moveUp(void); void moveDown(void); void moveLeft(void); void moveRight(void); void change(void); int map[WIDTH][LENGTH] = { {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 2, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1}, {1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1}, {1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1}, {1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1}, {1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1}, {1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1}, {1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 3, 1}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} }; int x = 1;//初始x坐标 int y = 1;//初始y坐标 int xx = 0; int yy = 0; bool victory = false; int main() { initgraph(460, 230);//initgraph(230,460);//230 460 char select; setrendermode(RENDER_MANUAL);//防止刷图闪 draw(); // setrendermode(RENDER_MANUAL); while (!victory) { setrendermode(RENDER_AUTO); select = getch(); switch(select) { case 72: case 'W': case 'w': moveUp(); change(); setrendermode(RENDER_MANUAL); cleardevice();//system("cls"); draw(); // setrendermode(RENDER_MANUAL); break; case 80: case 'S': case 's': moveDown(); change(); setrendermode(RENDER_MANUAL); cleardevice();//system("cls"); draw(); break; case 75: case 'A': case 'a': moveLeft(); change(); setrendermode(RENDER_MANUAL); cleardevice();//system("cls"); draw(); break; case 77: case 'D': case 'd': moveRight(); change(); setrendermode(RENDER_MANUAL); cleardevice();//system("cls"); draw(); break; } // setrendermode(RENDER_AUTO); } cout << "win!\n"; closegraph(); return 0; } void draw()//draw a map// { PIMAGE img; int val_1 = 0; int val_2 = 0; for (val_1=0; val_1<WIDTH; ++val_1)//<>--------------遍历数组// { for (val_2=0; val_2<LENGTH; ++val_2) { img = newimage(); if (1 == map[val_1][val_2])//<>-----------------画墙// { getimage(img, "JPG", "qiang"); // cout << "# "; // putimage(val_1*23, val_2*23, img); putimage(val_2*23, val_1*23, img);//注意顺序 } else if (0 == map[val_1][val_2])//<>-----------------画路// { getimage(img, "JPG", "kongge"); // cout << " "; // putimage(val_1*23, val_2*23, img); putimage(val_2*23, val_1*23, img); } else if (2 == map[val_1][val_2])//<>-----------------画人// { getimage(img, "JPG", "ren"); // cout << "·"; // putimage(val_1*23, val_2*23, img); putimage(val_2*23, val_1*23, img); } else if (3 == map[val_1][val_2])//<>-----------------画出口// { getimage(img, "JPG" ,"end"); // cout << "* "; // putimage(val_1*23, val_2*23, img); putimage(val_2*23, val_1*23, img); } delimage(img); } cout << endl; } } void moveUp()//上移// { if (1 != map[x-1][y]) { xx = x; yy = y; x = x - 1; } else { return ; } } void moveDown()//下移// { if (1 != map[x+1][y]) { xx = x; yy = y; x = x + 1; } else { return ; } } void moveLeft()//左移// { if (1 != map[x][y-1]) { xx = x; yy = y; y = y - 1; } else { return ; } } void moveRight()//右移// { if (1 != map[x][y+1]) { xx = x; yy = y; y = y + 1; } else { return ; } } void change() { if (3 == map[x][y]) { victory = true; } map[x][y] = 2; map[xx][yy] = 0; }