Get the App
SLTechnology News&Howtos  ›  Development  › 

How does C++ realize Maze Game based on easyx

Shulou Source: shulou.com Published: 2022-05-31 17:49:50 10月03日 Update

This article introduces the relevant knowledge of "how C++ implements maze games based on easyx". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Effect:

/ * Walk the maze * / # define _ CRT_SECURE_NO_DEPRECATEd#define _ CRT_SECURE_NO_WARNINGS#include#include#include#include#define LEFT 0 define RIGHT 1#define UP / Direction # define RIGHT 1#define UP 0 pick / since the current material is only in the left and right directions, the left and right directions are shared up and down, so the left and right directions are shared up and down / map element type # define WALL 1#define ENTERX 1 pick / entry x column Y line # define ENTERY 0#define OUTX 11 / / exit x column, y line # define OUTY 8#define HUMANWIDTH 75#define HUMANHEIGHT 130#define WIDTH 12 define OUTY 8#define HUMANWIDTH 75#define HUMANHEIGHT 130#define WIDTH / map size # define HEIGHT 10IMAGE img_human IMAGE img_human_mask;IMAGE img_wall;IMAGE img_road;int moveNum [2] = {0}; / / current action sequence number int direction;// up and down four directions int human_witdh;int human_height;int x, y / / number of x columns The number of rows int map [HEIGHT] [WIDTH] = {/ / Map {1meme] [WIDTH] = {1meme] [WIDTH] = {/ / Map {1meme] [WIDTH] = {1meme1phion] = {0meme1pyrrine 1pyror1}, {0pje 1pje 1pyrrine 1pyrrine 1pyrrine 1}, {1pyrort 1phitem1pentium 1pyrort 1mme1pr 1pentin 1}, {1pr 1pr 0pr 1pr 1jp1} {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 () {/ / draw background 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; iMel -) {/ / transition animation show ((x-oldx) * I, (y-oldy) * I); moveNum [direction] +; / / Action sequence number, a complete action is decomposed into four positions moveNum [direction]% = 4;} int main () {start (); BeginBatchDraw () While (1) {show (0,0); Sleep (50); if (x = = OUTX & & y = = OUTY) / / reached exit {outtextxy (0,0, _ T ("reach target!"); Sleep (50); break;} updateWithoutInput (); updateWithInput ();} EndBatchDraw () _ getch (); closegraph (); return 0;}

This is the end of the introduction of "how C++ implements the maze game based on easyx". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Directions mazes actions maps clockwise up and down content serial numbers more knowledge exit practical up and down learning and then elements entrances animations only dilemmas. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Xiaomi MariaDB macOS Linux