C language implementation of the push box game

Source: Internet
Author: User
Tags exit printf

* * This is Peng Bo classmate's push box game, we try to play, who has better idea? */

#include "stdio.h"
#include "Bios.h"
#define LEFT 75
#define RIGHT 77
#define UPPER 72
#define DOWN 80
#define ESC 27
struct BOXSS/* Defines the box structure, which contains the coordinate properties * *
{
int x,y;
};
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,tx=11,ty=6; /*x,y for the characters to move after the coordinates, Tx,ty for the characters before moving coordinates * *
struct BOXSS box[4]; /* Define the number of boxes * *
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 the target * *
{0,1,0,0,0,0,1,1,1,0},
{0,1,0,2,0,0,1,0,1,0},
{0,1,0,1,0,0,1,0,1,0},
{0,1,1,1,0,0,1,0,1,0},
{0,1,0,0,0,0,1,0,1,0},
{0,1,1,1,1,1,1,0,1,0},
{0,1,0,1,0,0,0,0,2,0},
{0,2,0,1,1,1,1,2,0,0},
{0,0,0,0,0,0,0,0,0,0},
};
int i,j;
box[0].x=13; /* Define the coordinate properties of the box * *
box[1].x=11;
box[2].x=14;
box[3].x=18;
box[0].y=8;
box[1].y=7;
box[2].y=13;
box[3].y=7;
while (1)//* Repeated movement of the coordinate operation * *
{
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 ("X");
}
}
j=0; /* To determine if all the boxes are in the target coordinates * *
for (i=0;i<4;i++)
if (chmap[box[i].y-5][box[i].x-10]==2)
j + +;
if (j==4)/* If all the boxes are in position to output "you win!" Exit * *
{
CLRSCR ();
printf ("You win!");
Break
}
for (i=0;i<4;i++)/* At the beginning (or after moving) the coordinate output box * *
{
Gotoxy (BOX[I].X,BOX[I].Y);
printf ("0");
}
Gotoxy (X,y); /* At the beginning (or after the movement) of the coordinates of the output person * *
printf ("*\b");
Tx=x; /* Record the coordinates before the move
Ty=y;
Ikey=fngetkey ();
if (ikey==left&&chmap[y-5][x-1-10]!=0)/* Press the Read key information to change the coordinates if the changed coordinates and the wall (0) coincide without change * *
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++; /* Enter ESC to exit and output "you LOST" * *
if (IKEY==ESC)
{
CLRSCR ();
printf ("You Lost");
Break
}
for (i=0;i<4;i++)/* If the moving person's coordinates coincide with the box coordinates, change the box coordinates forward one.
if (box[i].x==x&&box[i].y==y)
{
box[i].x+= (X-TX);
box[i].y+= (Y-ty);
if (chmap[box[i].y-5][box[i].x-10]==0)/* If the moved box coordinates will appear on the wall, so that the box coordinates and human coordinates are returned before moving the value * *
{
box[i].x-= (X-TX);
box[i].y-= (Y-ty);
X=TX;
Y=ty;
}
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.