C language (simple game)-out of the maze

Source: Internet
Author: User

1#include <stdio.h>2 //macro definition Maze[rows][cols]; rows and columns;3 #defineROWS 74 #defineCOLS 65 //Draw a maze (global variable)6 Charmaze[rows][cols]= {7{'#','#','#','#','#','#'},8{'#','0','#',' ',' ',' '},9{'#',' ','#',' ','#','#'},Ten{'#',' ','#',' ',' ','#'}, One{'#',' ',' ','#',' ','#'}, A{'#','#',' ',' ',' ','#'}, -{'#','#','#','#','#','#'} - }; the //set X, y coordinates (global variables); - intcurrentx=1, currenty=1; - //the XY coordinate after moving (global variable); - intnextx,nexty; + //See if the next step can go int[x][y]== '; - CharStreet =' '; +  A //initialization function at voidPrintmaze (); - voidmovetonextposition (); - voidCalculatenextposition (Chardirection); -  -  -  in intMainintargcConst Char*argv[]) { -NEXTX =CurrentX; toNexty =CurrentY; +     //the screen prints out the maze; - Printmaze (); the     Chardirection; *      while(1) { $printf"please move the character, use the keyboard w/s/a/d (up and down) operation \ n");Panax Notoginsengscanf"%c",&direction); - calculatenextposition (direction); the movetonextposition (); + Printmaze (); A         if(currentx==rows-1|| currenty==cols-1){ theprintf"customs clearance, hehe!"); +              Break; -         } $     } $     return 0; - } -  the  - //Print MapWuyi voidPrintmaze () { the      for(inti =0; i<rows;i++){ -          for(intj =0; j<cols;j++) { Wuprintf"%c", Maze[i][j]); -         } Aboutprintf"\ n"); $     } - } - //Moving People - voidmovetonextposition () { A     if(maze[nextx][nexty]==Street) { +         Chartemp =Maze[currentx][currenty]; theMaze[currentx][currenty] =Maze[nextx][nexty]; -Maze[nextx][nexty] =temp; $CurrentX =nextx; theCurrentY =nexty; the          the}Else{ theNEXTX =CurrentX; -Nexty =CurrentY; in     } the } the //Calculate Next Position About voidCalculatenextposition (Chardirection) { the     Switch(direction) { the          Case 'W':{ theNEXTX = CurrentX-1; +              Break; -         } the          Case 's':{BayiNEXTX = CurrentX +1; the              Break; the         } -          Case 'a':{ -Nexty = CurrentY-1; the              Break; the         } the          Case 'D':{ theNexty = CurrentY +1; -              Break; the         } the         default: the              Break;94     } the}

C language (simple game)-out of the maze

Related Article

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.