您好,登錄后才能下訂單哦!
本篇內容介紹了“C++基于easyx怎么實現迷宮游戲”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
效果:
/*走迷宮*/ #define _CRT_SECURE_NO_DEPRECATEd #define _CRT_SECURE_NO_WARNINGS #include<graphics.h> #include<conio.h> #include<Windows.h> #include<stdio.h> #define LEFT 0//方向 #define RIGHT 1 #define UP 0//由于當前素材只有左右二個方向,所以上下共用了左右方向 #define DOWN 1 #define ROAD 0//地圖元素類型 #define WALL 1 #define ENTERX 1//入口 x列,y行 #define ENTERY 0 #define OUTX 11 //出口 x列,y行 #define OUTY 8 #define HUMANWIDTH 75 #define HUMANHEIGHT 130 #define WIDTH 12//地圖大小 #define HEIGHT 10 IMAGE img_human; IMAGE img_human_mask; IMAGE img_wall; IMAGE img_road; int moveNum[2] = { 0 };//當前動作序號 int direction;//上下左右四個方向 int human_witdh; int human_height; int x, y;//x列數,y行數 int map[HEIGHT][WIDTH] = {//地圖 { 1,1,1,1,1,1,1,1,1,1,1,1 }, { 0,0,0,1,1,1,1,1,1,1,1,1 }, { 1,1,0,1,1,1,1,0,1,1,1,1 }, { 1,1,0,0,1,1,1,0,1,1,1,1 }, { 1,1,1,0,1,1,1,0,1,1,1,1 }, { 1,1,1,0,1,1,1,0,1,1,1,1 }, { 1,1,1,0,1,1,1,0,1,1,1,1 }, { 1,1,1,0,0,0,0,0,0,0,1,1 }, { 1,1,1,1,1,1,1,1,1,0,0,0 }, { 1,1,1,1,1,1,1,1,1,1,1,1 }, }; void showbk() {//繪制背景 for (int j = 0; j < WIDTH; j++) for (int i = 0; i < HEIGHT; i++) if (map[i][j] == WALL) putimage(j * img_wall.getwidth(), i * img_wall.getheight(), img_wall.getwidth(), img_wall.getheight(), &img_wall, 0, 0, SRCCOPY); else putimage(j * img_wall.getwidth(), i * img_wall.getheight(), img_wall.getwidth(), img_wall.getheight(), &img_road, 0, 0, SRCCOPY); } void start()//初始化 { loadimage(&img_wall, _T(".\\walls.gif")); initgraph(img_wall.getwidth() * WIDTH, img_wall.getheight() * HEIGHT); loadimage(&img_human, _T(".\\行走素材圖.jpg")); loadimage(&img_human_mask,_T( ".\\行走素材圖mask.jpg")); human_witdh = 75;//img_human.getwidth()/4; human_height = 130;//img_human.getheight()/2; //putimage(x,y,HUMANWIDTH,HUMANHEIGHT,&img_human,0,0); loadimage(&img_road, _T(".\\road.gif")); x = 0; y = 1; } void updateWithoutInput() { } void drawRole(int x0, int y0)//繪制前景 { putimage((x - x0 / 4.0) * img_wall.getwidth() - 7, (y - y0 / 4.0) * img_wall.getheight() - 70, human_witdh, human_height, &img_human_mask, moveNum[direction] * human_witdh, direction * (human_height - 10), NOTSRCERASE); putimage((x - x0 / 4.0) * img_wall.getwidth() - 7, (y - y0 / 4.0) * img_wall.getheight() - 70, human_witdh, human_height, &img_human, moveNum[direction] * human_witdh, direction * (human_height - 10), SRCINVERT); } void show(int x0, int y0) { showbk(); //clearrectangle(x,y,x+human_witdh,y+human_height); //先顯示背景 //準備好遮罩MASK圖和源圖,三元光柵操作 drawRole(x0, y0); FlushBatchDraw(); Sleep(50); } void readRecordFile() {//讀取存檔 FILE* fp; int temp; fp = fopen(".\\record.dat", "r"); fscanf(fp, "%d %d", &x, &y); fclose(fp); } void WriteRecordFile() {//保存存檔 FILE* fp; int temp; fp = fopen(".\\record.dat", "w"); fprintf(fp, "%d %d ", x, y); fclose(fp); } void updateWithInput() {//增加過度 char input; int olddirection = direction; int oldx = x; int oldy = y; /******異步輸入檢測方向鍵狀態 if(GetAsyncKeyState(VK_LEFT)&0x8000) 向左 if(GetAsyncKeyState(VK_RIGHT)&0x8000) 向右 if(GetAsyncKeyState(VK_UP)&0x8000) 向上 if(GetAsyncKeyState(VK_DOWN)&0x8000) 向下 ********/ if (_kbhit()) { input = _getch(); switch (input) { case 'a':direction = LEFT; if (map[y][x - 1] == ROAD) x--; moveNum[direction] = 0; break; case 'd':direction = RIGHT; if (map[y][x + 1] == ROAD) x++; moveNum[direction] = 0; break; case 'w':direction = UP; if (map[y - 1][x] == ROAD) y--; moveNum[direction] = 0; break; case 's':direction = DOWN; if (map[y + 1][x] == ROAD) y++; moveNum[direction] = 0; break; case 'W':WriteRecordFile(); break; case 'R':readRecordFile(); break; } if (x != oldx || y != oldy) for (int i = 4; i > 0; i--) {//過渡動畫 show((x - oldx) * i, (y - oldy) * i); moveNum[direction]++;//動作序號,一個完整動作分解為四個姿勢 moveNum[direction] %= 4; } } } int main() { start(); BeginBatchDraw(); while (1) { show(0, 0); Sleep(50); if (x == OUTX && y == OUTY)//到達了出口 { outtextxy(0, 0, _T("reach target!")); Sleep(50); break; } updateWithoutInput(); updateWithInput(); } EndBatchDraw(); _getch(); closegraph(); return 0; }
“C++基于easyx怎么實現迷宮游戲”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。