现在的位置: 首页 > 综合 > 正文

C#写一个控制台小游戏(猜拳游戏)

2012年09月11日 ⁄ 综合 ⁄ 共 2295字 ⁄ 字号 评论关闭

  今天中午花了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 }

这游戏,不知道你们兴趣怎样,想起了童年的剪刀石头布....

抱歉!评论已关闭.