C#寫一個控制台小游戲(猜拳游戲)


  今天中午花了1個多小時的時間寫了一個C#控制台的小游戲.至於我為什么寫為控制台界面,一是控制台不需要做界面,簡單,二是內存占用的少(32位占用1.2MB內存,64位占用1.8MB內存).

   此游戲設計思路十分簡單,就是不斷的要求用戶鍵入鍵盤上的鍵來達到游戲的目的。游戲是屬於人和機器人對戰的游戲,廢話不多講,144行代碼(無注釋):

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 
  7 namespace A_Smart_Game
  8 {
  9     class Program
 10     {
 11 
 12         static string shuoming;
 13         static int maxgrade;
 14         static void Main(string[] args)
 15         {
 16             Console.Title = "猜拳游戲(石頭、剪子、布)!";
 17             shuoming  = "猜拳游戲游戲說明:\n\r點相應的鍵代表石頭(數字鍵盤1)、剪子(數字鍵盤2)、布(數字鍵盤3),按B查看說明,按數字鍵盤0查看分數情況,按R重新開始,按ESC退出,贏一次得一分,最先贏得指定的分數者得即為贏家。\n\r";
 18 
 19             Console.Write(shuoming);
 20             Console.Write("設置游戲的局數(即進行多少次):");
 21             maxgrade = int.Parse(Console.ReadLine());
 22             Console.Write("請選擇操作:\n\r[S]開始游戲 \n\r[E]結束游戲\n\r");
 23            ConsoleKeyInfo cki= Console.ReadKey();
 24            if (cki.Key == ConsoleKey.S || cki.Key == ConsoleKey.S)
 25             {
 26                 Start();
 27             }
 28             else if(cki.Key==ConsoleKey.E)
 29             {
 30                 return;
 31             }
 32             else
 33             {
 34                 Main(null);
 35             }
 36         }
 37 
 38         private static void Start()
 39         {
 40             int playergrade = 0;
 41             int computergrade = 0;
 42             Console.WriteLine("游戲開始!!");
 43             while (true)
 44             {
 45                 if (playergrade == 30)
 46                 {
 47                     Console.WriteLine("你贏了,你是最后的贏家!!");
 48                 }
 49                 else if (computergrade == 30)
 50                 {
 51                     Console.WriteLine("你輸了,電腦是最后的贏家!!");
 52                 }
 53 
 54                 bool isc = false;
 55                 Console.Write("輸入你的操作:");
 56                 ConsoleKeyInfo cki= Console.ReadKey();
 57                 Console.WriteLine();
 58                 int com = new Random().Next(1, 3);
 59                 int pla=0;
 60                 switch (cki.Key)
 61                 { 
 62                     case ConsoleKey.NumPad1:
 63                         {
 64                             isc = true;
 65                             pla = 1;
 66                             break;
 67                         }
 68                     case ConsoleKey.NumPad2:
 69                         {
 70                             isc = true;
 71                             pla = 2;
 72                             break;
 73                         }
 74                     case ConsoleKey.NumPad3:
 75                         {
 76                             isc = true;
 77                             pla = 3;
 78                             break;
 79                         }
 80                     case ConsoleKey.NumPad0:
 81                         {
 82                             Console.WriteLine("玩家{0}分,電腦{1}分", playergrade, computergrade);
 83                             break;
 84                         }
 85                     case ConsoleKey.B:
 86                         {
 87                             Console.Write(shuoming);
 88                             break;
 89                         }
 90                     case ConsoleKey.Escape:
 91                         {
 92                             return;
 93                             
 94                         }
 95                     case ConsoleKey.R:
 96                         {
 97                             Start();
 98                             break;
 99                         }
100                     default:
101                             {
102                                 Console.WriteLine("請輸入有效鍵,具體請按下B鍵看說明");
103                                 break;
104                             }
105                 }
106                 if (isc)
107                 {
108                     if (Math.Abs(com - pla) == 2)  //電腦布,玩家剪刀反之
109                     {
110                         if (com < pla)
111                         {
112                             computergrade++;
113                             Console.WriteLine("輸了。。。。。。");
114                         }
115                         else
116                         {
117                             playergrade++;
118                             Console.WriteLine("贏了!!");
119                         }
120                     }
121                     else if (Math.Abs(com - pla) == 1)  //電腦布,玩家石頭 ;反之
122                     {
123                         if (com > pla)
124                         {
125                             computergrade++;
126                             Console.WriteLine("輸了。。。。。。");
127 
128                         }
129                         else
130                         {
131                             playergrade++;
132                             Console.WriteLine("贏了!!");
133                         }
134                     }
135                     else if (com - pla == 0)
136                     {
137                         Console.WriteLine("平了");
138 
139                     }
140                 }
141             }
142         } 
143     }
144 }


這游戲,不知道你們興趣怎樣,想起了童年的剪刀石頭布....


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM