WriteLine
How do I use Write Line
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: PingCloudRegions.cs
Copy
21 public override bool StartPing(string ip)
22 {
23 base.Init();
24
25 try
26 {
27 sock.ReceiveTimeout = 5000;
28 sock.Connect(ip, 5055);
29
30 PingBytes[PingBytes.Length - 1] = PingId;
31 sock.Send(PingBytes);
32 PingBytes[PingBytes.Length - 1] = (byte)(PingId - 1);
33 }
34 catch (Exception e)
35 {
36 sock = null;
37 Console.WriteLine(e);
38 }
39
40 return false;
41 }
File name: ChatClient.cs
Copy
556 void IPhotonPeerListener.DebugReturn(DebugLevel level, string message)
557 {
558#if UNITY_EDITOR || UNITY_STANDALONE
559 if (level == DebugLevel.ERROR)
560 {
561 UnityEngine.Debug.LogError(message);
562 }
563 else if (level == DebugLevel.WARNING)
564 {
565 UnityEngine.Debug.LogWarning(message);
566 }
567 else
568 {
569 UnityEngine.Debug.Log(message);
570 }
571#else
572 Debug.WriteLine(message);
573#endif
574 }
File name: ChatClient.cs
Copy
854 private bool AuthenticateOnFrontEnd()
855 {
856 if (CustomAuthenticationValues != null)
857 {
858 var d = new Dictionary
859 return this.chatPeer.OpCustom((byte)ChatOperationCode.Authenticate, d, true);
860 }
861 else
862 {
863 Debug.WriteLine("Can't authenticate on front end server. CustomAuthValues is null");
864 }
865 return false;
866 }
File name: ChatClient.cs
Copy
868 private void LogWarning(string message)
869 {
870#if UNITY
871 UnityEngine.Debug.LogWarning(message);
872#else
873 Debug.WriteLine(message, "Warning");
874#endif
875 }
File name: ChatClient.cs
Copy
877 private void Log(string message)
878 {
879#if UNITY
880 UnityEngine.Debug.Log(message);
881#else
882 Debug.WriteLine(message);
883#endif
884 }
File name: Program.cs
Copy
135 private static void MoveBall()
136 {
137 if (ballPositionY == 0)
138 {
139 ballDirectionUp = false;
140 }
141 if (ballPositionY == Console.WindowHeight - 1)
142 {
143 ballDirectionUp = true;
144 }
145 if (ballPositionX == Console.WindowWidth - 1)
146 {
147 SetBallAtTheMiddleOfTheGameField();
148 ballDirectionRight = false;
149 ballDirectionUp = true;
150 firstPlayerResult++;
151 Console.SetCursorPosition(Console.WindowWidth / 2, Console.WindowHeight / 2);
152 Console.WriteLine("First player wins!");
153 Console.ReadKey();
154 }
155 if (ballPositionX == 0)
156 {
157 SetBallAtTheMiddleOfTheGameField();
158 ballDirectionRight = true;
159 ballDirectionUp = true;
160 secondPlayerResult++;
161 Console.SetCursorPosition(Console.WindowWidth / 2, Console.WindowHeight / 2);
162 Console.WriteLine("Second player wins!");
163 Console.ReadKey();
164 }
165
166 if (ballPositionX < 3)
167 {
168 if (ballPositionY >= firstPlayerPosition
169 && ballPositionY < firstPlayerPosition + firstPlayerPadSize)
170 {
171 ballDirectionRight = true;
172 }
173 }
174
175 if (ballPositionX >= Console.WindowWidth - 3 - 1)
176 {
177 if (ballPositionY >= secondPlayerPosition
178 && ballPositionY < secondPlayerPosition + secondPlayerPadSize)
179 {
180 ballDirectionRight = false;
181 }
182 }
183
184 if (ballDirectionUp)
185 {
186 ballPositionY--;
187 }
188 else
189 {
190 ballPositionY++;
191 }
192
193
194 if (ballDirectionRight)
195 {
196 ballPositionX++;
197 }
198 else
199 {
200 ballPositionX--;
201 }
202 }
File name: Program.cs
Copy
23 static void Main(string[] args)
24 {
25 byte right = 0;
26 byte left = 1;
27 byte down = 2;
28 byte up = 3;
29 int lastFoodTime = 0;
30 int foodDissapearTime = 8000;
31 int negativePoints = 0;
32
33 Position[] directions = new Position[]
34 {
35 new Position(0, 1), // right
36 new Position(0, -1), // left
37 new Position(1, 0), // down
38 new Position(-1, 0), // up
39 };
40 double sleepTime = 100;
41 int direction = right;
42 Random randomNumbersGenerator = new Random();
43 Console.BufferHeight = Console.WindowHeight;
44 lastFoodTime = Environment.TickCount;
45
46 List
47 {
48 new Position(12, 12),
49 new Position(14, 20),
50 new Position(7, 7),
51 new Position(19, 19),
52 new Position(6, 9),
53 };
54 foreach (Position obstacle in obstacles)
55 {
56 Console.ForegroundColor = ConsoleColor.Cyan;
57 Console.SetCursorPosition(obstacle.col, obstacle.row);
58 Console.Write("=");
59 }
60
61 Queue
62 for (int i = 0; i <= 5; i++)
63 {
64 snakeElements.Enqueue(new Position(0, i));
65 }
66
67 Position food;
68 do
69 {
70 food = new Position(randomNumbersGenerator.Next(0, Console.WindowHeight),
71 randomNumbersGenerator.Next(0, Console.WindowWidth));
72 }
73 while (snakeElements.Contains(food) || obstacles.Contains(food));
74 Console.SetCursorPosition(food.col, food.row);
75 Console.ForegroundColor = ConsoleColor.Yellow;
76 Console.Write("@");
77
78 foreach (Position position in snakeElements)
79 {
80 Console.SetCursorPosition(position.col, position.row);
81 Console.ForegroundColor = ConsoleColor.DarkGray;
82 Console.Write("*");
83 }
84
85 while (true)
86 {
87 negativePoints++;
88
89 if (Console.KeyAvailable)
90 {
91 ConsoleKeyInfo userInput = Console.ReadKey();
92 if (userInput.Key == ConsoleKey.LeftArrow)
93 {
94 if (direction != right) direction = left;
95 }
96 if (userInput.Key == ConsoleKey.RightArrow)
97 {
98 if (direction != left) direction = right;
99 }
100 if (userInput.Key == ConsoleKey.UpArrow)
101 {
102 if (direction != down) direction = up;
103 }
104 if (userInput.Key == ConsoleKey.DownArrow)
105 {
106 if (direction != up) direction = down;
107 }
108 }
109
110 Position snakeHead = snakeElements.Last();
111 Position nextDirection = directions[direction];
112
113 Position snakeNewHead = new Position(snakeHead.row + nextDirection.row,
114 snakeHead.col + nextDirection.col);
115
116 if (snakeNewHead.col < 0) snakeNewHead.col = Console.WindowWidth - 1;
117 if (snakeNewHead.row < 0) snakeNewHead.row = Console.WindowHeight - 1;
118 if (snakeNewHead.row >= Console.WindowHeight) snakeNewHead.row = 0;
119 if (snakeNewHead.col >= Console.WindowWidth) snakeNewHead.col = 0;
120
121 if (snakeElements.Contains(snakeNewHead) || obstacles.Contains(snakeNewHead))
122 {
123 Console.SetCursorPosition(0, 0);
124 Console.ForegroundColor = ConsoleColor.Red;
125 Console.WriteLine("Game over!");
126 int userPoints = (snakeElements.Count - 6) * 100 - negativePoints;
127 //if (userPoints < 0) userPoints = 0;
128 userPoints = Math.Max(userPoints, 0);
129 Console.WriteLine("Your points are: {0}", userPoints);
130 return;
131 }
132
133 Console.SetCursorPosition(snakeHead.col, snakeHead.row);
134 Console.ForegroundColor = ConsoleColor.DarkGray;
135 Console.Write("*");
136
137 snakeElements.Enqueue(snakeNewHead);
138 Console.SetCursorPosition(snakeNewHead.col, snakeNewHead.row);
139 Console.ForegroundColor = ConsoleColor.Gray;
140 if (direction == right) Console.Write(">");
141 if (direction == left) Console.Write("<");
142 if (direction == up) Console.Write("^");
143 if (direction == down) Console.Write("v");
144
145
146 if (snakeNewHead.col == food.col && snakeNewHead.row == food.row)
147 {
148 // feeding the snake
149 do
150 {
151 food = new Position(randomNumbersGenerator.Next(0, Console.WindowHeight),
152 randomNumbersGenerator.Next(0, Console.WindowWidth));
153 }
154 while (snakeElements.Contains(food) || obstacles.Contains(food));
155 lastFoodTime = Environment.TickCount;
156 Console.SetCursorPosition(food.col, food.row);
157 Console.ForegroundColor = ConsoleColor.Yellow;
158 Console.Write("@");
159 sleepTime--;
160
161 Position obstacle = new Position();
162 do
163 {
164 obstacle = new Position(randomNumbersGenerator.Next(0, Console.WindowHeight),
165 randomNumbersGenerator.Next(0, Console.WindowWidth));
166 }
167 while (snakeElements.Contains(obstacle) ||
168 obstacles.Contains(obstacle) ||
169 (food.row != obstacle.row && food.col != obstacle.row));
170 obstacles.Add(obstacle);
171 Console.SetCursorPosition(obstacle.col, obstacle.row);
172 Console.ForegroundColor = ConsoleColor.Cyan;
173 Console.Write("=");
174 }
175 else
176 {
177 // moving...
178 Position last = snakeElements.Dequeue();
179 Console.SetCursorPosition(last.col, last.row);
180 Console.Write(" ");
181 }
182
183 if (Environment.TickCount - lastFoodTime >= foodDissapearTime)
184 {
185 negativePoints = negativePoints + 50;
186 Console.SetCursorPosition(food.col, food.row);
187 Console.Write(" ");
188 do
189 {
190 food = new Position(randomNumbersGenerator.Next(0, Console.WindowHeight),
191 randomNumbersGenerator.Next(0, Console.WindowWidth));
192 }
193 while (snakeElements.Contains(food) || obstacles.Contains(food));
194 lastFoodTime = Environment.TickCount;
195 }
196
197 Console.SetCursorPosition(food.col, food.row);
198 Console.ForegroundColor = ConsoleColor.Yellow;
199 Console.Write("@");
200
201 sleepTime -= 0.01;
202
203 Thread.Sleep((int)sleepTime);
204 }
205 }
File name: Program.cs
Copy
32 static void Main(string[] args)
33 {
34 SetGameField();
35 StartupScreen();
36
37 isUsed = new bool[Console.WindowWidth, Console.WindowHeight];
38
39
40 while (true)
41 {
42 if (Console.KeyAvailable)
43 {
44 ConsoleKeyInfo key = Console.ReadKey(true);
45 ChangePlayerDirection(key);
46 }
47
48
49 MovePlayers();
50
51
52 bool firstPlayerLoses = DoesPlayerLose(firstPlayerRow, firstPlayerColumn);
53 bool secondPlayerLoses = DoesPlayerLose(secondPlayerRow, secondPlayerColumn);
54
55
56 if (firstPlayerLoses && secondPlayerLoses)
57 {
58 firstPlayerScore++;
59 secondPlayerScore++;
60 Console.WriteLine();
61 Console.WriteLine("Game over");
62 Console.WriteLine("Draw game!!!");
63 Console.WriteLine("Current score: {0} - {1}", firstPlayerScore, secondPlayerScore);
64 ResetGame();
65 }
66 if (firstPlayerLoses)
67 {
68 secondPlayerScore++;
69 Console.WriteLine();
70 Console.WriteLine("Game over");
71 Console.WriteLine("Second player wins!!!");
72 Console.WriteLine("Current score: {0} - {1}", firstPlayerScore, secondPlayerScore);
73 ResetGame();
74 }
75 if (secondPlayerLoses)
76 {
77 firstPlayerScore++;
78 Console.WriteLine();
79 Console.WriteLine("Game over");
80 Console.WriteLine("First player wins!!!");
81 Console.WriteLine("Current score: {0} - {1}", firstPlayerScore, secondPlayerScore);
82 ResetGame();
83 }
84
85
86 isUsed[firstPlayerColumn, firstPlayerRow] = true;
87 isUsed[secondPlayerColumn, secondPlayerRow] = true;
88
89
90 WriteOnPosition(firstPlayerColumn, firstPlayerRow, '*', ConsoleColor.Yellow);
91 WriteOnPosition(secondPlayerColumn, secondPlayerRow, '*', ConsoleColor.Cyan);
92
93
94 Thread.Sleep(100);
95 }
96 }
File name: Program.cs
Copy
99 static void StartupScreen()
100 {
101 string heading = "A simple tron-like game";
102 Console.CursorLeft = Console.BufferWidth / 2 - heading.Length / 2;
103 Console.WriteLine(heading);
104
105
106 Console.ForegroundColor = ConsoleColor.Yellow;
107 Console.WriteLine("Player 1's controls:\n");
108 Console.WriteLine("W - Up");
109 Console.WriteLine("A - Left");
110 Console.WriteLine("S - Down");
111 Console.WriteLine("D - Right");
112
113 string longestString = "Player 2's controls:";
114 int cursorLeft = Console.BufferWidth - longestString.Length;
115
116 Console.CursorTop = 1;
117 Console.ForegroundColor = ConsoleColor.Cyan;
118 Console.CursorLeft = cursorLeft;
119 Console.WriteLine("Player 2's controls:");
120 Console.CursorLeft = cursorLeft;
121 Console.WriteLine("Up Arrow - Up");
122 Console.CursorLeft = cursorLeft;
123 Console.WriteLine("Left Arrow - Left");
124 Console.CursorLeft = cursorLeft;
125 Console.WriteLine("Down Arrow - Down");
126 Console.CursorLeft = cursorLeft;
127 Console.WriteLine("Right Arrow - Right");
128
129 Console.ReadKey();
130 Console.Clear();
131 }
File name: Program.cs
Copy
132 static void ResetGame()
133 {
134 isUsed = new bool[Console.WindowWidth, Console.WindowHeight];
135 SetGameField();
136 firstPlayerDirection = right;
137 secondPlayerDirection = left;
138 Console.WriteLine("Press any key to start again...");
139 Console.ReadKey();
140 Console.Clear();
141 MovePlayers();
142 }
WriteLine 128 lượt xem
Gõ tìm kiếm nhanh...