15-01-07 C # oriented procedure 08

Source: Internet
Author: User
Tags clear screen

Double avg = 9.99999999

string s = Avg. ToString ("0.00") to convert the double type to 9.99

Avg = convert.todouble (s);

Converts a double to a string of 2 decimal places, and then converts a string to a double;

Console.WriteLine ("{0:0.00}"); and ToString ("0.00"); will keep rounding;

To implement a function it is best to write it as a method, so that every time you use this function, you can call this method.

You want to change an array, whether it is changing the position of the element or changing the size of the element, or a method to implement it, this method does not need to have any return value, the parameters of the method does not need ref; The array is an exception;

Flying chess: The screen is constantly flashing, in fact, is the process of re-painting, because to keep the painting, so it is encapsulated into a method, re-painting as long as the method can be called;

Console.foreground = Consolecolor.yellow;

Console.WriteLine ("*******************"); This line of output is yellow *

Console.foreground = consolecolor.red;

Console.WriteLine ("*******************"); This line of output is red *

In the game, a progress bar is read before entering a scene, which is the resource needed to load the map;

The process of displaying a special string in the digital programming console of an integer array when the console draws a map of the flying Chess is to initialize the map;

When a lot of methods need to use a variable, it can be declared as a static field to simulate the global variables; Static int[] maps = new INT[100];

When writing flight chess, because the special character map is stored by an int array, but there are many different special characters, squares, lucky plates, pentagram, etc., we put the lucky plate in the word subscript character to 1, the string default to 0 is set as a box, this time, we can declare a few arrays
Int[] Luckyturn = {6,23,40,55,69,83}; The pictures indexed in maps are the lucky disks;
Int[] landmine = {2,34,56,77}; Maps indexed these paintings as mines;
Int[] Pause = {23,45,76}; Maps that are indexed for these are paused;
Int[] Timetunnel = {20,56,64}; Maps indexed these paintings as space-time tunnels;

public static void Biaoji (int[] nums, int flag)
{
for (int i = 0; i<nums. length;i++)
{
Maps[nums[i]] = flag;
}
}

The method of his own writing is unknown to the wrong, and maps are assigned;

Initializing an array it's just that the map is assigned a value;

As the figure is a vertical, so the drawing can only be a horizontal, a vertical lines such painting;

Because there are two players, declare a static array to store the coordinates of player A and player B;

Static int[] Playerpos = new int[2];

public static void Drawmap ()
{
for (int i = 0;i <; i++)
{
Console.Write (Drawstringmap (i));
}
}//First Rampage

Why the level is not randomly generated, but is used to kill the array, may produce 6 consecutive mines cause the game can not play;

To make the code look clear, use #region #endregion fold the code;

When painting vertical lines, double nesting with for loop;

After painting the first row, you should change the line first;

Console.WriteLine ();

for (int i = +; i <; i++)

{
for (int j = 0;j <; j + +)
{
Console.Write ("");
}
Every line of the painting will be changed;
Console.Write (Drawstringmap (i));
Console.WriteLine ();
}//First vertical lines

for (int i = 64;i>=35;i--)
{
Console.Write (Drawstringmap (i));
}//Second rampage

Console.WriteLine ();

for (int i = 65;i<=69;i++)
{
Console.WriteLine (Drawstringmap (i));
}//Second vertical lines

for (int i = 70;i<=99;i++)
{
Console.Write (Drawstringmap (i));
}//Third rampage;

A method of drawing a map from a method, as it is often called, and the encapsulation of the implementation method;

If Player A and player B have the same coordinates and are on this map, then draw an angle bracket, first draw AB, because AB is always changing the other, and then draw the level;
public static string Drawstringmap (int i)
{
String str = "";
if (playerpos[0] = = playerpos[1]&&playerpos[1]==i)
{
str = "<>"; <> is half-width, a, B is full-width; the two half-width of the console equals a full-width
}
else if (playerpos[0] = i)
{
str = "a";
}
else if (playerpos[1] = i)
{
str = "B";
}
Else
{
Switch (Maps[i])
{
Case 0;
Console.foreground = Consolecolor.yellow;
str = "";
Break
Case 1;
Console.foreground = consolecolor.red;
str = "0";
Break
Case 2;
Console.foreground = Consolecolor.green;
str = "★";
Break
Case 3;
Console.foreground = Consolecolor.blue;
str = "";
Break
Case 4;
Console.foreground = Consolecolor.purple;
str = "卐";
Break
}
}
return str;
}

The above is three steps
1. Draw the game head

2. Initialize maps to assign values to maps,

3. Draw a map of the game; Write a unified method of printing characters, print a map;

4. Enter your name;

Store two players ' names

Static string[] PlayerName = new string[2];

Console.clear (); Clear screen;

How to copy text in the console, right-click on the top corner of the console icon, right-click, edit, Mark; Select the text you want to copy, press ENTER to copy;

5. Formally began to write the essence of the game;

A play once B play once, if the game is written as a method is called a call once, B call once, the game has a player to the end of the play, they have to play the conditions of the game is no one to the end;

Console.readkey (true); The user pressed key is not displayed in the console;

while (playerpos[0]<99 && playerpos[1]<99)
{

}

Playerpos[0] + = 4; Let the player move forward quadrant motif;

by maps[playpos[0]] = = 0,1,2,3,4 To determine what the player stepped on;

Switch can be used without default;

When a piece of code is written as a method, copy the past, see which variables have a red line below it, and send it to the reference;

Static bool[] flags = new BOOL[2]; The player's two marks, which are related to a pause;

if (playerpos[playernum] = = playerpos[1-playernum])
{
playerpos[Playernum]-= 6;

}
while (true)
{
if (input = = "1")
{
int temp = Playpos[playnumber];
Playpos[playnumber] = Playpos[1-playnumber];
Playpos[1-playnumber] = temp;

}
.........
}

Pause the words with these code to achieve;

if (flags[0] = = False)

{

Playgames[0];

}

Else

{

Flags[0] = = true;
}

if (flags[1] = = False)
{
PLAYGAMES[1];
}
Else
{
Flags[1] = = true;
}

15-01-07 C # oriented procedure 08

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.