RightArrow
How do I use Right Arrow
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: PlayerController.cs
Copy
43 void PlayerMovement(){
44 if(Input.GetKey(KeyCode.LeftArrow)){
45 position.x -= speed * Time.deltaTime;
46 }else if(Input.GetKey(KeyCode.RightArrow)){
47 position.x += speed * Time.deltaTime;
48 }
49
50 position.x = Mathf.Clamp (position.x, maxLeft + 0.5f, maxRight - 0.5f);
51 transform.position = position;
52
53 }
File name: Program.cs
Copy
31 static void Main()
32 {
33 double speed = 100.0;
34 double acceleration = 0.5;
35 int playfieldWidth = 5;
36 int livesCount = 5;
37 Console.BufferHeight = Console.WindowHeight = 20;
38 Console.BufferWidth = Console.WindowWidth = 30;
39 Object userCar = new Object();
40 userCar.x = 2;
41 userCar.y = Console.WindowHeight - 1;
42 userCar.c = '@';
43 userCar.color = ConsoleColor.Yellow;
44 Random randomGenerator = new Random();
45 List
46 while (true)
47 {
48 speed += acceleration;
49 if (speed > 400)
50 {
51 speed = 400;
52 }
53
54 bool hitted = false;
55 {
56 int chance = randomGenerator.Next(0, 100);
57 if (chance < 10)
58 {
59 Object newObject = new Object();
60 newObject.color = ConsoleColor.Cyan;
61 newObject.c = '-';
62 newObject.x = randomGenerator.Next(0, playfieldWidth);
63 newObject.y = 0;
64 objects.Add(newObject);
65 }
66 else if (chance < 20)
67 {
68 Object newObject = new Object();
69 newObject.color = ConsoleColor.Cyan;
70 newObject.c = '*';
71 newObject.x = randomGenerator.Next(0, playfieldWidth);
72 newObject.y = 0;
73 objects.Add(newObject);
74 }
75 else
76 {
77 Object newCar = new Object();
78 newCar.color = ConsoleColor.Green;
79 newCar.x = randomGenerator.Next(0, playfieldWidth);
80 newCar.y = 0;
81 newCar.c = '#';
82 objects.Add(newCar);
83 }
84 }
85
86 while (Console.KeyAvailable)
87 {
88 ConsoleKeyInfo pressedKey = Console.ReadKey(true);
89 //while (Console.KeyAvailable) Console.ReadKey(true);
90 if (pressedKey.Key == ConsoleKey.LeftArrow)
91 {
92 if (userCar.x - 1 >= 0)
93 {
94 userCar.x = userCar.x - 1;
95 }
96 }
97 else if (pressedKey.Key == ConsoleKey.RightArrow)
98 {
99 if (userCar.x + 1 < playfieldWidth)
100 {
101 userCar.x = userCar.x + 1;
102 }
103 }
104 }
105 List
106 for (int i = 0; i < objects.Count; i++)
107 {
108 Object oldCar = objects[i];
109 Object newObject = new Object();
110 newObject.x = oldCar.x;
111 newObject.y = oldCar.y + 1;
112 newObject.c = oldCar.c;
113 newObject.color = oldCar.color;
114 if (newObject.c == '*' && newObject.y == userCar.y && newObject.x == userCar.x)
115 {
116 speed -= 20;
117 }
118 if (newObject.c == '-' && newObject.y == userCar.y && newObject.x == userCar.x)
119 {
120 livesCount++;
121 }
122 if (newObject.c == '#' && newObject.y == userCar.y && newObject.x == userCar.x)
123 {
124 livesCount--;
125 hitted = true;
126 speed += 50;
127 if (speed > 400)
128 {
129 speed = 400;
130 }
131 if (livesCount <= 0)
132 {
133 PrintStringOnPosition(8, 10, "GAME OVER!!!", ConsoleColor.Red);
134 PrintStringOnPosition(8, 12, "Press [enter] to exit", ConsoleColor.Red);
135 Console.ReadLine();
136 Environment.Exit(0);
137 }
138 }
139 if (newObject.y < Console.WindowHeight)
140 {
141 newList.Add(newObject);
142 }
143 }
144 objects = newList;
145 Console.Clear();
146 if (hitted)
147 {
148 objects.Clear();
149 PrintOnPosition(userCar.x, userCar.y, 'X', ConsoleColor.Red);
150 }
151 else
152 {
153 PrintOnPosition(userCar.x, userCar.y, userCar.c, userCar.color);
154 }
155 foreach (Object car in objects)
156 {
157 PrintOnPosition(car.x, car.y, car.c, car.color);
158 }
159
160 // Draw info
161 PrintStringOnPosition(8, 4, "Lives: " + livesCount, ConsoleColor.White);
162 PrintStringOnPosition(8, 5, "Speed: " + speed, ConsoleColor.White);
163 PrintStringOnPosition(8, 6, "Acceleration: " + acceleration, ConsoleColor.White);
164 //Console.Beep();
165 Thread.Sleep((int)(600 - speed));
166 }
167 }
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
75 static void Main()
76 {
77 Console.OutputEncoding = Encoding.GetEncoding(1252);
78 Console.CursorVisible = false;
79 Console.Title = "Tetris";
80 Console.WindowWidth = GameWidth;
81 Console.BufferWidth = GameWidth;
82 Console.WindowHeight = GameHeight + 1;
83 Console.BufferHeight = GameHeight + 1;
84
85 StartNewGame();
86 PrintBorders();
87
88 Task.Run(() =>
89 {
90 while (true)
91 {
92 PlaySound();
93 }
94 });
95
96 while(true)
97 {
98 if (Console.KeyAvailable)
99 {
100 var key = Console.ReadKey();
101 if (key.Key == ConsoleKey.LeftArrow)
102 {
103 if (currentFigureCol > 1)
104 {
105 currentFigureCol--;
106 }
107 }
108 else if (key.Key == ConsoleKey.RightArrow)
109 {
110 if (currentFigureCol + currentFigure.GetLength(1) - 1 < TetrisWidth)
111 {
112 currentFigureCol++;
113 }
114 }
115 }
116
117 if (CollisionDetection())
118 {
119 PlaceCurrentFigure();
120 int removedLines = CheckForFullLines();
121
122 if (removedLines > 0)
123 {
124 Score += scorePerLines[removedLines - 1] * Level;
125 }
126
127 Level = Score / 1000 + 1;
128
129 currentFigure = nextFigure;
130 nextFigure = Figures[random.Next(0, Figures.Length)];
131 currentFigureRow = 1;
132 currentFigureCol = 4;
133 }
134 else
135 {
136 currentFigureRow++;
137 }
138
139 PrintInfoPanel();
140
141 PrintGameField();
142
143 PrintBorders();
144
145 PrintFigure(currentFigure,
146 currentFigureRow, currentFigureCol);
147
148 Thread.Sleep(speedPerLevel[Level - 1]);
149 }
150 }
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 }
RightArrow 151 lượt xem
Gõ tìm kiếm nhanh...