C # Small program Flying Chess, Program effect chart
1, design analysis
This program interface is roughly divided into four parts:
① Top Game Name interface
② Information Tip Area
③ Game Interface Area
④ Game Action Tip Area
2, the partition design realizes
One, the game interface display area, because only needs to display the graph, therefore directly with Console.WriteLine () output can.
Second, the information tip area, which is used to show the game against both sides of the name and other information, at the beginning of the game needs to be entered by the user to fight both the name information, so you can use Console.ReadLine () to read the value of the user type,< Note: the need to verify the name of the >
Third, the game interface area, which is used to show each step of the game interface, you can see that the interface has ordinary squares, level blocks and games against both sides. For this interface you need to draw more than once, so you can write a separate method to achieve, the game interface of the most common box, so you can initialize the map to the array and initialize it into a normal box, where you can use a plastic array to store the coordinates of the properties, (for example: 0 for the normal box, 1 indicates a lucky turntable, 2 represents a mine, and a 3 indicates a pause, 4 means the space-time tunnel, A, B, respectively, to play the user in the map when mapping according to different figures to draw the corresponding pattern, in the user to play a game redraw map only according to the corresponding coordinates of the value of the map can be drawn, the screen can be divided into 5 parts To draw, as shown in the following illustration:
Draw the first and fifth parts, you can use the general Console.Write () for output, while the second part of the need to draw out the preceding space, the third part because it is backward rendering, so in the drawing can be used to draw from the large coordinates to the small coordinates of the decrease.
Four, the game Operation Hint area, here is used to prompt the user to operate the game, only can output the statement.
The hardest part of this flying chess game is to map the game and the level of the operation.
Draw a map:
1, initialize the map, in the drawing can first initialize the map, with an array to store the location of the checkpoint, and then use the loop to the map where the level of the points to give the value of the level.
The key code is as follows
///<summary>///initialization game map///</summary> static void Initialmap () {
for (int i=0;i<map.length;i++) {Map[i] = 0; //For storage checkpoint location int[] Luckyturn = {6, 23, 40, 55, 69, 83,98};//lucky turntable 1 int[] landmine = {5, 13, 17, 33, 3 8, 50, 64, 80, 94};//Mines 2 int[] Pause = {9, 27, 60, 93};//paused 3 int[] Timetunnel = {20, 25, 45, 63, 72, 88,
90};//Space-Time tunnel 4 for (int i=0;i<luckyturn.length;i++) {int pos = luckyturn[i];
Map[pos] = 1;
for (int i=0;i<landmine.length;i++) {Map[landmine[i]] = 2; for (int i=0;i<pause.
length;i++) {int pos = pause[i];
Map[pos] = 3;
for (int i=0;i<timetunnel.length;i++) {int pos = timetunnel[i];
Map[pos] = 4; }
}
2, check the coordinates of the value, after the map initialization, you can begin to map the operation of the map can be used in the design of the distribution of the layout, the map should be tested when the value of the sitting punctuation, in accordance with the value of the point to draw the corresponding pattern, in the check according to the value of the corresponding pattern returned, When you draw out the loop, the value code for the check coordinates is as follows:
<summary>///get the coordinates to draw///</summary>///<param name= "I" > coordinates to be drawn </param>///
<returns></returns> static string getmapstring (int i) {string result= "";//used to return the pattern corresponding to a coordinate if (playerpos[0] = = i && playerpos[1] = = i)//judge whether it is against both sides in this place {Console.foregroundcolor = Consolecolo r.yellow;//sets the foreground color of the pattern to yellow result = "<>"//Get both in pattern} else if (playerpos[0] = i) {Co Nsole.
Foregroundcolor = Consolecolor.yellow; result = "a";//get a both in pattern} else if (playerpos[1] = i) {Console.foregroundcolor = Consolecolor.y
Ellow; result = "B";//get B all in pattern} else {switch (Map[i]) {case 0:console .
Foregroundcolor = Consolecolor.white;
result = "-";//get common all in pattern break;
Case 1:console.foregroundcolor = consolecolor.red; result = "0";//Have a turntable pattern break;
Case 2:console.foregroundcolor = Consolecolor.blue;
result = "☆";
Break
Case 3:console.foregroundcolor = Consolecolor.green;
result = "▲";
Break
Case 4:console.foregroundcolor = Consolecolor.darkblue;
result = "Swastika";
Break } return result;
Return pattern}
3, draw the map, after the return of the pattern, you can map the drawing, here is to draw the first line of code
<summary>
///Draw game map
///</summary>
static void Drownmap ()
{
Console.WriteLine ( "Legend: Lucky turntable 0 Mine ☆ suspend time and Space tunnel 卍");
Draw the first line subscript 0-29 map for
(int i=0;i<30;i++)/loop coordinates get the pattern of each point in the first row
{
Console.Write (getmapstring (i)); Call function to get the pattern of each coordinate
console.write ("\ n");
Console.resetcolor ()//reset foreground color
}
Checkpoint Operation:
1, analysis and design
The basic rule for flying chess is to roll the dice for two people, this is the circular structure, and then according to the number of points to be thrown forward < note: After each step forward should be judged whether to go through the game, in the encounter when the corresponding operation, checkpoints have four, each corresponding operation is different, So here you can use the Switch-case cycle structure in each step after the operation are judged whether to step on the level, in accordance with the results of the next step, each operation completed, redraw the latest map.
2, Code implementation
one player throws the dice, the two take turns throwing, and when one wins, the game ends. Throw dice the available function random () produces a random number implementation, each step needs to check whether the game wins, the following methods:
<summary>
///Check that the coordinates are out of bounds
///</summary>
static void Checkpos ()
{for
(int i = 0; i < = 1; i++)
{
if (Playerpos[i] >)
{
playerpos[i] =
}
if (Playerpos[i] < 0)
{
playerpos[i] = 0;
}
}
}
When encountering the checkpoint, according to the nature of the level set up to operate, in the face of the lucky turntable for the user to choose 1: Exchange position with the other side, 2: bombing each other, let the other side back 6, you need to let the user enter 1 or 2, the user needs to type the value of the test, the following methods can be implemented:
static int ReadInt (int min,int max)
{while
(true)
{
try
{
int number = Convert.ToInt32 ( Console.ReadLine ());
if (number<min| | Number>max)
{
Console.WriteLine ("Enter only the number between {0}-{1}", please re-enter!) ", Min, max);
Continue;
}
return number;
}
Catch
{
Console.WriteLine ("Enter only numbers, please re-enter!") ");
}
}
}
When a checkpoint 3 timeout is encountered, the user is required to pause the next time the action is made, where a bool-type array is defined to indicate whether the user should suspend the operation in this step, and the code to throw user A is given below:
In this cycle the player turns the dice, and when any one of the coordinates >=99, the game ends while (playerpos[0]<99&&playerpos[1]<99) {Random R
= new Random ()///Generate random number of int step;//random numbers generated; if (isstop[0] = false) {#region//Player A dice
Console.WriteLine ("{0} Press any key to dice ...", names[0]);
Consolekeyinfo rec = Console.readkey (true); if (rec.
Key = = consolekey.tab) {step = 6;
else {step = R.next (1, 7);
} Console.WriteLine ("{0}" throws {1} ", Names[0], step);
Console.WriteLine ("{0} Press any key action ...", Names[0]);
Console.readkey (TRUE);/does not display the value of the pressed key playerpos[0] = Playerpos[0] + step;//change coordinates < Once the coordinates have changed, judge whether it is greater than 99 or less than 0>
Checkpos ();
if (playerpos[0] = = Playerpos[1])/Player A steps to Player B {playerpos[1] = 0; msg = string.
Format ("{0} stepped on {1}, return origin", Names[0], names[1]);//format function for stitching character segments} #region else//No step, to determine if there are other checkpoints in this location {switch (Map[playerpos[0]]) {case 0:
Normal position, no effect msg = "";
Break
Case 1://Lucky turntable console.clear ();
Drownmap (); Console.WriteLine ("{0}" go to the lucky turntable, please choose luck?)
", Names[0]);
Console.WriteLine ("1: Exchange Position 2: bombing");
int userselect = READINT (1, 2);
if (Userselect = = 1) {int temp;
temp = playerpos[0];
Playerpos[0] = playerpos[1];
PLAYERPOS[1] = temp; msg = string.
Format ("{0}" is selected to swap location with {1}, Haha, back to the liberation of the night, "names[0], names[1]);
else {playerpos[1] = playerpos[1]-6;
Checkpos (); msg = string.
Format ("{0}" has been selected with let {1} back 6 steps, since the Names[0), names[1]); } break;
Case 2://mine playerpos[0] = playerpos[0]-6;
Checkpos (); msg = string.
Format ("{0} stepped on the mine, back 6 steps, Amitabha", names[0]);
Break
Case 3://pause isstop[0] = true; msg = string.
Format ("{0} Paused once!", Names[0]);
Break
Case 4://Space-Time tunnel playerpos[0] = playerpos[0] + 6;
Checkpos (); msg = string. Format ("{0}" enters the space-time tunnel, go ahead!)
", Names[0]);
Break
}} #endregion Console.WriteLine ("Press any key to start action ...");
Console.readkey (TRUE);
Console.clear ();
Drownmap ();
if (msg!= "") {Console.WriteLine (msg); {1} Console.WriteLine ("{0}"), Action completed!
", Names[0], step);
Console.WriteLine ("*******w Player A and Player B's position as follows ***********"); Console.WriteLine ("Player {0} 's position is {1}", Names[0], playerpos[0] + 1);
Console.WriteLine ("Player {0} 's position is {1}", Names[1], playerpos[1] + 1);
#endregion} else {//Note a suspend isstop[0] = false;
} if (playerpos[0]>=99) {break;
#region Player B roll the Dice #endregion}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.