大连网站建设开源,wordpress和dz,seo培训学什么,C 做的窗体怎么变成网站老鼠形象可以辨认#xff0c;可以用上下左右操纵老鼠;正确检测结果#xff0c;若老鼠在规定的时间内走到粮仓#xff0c;提示成功#xff0c;否则提示失败。代码分为3个文件#xff1a;main.cpp、play.h、play.cpp。
main.cpp:
#include iostream #include …老鼠形象可以辨认可以用上下左右操纵老鼠;正确检测结果若老鼠在规定的时间内走到粮仓提示成功否则提示失败。代码分为3个文件main.cpp、play.h、play.cpp。
main.cpp:
#include iostream #include windows.h #include play.h #include stdio.h using namespace std; /* run this program using the console pauser or add your own _getch, system(pause) or input loop */
int main() { int Count 0; cout 欢迎使用自制迷宫游戏Are you ready; Sleep(500); cout .; Sleep(500); cout .; Sleep(500); cout .; // system(cls); // cout\t\t*************************************************endl; // cout\t\t* *endl; // cout\t\t* 1.开始游戏 *endl; // cout\t\t* *endl; // cout\t\t* 2.编辑游戏 *endl; // cout\t\t* *endl; // cout\t\t* 3.退出游戏 *endl; // cout\t\t* *endl; // cout\t\t*************************************************endl; Player play_1(11, 11); Player play_2(13, 13); Player play_3(15, 15); int Menu; while (1) { if (Count 1) { system(cls); cout \t\t************************************************* endl; cout \t\t* * endl; cout \t\t* 1.开始游戏 * endl; cout \t\t* * endl; cout \t\t* 2.编辑游戏 * endl; cout \t\t* * endl; cout \t\t* 3.查看最短路径与所有路径 * endl; cout \t\t* * endl; cout \t\t* 4.退出游戏 * endl; cout \t\t* * endl; cout \t\t************************************************* endl; } else if (Count 1) { system(cls); cout \t\t************************************************* endl; cout \t\t* * endl; cout \t\t* 1.开始游戏 * endl; cout \t\t* * endl; cout \t\t* 2.编辑游戏 * endl; cout \t\t* * endl; cout \t\t* 3.退出游戏 * endl; cout \t\t* * endl; cout \t\t************************************************* endl; } cin Menu; system(cls); if (Menu 1) { cout *******************************游戏说明***************************** endl; cout 请使用键盘↑↓←→移动老鼠在规定时间内用尽量少的步骤帮老鼠找到粮仓 endl; system(cls); cout \t\t 第一关 endl; Sleep(500); play_1.show_Map(); play_1.Move(); play_1.KeepMap(); cout \t\t 请进行你的选择 endl; cout \t\t 1.继续游戏 endl; cout \t\t 2.结束游戏 endl; int choice, choice2; cin choice; if (choice 1) { cout \t\t 第二关 endl; Sleep(500); play_2.show_Map(); play_2.Move(); play_2.KeepMap(); cout \t\t 请进行你的选择 endl; cout \t\t 1.继续游戏 endl; cout \t\t 2.结束游戏 endl; cin choice2; if (choice2 1) { cout \t\t 第三关 endl; cout \t\t 请进行你的选择 endl; cout \t\t 1.继续游戏 endl; cout \t\t 2.结束游戏 endl; Sleep(500); play_3.show_Map(); play_3.Move(); play_3.KeepMap(); cout 您已通关感谢使用 endl; break; } else if (choice2 2) { cout 游戏结束感谢使用 endl; break; } } else if (choice 2) { cout 游戏结束感谢使用 endl; break; } } else if (Menu 2) { cout \t 请选择想要编辑的关卡 endl; cout \t\t 1.第一关 endl; cout \t\t 2.第二关 endl; cout \t\t 3.第三关 endl; int choice3; cin choice3; if (choice3 1) { play_1.EditorMap(); } else if (choice3 2) { play_2.EditorMap(); } else if (choice3 3) { play_3.EditorMap(); } system(cls); } else if (Menu 3) { cout 亲您只有一次查看机会哦╭●★★●╰。。。; Sleep(2000); if (Count 1) { Count; cout \t\t 请输入想要查看的关卡 endl; cout \t\t 1.第一关 endl; cout \t\t 2.第二关 endl; cout \t\t 3.第三关 endl; int Choice; cin Choice; if (Choice 1) { play_1.Pre_Short(); } else if (Choice 2) { play_2.Pre_Short(); } else if (Choice 3) { play_3.Pre_Short(); } } } if (Count 1) { if (Menu 4) { cout 感谢使用 endl; break; } } else if (Count 1) { if (Menu 3) { cout 感谢小主的使用 endl; break; } } } return 0; } play.cpp:
#include iostream #include play.h #include windows.h #include conio.h #include time.h using namespace std;
void Player::Push() { front rear 1; rear 0; top -1; top; Mp[top] MpQueue[front - 1]; int direc1[4][2] { 1,0,0,1,0,-1,-1,0 }; //定义方向 while (front ! rear) { front--; for (int j 0; j 4; j) { if (Mp[top].x direc1[j][0] MpQueue[front - 1].x Mp[top].y direc1[j][1] MpQueue[front - 1].y) { top; Mp[top] MpQueue[front - 1]; } } } }
void Player::show() { cout 鼠; for (int i 0; i top; i) { cout ( Mp[i].x , Mp[i].y ) -; } cout 仓; system(pausenul); }
void Player::Move() { time_t Start; time_t Over; int Count 100; char Enter; int Time 30; int a, b 0, c Map_Length / 2, d Map_Width / 2, i, j; Start time(NULL); while (Time 0) { Over time(NULL); a Over - Start; if (_kbhit() 0) { if (b ! a) { system(cls); for (i 1; i Map_Length; i) { for (j 1; j Map_Length; j) { if (Map[i][j].data 1) { cout ■; } else if (Map[i][j].data 0) { cout ; } else if (Map[i][j].data 2) { cout 鼠; } else if (Map[i][j].data 3) { cout 仓; } else if (Map[i][j].data 4) { cout ; } } cout endl; } cout 剩余时间 Time-- 秒 endl; b a; if (Time -1) { system(cls); cout 闯关失败 endl; exit(1); break; } } } if (_kbhit() ! 0) { Enter _getch(); system(cls); if (Enter -32) { Enter _getch(); if (Enter 75) { if (Map[c][d - 1].data 1) { cout 老鼠不能穿墙 endl; } else { Map[c][d - 1].data 2; Map[c][d].data 4; d d - 1; Count--; } } else if (Enter 77) { if (Map[c][d 1].data 1) { cout 老鼠不能穿墙 endl; } else { Map[c][d 1].data 2; Map[c][d].data 4; d d 1; Count--; } } else if (Enter 72) { if (Map[c - 1][d].data 1) { cout 老鼠不能穿墙 endl; } else { Map[c - 1][d].data 2; Map[c][d].data 4; c c - 1; Count--; } } else if (Enter 80) { if (Map[c 1][d].data 1) { cout 老鼠不能穿墙 endl; } else { Map[c 1][d].data 2; Map[c][d].data 4; c c 1; Count--; } } } for (i 1; i Map_Length; i) { for (j 1; j Map_Length; j) { if (Map[i][j].data 1) { cout ■; } else if (Map[i][j].data 0) { cout ; } else if (Map[i][j].data 2) { cout 鼠; } else if (Map[i][j].data 3) { cout 仓; } else if (Map[i][j].data 4) { cout ; } } cout endl; } if (Map[Map_Length - 1][Map_Length - 1].data ! 3) { system(cls); cout 闯关成功 endl; cout 您的积分为 Count endl; break; } } } }
void Player::KeepMap() //保存老鼠走过的路径 { for (int i 1; i Map_Length; i) { for (int j 1; j Map_Width; j) { if (Map[i][j].data 0) { cout ; } else if (Map[i][j].data 1) { cout ■; } else if (Map[i][j].data 2) { cout 鼠; } else if (Map[i][j].data 3) { cout 仓; } else if (Map[i][j].data 4) { cout ◇; } } cout endl; } }
void Player::show_Map() //编辑地图 { int i, j; //srand((unsigned)time(NULL)); //如果不适用随机数种子那么每次程序启动生成的随机数(rand)都是一样的 GenerateMap(2 * (rand() % (Map_Length / 2 1)), 2 * (rand() % (Map_Width / 2 1))); Map[Map_Length / 2][Map_Width / 2].data 2; //初始化鼠 当二维数组的值为2时代表鼠 Map[Map_Length - 1][Map_Width - 1].data 3; //初始化仓 当二维数组的值为3时代表仓 for (i 1; i Map_Length; i) { for (j 1; j Map_Width; j) { if (Map[i][j].data 1) { cout ■; } else if (Map[i][j].data 0) { cout ; } else if (Map[i][j].data 2) { cout 鼠; } else if (Map[i][j].data 3) { cout 仓; } } cout endl; } }
void Player::Pre_Short() { rear front -1; for (int i 1; i Map_Length 1; i) //1-Map_Length才是想要的 { for (int j 1; j Map_Width 1; j) { if (i 0 || i Map_Length 1 || j 0 || j Map_Width 1) { Map[i][j].data 0; } else { Map[i][j].data 1; } } } for (int i 0; i Map_Length; i) { for (int j 0; j Map_Width; j) { Map[i][j].visited 0; } } show_Map(); system(cls); int m Map_Length - 1, n Map_Width - 1; MapPoint p; p.x m, p.y n, p.visited 1; p.data 3; ShortMap(p); show(); while (top ! -1) { top--; } }
void Player::EditorMap() { int c Map_Length / 2; int d Map_Width / 2; show_Map(); system(cls); char Enter; while (1) { for (int i 1; i Map_Length; i) { for (int j 1; j Map_Length; j) { if (Map[i][j].data 1) { cout ■; } else if (Map[i][j].data 0) { cout ; } else if (Map[i][j].data 2) { cout 鼠; } else if (Map[i][j].data 3) { cout 仓; } else if (Map[i][j].data 4) { cout ; } } cout endl; } cout 输入回车键保存修改 endl; Enter _getch(); system(cls); if (Enter -32) { Enter _getch(); if (Enter 75) { if (Map[c][d - 1].data 1) { cout 老鼠不能穿墙 endl; } else { Map[c][d - 1].data 2; Map[c][d].data 4; d d - 1; } } else if (Enter 77) { if (Map[c][d 1].data 1) { cout 老鼠不能穿墙 endl; } else { Map[c][d 1].data 2; Map[c][d].data 4; d d 1; } } else if (Enter 72) { if (Map[c - 1][d].data 1) { cout 老鼠不能穿墙 endl; } else { Map[c - 1][d].data 2; Map[c][d].data 4; c c - 1; } } else if (Enter 80) { if (Map[c 1][d].data 1) { cout 老鼠不能穿墙 endl; } else { Map[c 1][d].data 2; Map[c][d].data 4; c c 1; } } } if (Enter 97) { if (Map[c][d - 1].data 1) { Map[c][d - 1].data 0; } else if (Map[c][d - 1].data 0 || Map[c][d - 1].data 4) { Map[c][d - 1].data 1; } } else if (Enter 119) { if (Map[c - 1][d].data 1) { Map[c - 1][d].data 0; } else if (Map[c - 1][d].data 0 || Map[c - 1][d].data 4) { Map[c - 1][d].data 1; } } else if (Enter 100) { if (Map[c][d 1].data 1) { Map[c][d 1].data 0; } else if (Map[c][d 1].data 0 || Map[c][d 1].data 4) { Map[c][d 1].data 1; } } else if (Enter 115) { if (Map[c 1][d].data 1) { Map[c 1][d].data 0; } else if (Map[c 1][d].data 0 || Map[c 1][d].data 4) { Map[c 1][d].data 1; } } else if (Enter 0x0D) { Map[c][d].data 0; break; } } }
void Player::ShortMap(MapPoint M) { M.visited 1; for (int i 1; i Map_Length; i) { for (int j 1; j Map_Length; j) { if (Map[i][j].data 1) { cout ■; } else if (Map[i][j].data 0) { cout ; } else if (Map[i][j].data 2) { cout 鼠; } else if (Map[i][j].data 3) { cout 仓; } else if (Map[i][j].data 4) { cout ; } } cout endl; } front rear -1; rear; MpQueue[rear] M; int direc1[4][2] { 1, 0, 0, 1, 0, -1, -1, 0 }; //d定义四个方向 while (front ! rear) { front; for (int j 0; j 4; j) { if ((Map[MpQueue[front].x direc1[j][0]][MpQueue[front].y direc1[j][1]].data 0 || Map[MpQueue[front].x direc1[j][0]][MpQueue[front].y direc1[j][1]].data 2 || Map[MpQueue[front].x direc1[j][0]][MpQueue[front].y direc1[j][1]].data 4) Map[MpQueue[front].x direc1[j][0]][MpQueue[front].y direc1[j][1]].visited 0 MpQueue[front].x Map_Width MpQueue[front].x 1 MpQueue[front].y Map_Length MpQueue[front].y 1) { rear; MpQueue[rear].x MpQueue[front].x direc1[j][0]; MpQueue[rear].y MpQueue[front].y direc1[j][1]; Map[MpQueue[front].x direc1[j][0]][MpQueue[front].y direc1[j][1]].visited 1; if (MpQueue[rear].x (Map_Length / 2) MpQueue[rear].y (Map_Width / 2)) { flag 1; break; } } } if (flag 1) { break; } } Push(); }
void Player::GenerateMap(int x, int y) { int direction[4][2] { 1,0,0,1,0,-1,-1,0 }; //定义方向 int i, j, temp; for (i 0; i 4; i) //打乱方向 { j rand() % 4; //随机选取方向 temp direction[i][0]; direction[i][0] direction[j][0]; direction[j][0] temp; temp direction[i][1]; direction[i][1] direction[j][1]; direction[j][1] temp; } Map[x][y].data 0; for (i 0; i 4; i) //任何两个空的地方都有路可走 { if (Map[x 2 * direction[i][0]][y 2 * direction[i][1]].data 1) { Map[x direction[i][0]][y direction[i][1]].data 0; //打通墙 GenerateMap(x 2 * direction[i][0], y 2 * direction[i][1]); } } }
Player::Player(int m, int n) { int i, j; Map_Length m, Map_Width n; for (i 1; i Map_Length 1; i) //1-Map_Length才是想要的 { for (j 1; j Map_Width 1; j) { if (i 0 || i Map_Length 1 || j 0 || j Map_Width 1) { Map[i][j].data 0; } else { Map[i][j].data 1; } } } for (int i 0; i Size; i) { for (int j 0; j Size; j) { Map[i][j].visited 0; } } flag 0; front rear -1; top -1; }
play.h:
#ifndef PLAY_H #define PLAY_H const int Size 100;
struct MapPoint { int data; int x, y; //保存路径的x与y坐标 int visited; //是否访问过的标签 };
class Player { private: int top; int flag; int x, y; int rear; int front; int Mouse_x, Mouse1_y; //老鼠的位置 int Map_Length, Map_Width; MapPoint Mp[Size]; //栈 MapPoint MpQueue[230]; //队列
public: Player(int m, int n); void Push(); //入栈操作 void show(); void Move(); //老鼠移动 void KeepMap(); //保存路径 void PlayGame(); //开始游戏 void show_Map(); //显示地图 void Pre_Short(); void EditorMap(); //编辑地图 void ShortMap(MapPoint M); //计算最短路径 void GenerateMap(int x, int y); //生成地图 MapPoint Map[Size][Size]; //地图数组 };
#endif