C# 控制台项目小游戏

 

 

 

 

 代码:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading;
  6 using System.Threading.Tasks;
  7 
  8 
  9 class Program
 10 {
 11     //方向
 12     static int left = 0;
 13     static int right = 1;
 14     static int up = 2;
 15     static int down = 3;
 16 
 17 
 18     static int firstPlayerScore = 0;
 19     static int firstPlayerDirection = right;
 20     static int firstPlayerColumn = 0; // column位置
 21     static int firstPlayerRow = 0; // row位置
 22 
 23 
 24     static int secondPlayerScore = 0;
 25     static int secondPlayerDirection = left;
 26     static int secondPlayerColumn = 240; // column位置
 27     static int secondPlayerRow = 5; // row位置
 28 
 29 
 30     static bool[,] isUsed;
 31 
 32     static void Main(string[] args)
 33     {
 34         //Program2.Main(args);
 35         MainTwoPlayers(args);
 36     }
 37     static void MainTwoPlayers(string[] args)
 38     {
 39         SetGameField();//设置窗口大小和玩家初始位置
 40         StartupScreen();//提示操作
 41 
 42         isUsed = new bool[Console.WindowWidth, Console.WindowHeight];
 43 
 44         Console.WriteLine("isUsed.GetValue(1):" + isUsed.GetValue(0,1));
 45         while (true)
 46         {
 47             //Console.WriteLine(Console.KeyAvailable); 
 48             if (Console.KeyAvailable)//按下一个键时为true,
 49             {
 50                 ConsoleKeyInfo keyInfo = Console.ReadKey(true);
 51                 
 52                 if (keyInfo.Key==ConsoleKey.Delete)
 53                 {
 54                     Console.ReadKey();//Delete键暂停
 55                 }
 56                 ChangePlayerDirection(keyInfo);//判断按键改变方向是否有效,向上的时候,向下操作失灵,向右时按向左或向右失灵
 57             }
 58 
 59 
 60             MovePlayers();//左右位置++或--
 61 
 62 
 63             bool firstPlayerLoses = DoesPlayerLose(firstPlayerRow, firstPlayerColumn);
 64             bool secondPlayerLoses = DoesPlayerLose(secondPlayerRow, secondPlayerColumn);
 65 
 66 
 67             if (firstPlayerLoses && secondPlayerLoses)
 68             {
 69                 firstPlayerScore++;
 70                 secondPlayerScore++;
 71                 Console.WriteLine();
 72                 Console.WriteLine("Game over");
 73                 Console.WriteLine("Draw game!!!");
 74                 Console.WriteLine("Current score: {0} - {1}", firstPlayerScore, secondPlayerScore);
 75                 ResetGame();
 76             }
 77             if (firstPlayerLoses)
 78             {
 79                 secondPlayerScore++;
 80                 Console.WriteLine();
 81                 Console.WriteLine("Game over");
 82                 Console.WriteLine("Second player wins!!!");
 83                 Console.WriteLine("Current score: {0} - {1}", firstPlayerScore, secondPlayerScore);
 84                 ResetGame();
 85             }
 86             if (secondPlayerLoses)
 87             {
 88                 firstPlayerScore++;
 89                 Console.WriteLine();
 90                 Console.WriteLine("Game over");
 91                 Console.WriteLine("First player wins!!!");
 92                 Console.WriteLine("Current score: {0} - {1}", firstPlayerScore, secondPlayerScore);
 93                 ResetGame();
 94             }
 95 
 96 
 97             isUsed[firstPlayerColumn, firstPlayerRow] = true;
 98             isUsed[secondPlayerColumn, secondPlayerRow] = true;
 99 
100 
101             WriteOnPosition(firstPlayerColumn, firstPlayerRow, '*', ConsoleColor.Green);
102             WriteOnPosition(secondPlayerColumn, secondPlayerRow, '*', ConsoleColor.Cyan);
103 
104 
105             Thread.Sleep(100);
106         }
107     }
108 
109 
110     static void StartupScreen()
111     {
112         string heading = "A simple tron-like game";
113         Console.CursorLeft = Console.BufferWidth / 2 - heading.Length / 2;
114         Console.WriteLine(heading);
115 
116 
117         Console.ForegroundColor = ConsoleColor.Yellow;
118         Console.WriteLine("Player 1's controls:\n");
119         Console.WriteLine("W - Up");
120         Console.WriteLine("A - Left");
121         Console.WriteLine("S - Down");
122         Console.WriteLine("D - Right");
123 
124         string longestString = "Player 2's controls:";
125         int cursorLeft = Console.BufferWidth - longestString.Length;
126 
127         Console.CursorTop = 1;
128         Console.ForegroundColor = ConsoleColor.Cyan;
129         Console.CursorLeft = cursorLeft;
130         Console.WriteLine("Player 2's controls:");
131         Console.CursorLeft = cursorLeft;
132         Console.WriteLine("Up Arrow - Up");
133         Console.CursorLeft = cursorLeft;
134         Console.WriteLine("Left Arrow - Left");
135         Console.CursorLeft = cursorLeft;
136         Console.WriteLine("Down Arrow - Down");
137         Console.CursorLeft = cursorLeft;
138         Console.WriteLine("Right Arrow - Right");
139 
140         Console.ReadKey();
141         Console.Clear();
142     }
143     static void ResetGame()
144     {
145         isUsed = new bool[Console.WindowWidth, Console.WindowHeight];
146         SetGameField();
147         firstPlayerDirection = right;
148         secondPlayerDirection = left;
149         Console.WriteLine("Press any key to start again...");
150         Console.ReadKey();
151         Console.Clear();
152         MovePlayers();
153     }
154 
155 
156     static bool DoesPlayerLose(int row, int col)
157     {
158         if (row < 0)
159         {
160             return true;
161         }
162         if (col < 0)
163         {
164             return true;
165         }
166         if (row >= Console.WindowHeight)
167         {
168             return true;
169         }
170         if (col >= Console.WindowWidth)
171         {
172             return true;
173         }
174 
175 
176         if (isUsed[col, row])
177         {
178             return true;
179         }
180 
181 
182         return false;
183     }
184 
185 
186     static void SetGameField()
187     {
188         Console.WindowHeight = 30;
189         Console.BufferHeight = 30;
190 
191 
192         Console.WindowWidth = 100;
193         Console.BufferWidth = 100;
194 
195 
196         /*
197          * 
198          * ->>>>            <<<<-
199          * 
200          */
201         firstPlayerColumn = 0;
202         firstPlayerRow = Console.WindowHeight / 2;
203 
204 
205         secondPlayerColumn = Console.WindowWidth - 1;
206         secondPlayerRow = Console.WindowHeight / 2;
207     }
208 
209      // 摘要:
210      //      根据方向字段移动位置
211     static void MovePlayers()
212     {
213         if (firstPlayerDirection == right)
214         {
215             firstPlayerColumn++;
216         }
217         if (firstPlayerDirection == left)
218         {
219             firstPlayerColumn--;
220         }
221         if (firstPlayerDirection == up)
222         {
223             firstPlayerRow--;
224         }
225         if (firstPlayerDirection == down)
226         {
227             firstPlayerRow++;
228         }
229 
230 
231         if (secondPlayerDirection == right)
232         {
233             secondPlayerColumn++;
234         }
235         if (secondPlayerDirection == left)
236         {
237             secondPlayerColumn--;
238         }
239         if (secondPlayerDirection == up)
240         {
241             secondPlayerRow--;
242         }
243         if (secondPlayerDirection == down)
244         {
245             secondPlayerRow++;
246         }
247     }
248 
249     //画新的移动位置
250     static void WriteOnPosition(int x, int y, char ch, ConsoleColor color)
251     {
252         Console.ForegroundColor = color;
253         Console.SetCursorPosition(x, y);
254         Console.Write(ch);
255     }
256 
257     //改变玩家方向
258     static void ChangePlayerDirection(ConsoleKeyInfo key)
259     {
260         if (key.Key == ConsoleKey.W && firstPlayerDirection != down)
261         {
262             firstPlayerDirection = up;
263         }
264         if (key.Key == ConsoleKey.A && firstPlayerDirection != right)
265         {
266             firstPlayerDirection = left;
267         }
268         if (key.Key == ConsoleKey.D && firstPlayerDirection != left)
269         {
270             firstPlayerDirection = right;
271         }
272         if (key.Key == ConsoleKey.S && firstPlayerDirection != up)
273         {
274             firstPlayerDirection = down;
275         }
276 
277 
278         if (key.Key == ConsoleKey.UpArrow && secondPlayerDirection != down)
279         {
280             secondPlayerDirection = up;
281         }
282         if (key.Key == ConsoleKey.LeftArrow && secondPlayerDirection != right)
283         {
284             secondPlayerDirection = left;
285         }
286         if (key.Key == ConsoleKey.RightArrow && secondPlayerDirection != left)
287         {
288             secondPlayerDirection = right;
289         }
290         if (key.Key == ConsoleKey.DownArrow && secondPlayerDirection != up)
291         {
292             secondPlayerDirection = down;
293         }
294     }
295 }
View Code

 

 

posted @ 2020-03-26 15:36  好Wu赖  阅读(1227)  评论(0编辑  收藏  举报