C language to achieve the maze game

Source: Internet
Author: User
Tags exit printf

Dark clouds Teacher's words: Learning to detect keyboard information, Yu Wenbiao classmate Classroom made a keyboard in the screen up and down to move a star program.

The teacher gave a few students the idea of the Maze game, Peng Bo students next class will do a good maze game out, wonderful!

The idea is clear, the code is concise, the annotation is clear, only everybody learns.

At the same time he also implemented a simple push box game, is a quick program.

#include "stdio.h"
#include "Bios.h"
#define LEFT 75
#define RIGHT 77
#define UPPER 72
#define DOWN 80
#define ESC 27
Union keyboard/* Defines the common body type to read the keyboard code * *
{
unsigned int ikeyinfo;
Char chkeybit[2];
};
int Fngetkey (void)/* Defines a function to read the keyboard code * *
{
Union keyboard UniKey1; /* Defines a common body variable to read the keyboard code * *
while (Bioskey (1) ==0); /* Check whether the user is pressing the button *
Unikey1.ikeyinfo=bioskey (0); /* Read key information * *
Return (unikey1.chkeybit[0]==0?unikey1.chkeybit[1]:unikey1.chkeybit[0]); /* Return ASCII code or extension code * *
}
void Main ()
{
int ikey,x=11,y=6; /*x,y for the character after moving the coordinates * *
int chmap[10][10]={/* Define map with two-dimensional array/*
{0,0,0,0,0,0,0,0,0,0},/*0 said the wall 1 means road 2 means export * *
{0,1,0,0,1,1,1,1,1,0},
{0,1,1,0,1,0,1,0,1,0},
{0,0,1,0,1,0,1,0,1,0},
{0,0,1,1,1,0,0,0,1,0},
{0,0,0,0,0,0,1,1,1,0},
{0,1,0,0,1,1,1,0,0,0},
{0,1,1,1,1,0,0,0,2,0},
{0,1,0,0,1,1,1,1,1,0},
{0,0,0,0,0,0,0,0,0,0},
};
int i,j; * * Repeatedly to move the coordinate operation
while (1)
{
for (i=0;i<10;i++)/* Output new Map (Refresh map) * *
{
Gotoxy (10,5+i);
for (j=0;j<10;j++)
{
if (chmap[i][j]==0)
printf ("#");
if (chmap[i][j]==1)
printf ("");
if (chmap[i][j]==2)
printf ("E");
}

}
if (chmap[y-5][x-10]==2)/* If the coordinates of the person and the target coordinate the output "you win!" Exit * *
{
CLRSCR ();
printf ("You win!");
Break
}/* The coordinate output of the starting (or moving) person */
Gotoxy (X,y);
printf ("*\b");
Ikey=fngetkey (); /* Press the Read key information to change the coordinates if the change of the coordinates and wall (0) coincidence will not change * *
if (ikey==left&&chmap[y-5][x-1-10]!=0)
x--;
if (ikey==right&&chmap[y-5][x+1-10]!=0)
x + +;
if (ikey==upper&&chmap[y-1-5][x-10]!=0)
y--;
if (ikey==down&&chmap[y+1-5][x-10]!=0)
y++;
if (IKEY==ESC)/* Enter ESC to exit and output "you LOST" * *
{
CLRSCR ();
printf ("You Lost");
Break
}
CLRSCR ();
}
Getch ();
}

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.