_c language of Maze game code based on C language

Source: Internet
Author: User
Tags clear screen

This article illustrates the method of realizing maze game based on C language, and the code has more detailed annotation, which is easy for readers to understand. Through the game code can be very good review of C language recursive algorithm and process Control knowledge, I believe that learning game development friends have a certain value for reference.

The complete instance code is as follows:

#include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> #include < dos.h> #define N 20/* Maze size, can change/int oldmap[n][n];/* recursive array, with global variables to save time/int Yes=0;/*yes is to determine whether to find the road signs, 1 found, 0 did not find/int way [100] [The 2],wayn=0;/*way array is used to display the route, Wayn is to statistically walk a few lattice/void Init (void);/* Graphics initialization/void close (void);/* graphics off/void drawpeople (int *x, int *y,int n);//* Painting manual Explorer/void Peoplefind (int (*x) [n]);/* Manual Discovery/void waycopy (int (*x) [N],int (*y) [n]); * for recursion in 8 directions, Copy the old maze map to the new array/int findway (int (*x) [N],int i,int J);/* Automatic discovery function/void Maprand (int (*x) [N]);/* Randomly generated maze function/void Prmap (int (*x) [ N]/* Output maze graph function/void result (void);/* output results processing/void find (void);/* Successful processing/void Notfind (void);/* failure handling/void main (void)/* Main function
  * * {int map[n][n];/* Maze array/char ch;
  CLRSCR ();
  printf ("\ nplease select Hand (1) Else auto\n");/* Select discovery Mode/scanf ("%c", &ch); Init (); /* Initialize */maprand (MAP)/* Generate Maze/prmap (map),/* Show Maze Map * * * * (ch== ' 1 ') peoplefind (map);/* Manual Discovery/Else Findway (map,1,1 /* The system automatically starts at the place where the subscript 1,1Explore * * result ()/* Output/close ();
  } void Init (void)/* graphic initialization/{int gd=detect,gm;
Initgraph (&AMP;GD,&AMP;GM, "C:\\TC"); } void drawpeople (int *x,int *y,int N)/* Draw Manual Control Chart * * * If the following two sentences are commented out, then the path of the manual walk is shown, * * Setfillstyle (Solid_fill,white);
Set the white solid fill style/bar (100+ (*y) *15-6,50+ (*x) *15-6,100+ (*y) *15+6,50+ (*x) *15+6); /* Restore the original channel/switch (n)/* To determine the changes in the X,y, 8 changes in the direction * * Case 1: (*x)--;break;/* on Case 2: (*x)--;(*y) ++;break;/* Right/CAs E 3: (*y) ++;break; /* Right * * Case 4: (*x) + +;(*y) ++;break; /* Right Lower * * Case 5: (*x) ++;break; /* under */Case 6: (*x) + +;(*y)--;break; /* Left Lower * * Case 7: (*y)--;break; /* Left/* Case 8: (*x)--;(*y)--;break;
*//} setfillstyle (solid_fill,red);/* New location to display Explorer/bar (100+ (*y) *15-6,50+ (*x) *15-6,100+ (*y) *15+6,50+ (*x) *15+6);
  } void Peoplefind (int (*MAP) [N])/* Manual lookup/{int x,y;
  The char c=0;/* receives the key variable * * x=y=1;/* the initial position of manual lookup/setcolor (11);
  Line (500,200,550,200);
  Outtextxy (570,197, "D");
  Line (500,200,450,200);
  Outtextxy (430,197, "a");
  Line (500,200,500,150); OuttexTxy (497,130, "w");
  Line (500,200,500,250);
  Outtextxy (497,270, "X");
  Line (500,200,450,150);
  Outtextxy (445,130, "Q");
  Line (500,200,550,150);
  Outtextxy (550,130, "E");
  Line (500,200,450,250);
  Outtextxy (445,270, "Z");
  Line (500,200,550,250);
  Outtextxy (550,270, "C"); * above is to draw 8 directions of the control introduction/SetColor (yellow);
  Outtextxy (420,290, "Press ' Enter ' to End");/* Press ENTER closing/Setfillstyle (solid_fill,red); Bar (100+y*15-6,50+x*15-6,100+y*15+6,50+x*15+6)/* Entry position Display/while (C!=13)/* If pressed not enter/{C=getch ();
   */if (c== ' W ' &&map[x-1][y]!=1) drawpeople (&x,&y,1); */Else if (c== ' e ' &&map[x-1][y+1]!=1)
   Drawpeople (&x,&y,2);/* Right up/else if (c== ' d ' &&map[x][y+1]!=1) drawpeople (&x,&y,3);/* Right/* else if (c== ' C ' &&map[x+1][y+1]!=1) drawpeople (&x,&y,4);/* lower Right/else if (c== ' x ' &&map[x+1] [y]!=1) Drawpeople (&x,&y,5);/* under */else if (c== ' z ' &&map[x+1][y-1]!=1) drawpeople (&x,&y,6); /* Left Lower */else if (c== ' a ' &&map[x][y-1]!=1) drawpeople (&x,&y,7); /* Left */else if (c== ' Q ' &&map[x-1][y-1]!=1) drawpeople (&x,&y,8); /* Left Upper */} setfillstyle (Solid_fill,white);
  * * Eliminate the Red Explorer, restore the original maze map/bar (100+Y*15-6,50+X*15-6,100+Y*15+6,50+X*15+6); if (x==n-2&&y==n-2)/* Manual control to find successful words * * * Yes=1;
  /* If the success flag is 1*/} void waycopy (int (*oldmap) [N],int (*map) [N])/* Copy maze array/{int i,j;
for (i=0;i<n;i++) for (j=0;j<n;j++) oldmap[i][j]=map[i][j]; int Findway (*MAP) [N],int i,int J)/* recursive find Road/{if (i==n-2&&j==n-2)/* Go to the exit/{yes=1;/* sign is 1, indicating success * * * Retu
  Rn The place that map[i][j]=1;/* passes becomes 1*/waycopy (OLDMAP,MAP);
   * Copy Maze Map * */if (oldmap[i+1][j+1]==0&&!yes)/* To determine whether the lower right to go/{Findway (oldmap,i+1,j+1);
 if (yes)//If the exit is reached, then the value is assigned to the way array showing the route, and that is why the specific route is saved from the last/{way[wayn][0]=i;
 Way[wayn++][1]=j;
   Return
  } waycopy (Oldmap,map); if (oldmap[i+1][j]==0&&!yes)/* To determine whether the bottom can go, if the logo yes is already 1 do not have to look down the * * Findway (OLDMAP,I+1,J);
 if (yes) {way[wayn][0]=i;
 Way[wayn++][1]=j;
   Return
  } waycopy (Oldmap,map);
   if (oldmap[i][j+1]==0&&!yes)/* Judge whether the right can go/{findway (oldmap,i,j+1);
 if (yes) {way[wayn][0]=i;
 Way[wayn++][1]=j;
   Return
  } waycopy (Oldmap,map);
   if (oldmap[i-1][j]==0&&!yes)/* Judge whether the above can walk * * {findway (OLDMAP,I-1,J);
 if (yes) {way[wayn][0]=i;
 Way[wayn++][1]=j;
   Return
  } waycopy (Oldmap,map);
   if (oldmap[i-1][j+1]==0&&!yes)/* Judge whether the upper right can go * * {findway (oldmap,i-1,j+1);
 if (yes) {way[wayn][0]=i;
 Way[wayn++][1]=j;
   Return
  } waycopy (Oldmap,map);
   if (oldmap[i+1][j-1]==0&&!yes)/* Judge whether the lower left can go * * {findway (oldmap,i+1,j-1);
 if (yes) {way[wayn][0]=i;
 Way[wayn++][1]=j;
   Return
  } waycopy (Oldmap,map);
   if (oldmap[i][j-1]==0&&!yes)/* Judge whether the left can go * * {findway (oldmap,i,j-1);
 if (yes) {way[wayn][0]=i;
 Way[wayn++][1]=j;
   Return
  } waycopy (Oldmap,map); if (oldmap[I-1][j-1]==0&&!yes)/* Judge whether the upper left can go * * {findway (oldmap,i-1,j-1);
 if (yes) {way[wayn][0]=i;
 Way[wayn++][1]=j;
   Return
} return;
  Maprand (int (*MAP) [N])///start random Maze Diagram */{int i,j; Cleardevice ()/* Clear Screen * * Randomize (); /* Random Number Generator * * (i=0;i<n;i++) {for (j=0;j<n;j++) {if i==0| | i==n-1| | j==0| |
 J==N-1)/* The outermost ring for the wall * * * map[i][j]=1; else if (i==1&&j==1| |
   I==N-2&AMP;&AMP;J==N-2)/* The starting point and end point as removable * * map[i][j]=0;
  else Map[i][j]=random (2);//* Other randomly generated 0 or 1*/} void Prmap (int (*MAP) [N])/* Output maze map/{int i,j; for (i=0;i<n;i++) for (j=0;j<n;j++) if (map[i][j]==0) {Setfillstyle (solid_fill,white);/* White is the accessible road/bar (100+j
 *15-6,50+I*15-6,100+J*15+6,50+I*15+6);
 else {setfillstyle (solid_fill,blue);/* Blue is wall */bar (100+J*15-6,50+I*15-6,100+J*15+6,50+I*15+6);
  } void Find (void)/* finds the path/{int i;
  Setfillstyle (solid_fill,red)/* Red output go to the specific route * * * wayn--; for (i=wayn;i>=0;i--) {bar (100+way[i][1]*15-6,50+way[i][0]*15-6,100+ way[i][1]*15+6,50+way[i][0]*15+6); Sleep (1);/* Control display Time/} bar (100+ (N-2) *15-6,50+ (N-2) *15-6,100+ N-2 (*15+6,50+) N-2);
  * * At the target point marked red/SetColor (GREEN);
Settextstyle (0,0,2);/* Set Font Size * * OUTTEXTXY (130,400, "Find a way!");}
  void Notfind (void)/* Failed to find a path/{setcolor (GREEN);
Settextstyle (0,0,2);/* Set Font Size * * OUTTEXTXY (130,400, "not find a way!");}
  void result (void)/* Results processing */{if (yes)/* If found/* find ();
  Else/* did not find the road * * Notfind ();
Getch (); } void Close (void)/* graphics off/{closegraph ();}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.