欢迎光临
我们一直在努力

c#实现简单金山打字小游戏(源码)

using GameDemo.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GameDemo
{
class Program
{
static void Main(string[] args)
{

int total=0;//计时

console.writeline("开始游戏");
Console.WriteLine("准备好开始游戏吗?y/n?");
if (Console.ReadLine().Equals("n")) {
Console.WriteLine("游戏已退出!");
return;
}

Console.WriteLine("请输入关卡数量");
int gk = Int32.Parse(Console.ReadLine());
Console.WriteLine("请输入每个关卡输入的次数");
int count = Int32.Parse(Console.ReadLine());
Console.WriteLine("请输入闯关输入的字数的个数");
int size = Int32.Parse(Console.ReadLine());

for (int i = 0; i <gk; i++)
{

for (int j = 0; j <count; j++)
{
Console.WriteLine("这是第"+(i+1)+"关"+"第"+(j+1)+"次");
//产生随机字母
string str = new RandomUtils().CreateRandomWord(size);
Console.WriteLine("你要输入的内容为:"+str);
//时间计算
DateTime start = DateTime.Now;
//等待用户输入
string userinput=Console.ReadLine();
DateTime end = DateTime.Now;

int t= (int)(end.Ticks - start.Ticks)/10000000;//单次计时
total += t;//总计时

//检查用户输入是否正确
if (userinput.Equals(str))
{

Console.WriteLine("恭喜,你输入对了!用时"+t+"秒");

}
else {

Console.WriteLine("抱歉,你输入错了,游戏结束!");
return;
}

}
if (i == gk-1) {//闯完所有关卡

Console.WriteLine("恭喜你全部过关,总用时为"+total+"秒");
return;
}
Console.WriteLine("准备好进入下一关了吗 y/n");
string comd = Console.ReadLine();

if (comd.Equals("n")) {
Console.WriteLine("游戏已退出!");
return;
}
}

}
}
}

 

//生产字符串的工具类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GameDemo.Utils
{
class RandomUtils
{
/// <summary>
/// 用来装载字符的数组
/// </summary>
private char[] chars = new char[50];

/// <summary>
/// 初始化数组数据
/// </summary>
public RandomUtils() {

//得到a-z的字符
int idx=0;
for (int i = 'a'; i <'z'+1; i++)
{
if (i == 'o') {//去掉o字母
continue;
}
chars[idx] += (char)i;
idx++;

}

//得到1-9的字符
int idx2=idx;
for (int j ='0'; j <'9'+1; j++)
{
chars[idx2++] = (char)j;

}

//重新组装数据
char[] newchars = new char[idx2];
for (int m = 0; m <idx2; m++)
{
if (chars[m] == 'l') {//将小写的l换成L

chars[m] = 'L';

}
newchars[m] = chars[m];

}
//将重组后的新数组赋值给原来的数组便于给其他方法访问数组数据
chars = newchars;

}

/// <summary>
/// 随机产生字符串
/// </summary>
/// <param name="size">产生的字符串个数</param>
/// <returns></returns>
public string CreateRandomWord(int size) {

StringBuilder builder = new StringBuilder();
Random r = new Random();
for (int i = 0; i <size; i++)
{

char c = chars[r.Next(chars.Length)];

if (builder.ToString().Contains(c)) {//处理字符串重复出现
i--;
continue;
}
builder.Append(c);

}

return builder.ToString();
}
}
}

 

  • 海报
海报图正在生成中...
赞(0) 打赏
声明:
1、本博客不从事任何主机及服务器租赁业务,不参与任何交易,也绝非中介。博客内容仅记录博主个人感兴趣的服务器测评结果及一些服务器相关的优惠活动,信息均摘自网络或来自服务商主动提供;所以对本博客提及的内容不作直接、间接、法定、约定的保证,博客内容也不具备任何参考价值及引导作用,访问者需自行甄别。
2、访问本博客请务必遵守有关互联网的相关法律、规定与规则;不能利用本博客所提及的内容从事任何违法、违规操作;否则造成的一切后果由访问者自行承担。
3、未成年人及不能独立承担法律责任的个人及群体请勿访问本博客。
4、一旦您访问本博客,即表示您已经知晓并接受了以上声明通告。
文章名称:《c#实现简单金山打字小游戏(源码)》
文章链接:https://www.456zj.com/41044.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址