DownArrow
How do I use Down Arrow
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: Cannon.cs
Copy
159 void CannonMovement(){
160
161 if (Input.GetKey (KeyCode.UpArrow)) {
162 currentRotation.z += 50f * Time.deltaTime;
163 } else if (Input.GetKey (KeyCode.DownArrow)) {
164 currentRotation.z -= 50f * Time.deltaTime;
165 }
166
167 currentRotation.z = Mathf.Clamp(currentRotation.z, minRot, maxRot);
168
169 transform.rotation = Quaternion.Euler (currentRotation);
170 }
File name: Program.cs
Copy
204 static void Main(string[] args)
205 {
206 RemoveScrollBars();
207 SetInitialPositions();
208 while (true)
209 {
210 if (Console.KeyAvailable)
211 {
212 ConsoleKeyInfo keyInfo = Console.ReadKey();
213 if (keyInfo.Key == ConsoleKey.UpArrow)
214 {
215 MoveFirstPlayerUp();
216 }
217 if (keyInfo.Key == ConsoleKey.DownArrow)
218 {
219 MoveFirstPlayerDown();
220 }
221 }
222 SecondPlayerAIMove();
223 MoveBall();
224 Console.Clear();
225 DrawFirstPlayer();
226 DrawSecondPlayer();
227 DrawBall();
228 PrintResult();
229 Thread.Sleep(60);
230 }
231 }
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
246 static void ChangePlayerDirection(ConsoleKeyInfo key)
247 {
248 if (key.Key == ConsoleKey.W && firstPlayerDirection != down)
249 {
250 firstPlayerDirection = up;
251 }
252 if (key.Key == ConsoleKey.A && firstPlayerDirection != right)
253 {
254 firstPlayerDirection = left;
255 }
256 if (key.Key == ConsoleKey.D && firstPlayerDirection != left)
257 {
258 firstPlayerDirection = right;
259 }
260 if (key.Key == ConsoleKey.S && firstPlayerDirection != up)
261 {
262 firstPlayerDirection = down;
263 }
264
265
266 if (key.Key == ConsoleKey.UpArrow && secondPlayerDirection != down)
267 {
268 secondPlayerDirection = up;
269 }
270 if (key.Key == ConsoleKey.LeftArrow && secondPlayerDirection != right)
271 {
272 secondPlayerDirection = left;
273 }
274 if (key.Key == ConsoleKey.RightArrow && secondPlayerDirection != left)
275 {
276 secondPlayerDirection = right;
277 }
278 if (key.Key == ConsoleKey.DownArrow && secondPlayerDirection != up)
279 {
280 secondPlayerDirection = down;
281 }
282 }
DownArrow 126 lượt xem
Gõ tìm kiếm nhanh...