ModelingDebug/MinLong/Main.cs

220 lines
6.6 KiB
C#
Raw Normal View History

2024-08-25 23:51:27 +00:00
using System.Diagnostics;
2024-08-25 23:58:04 +00:00
using System.Text;
2024-08-25 23:51:27 +00:00
namespace Debug.MinLong;
2024-08-25 23:58:04 +00:00
#pragma warning disable CA1822 // 将成员标记为 static
#pragma warning disable IDE0051 // 删除未使用的私有成员
#pragma warning disable IDE1006 // 命名样式
2024-08-28 04:19:44 +00:00
public class Main
2024-08-25 23:51:27 +00:00
{
2024-08-28 04:19:44 +00:00
public Main()
2024-08-25 23:51:27 +00:00
{
2024-10-12 02:47:46 +00:00
// Console.WriteLine("MinLong");
2024-08-28 04:19:44 +00:00
ResetChars();//初始化 渲染界面
2024-08-25 23:58:04 +00:00
ResetLong();//初始化小龙
2024-08-25 23:51:27 +00:00
new Thread(PrintCharArray).Start();
2024-08-28 04:19:44 +00:00
var timer = new System.Timers.Timer();
// 设置计时器事件处理方法
timer.Elapsed += (sender, e) =>
{
2024-08-29 01:05:13 +00:00
new Thread(() => Zhangaiwu.Newshitou(xiaoLong)).Start();
2024-08-28 04:19:44 +00:00
int interval = random.Next(3000, 6000); // 1000ms 到 5000ms
timer.Interval = interval;
};
// 启动计时器
timer.Start();
2024-08-25 23:58:04 +00:00
KeyThread();//监听键盘
2024-08-25 23:51:27 +00:00
}
private long time;
2024-08-28 04:19:44 +00:00
//随机数
Random random = new();
2024-08-25 23:51:27 +00:00
public void PrintCharArray()
{
var stopwatch = new Stopwatch();
int frameCount = 0;
double totalTime = 0;
int i; int j;
2024-09-24 07:24:06 +00:00
int p = 0;
2024-08-25 23:58:04 +00:00
var a = Program.chars.GetLength(0);
var b = Program.chars.GetLength(1);
var buffer = new StringBuilder();
2024-08-25 23:51:27 +00:00
while (Program.IsWhile)
{
stopwatch.Restart();
2024-08-25 23:58:04 +00:00
Console.SetCursorPosition(0, 0);// 设定光标位置
2024-08-25 23:51:27 +00:00
Console.Clear();
2024-08-25 23:58:04 +00:00
buffer.Clear();
2024-09-24 07:24:06 +00:00
xiaoLong.Char = xiaoLong.Char == '웃' ? '유' : '웃';
Program.chars[xiaoLong.y, xiaoLong.x].Char = xiaoLong.Char;
2024-08-25 23:51:27 +00:00
for (i = 0; i < a; i++)
{
for (j = 0; j < b; j++)
{
// Console.ForegroundColor = chars[i, j].Color;
// Console.Write($" {chars[i, j].Char} ");
2024-08-25 23:58:04 +00:00
buffer.Append(Program.chars[i, j].Char);
2024-08-25 23:51:27 +00:00
time++;
if (time > 100000)
{
GC.Collect(); time = 0;
}
}
2024-08-25 23:58:04 +00:00
buffer.AppendLine();
2024-08-25 23:51:27 +00:00
}
Console.ForegroundColor = ConsoleColor.White;
for (i = 0; i < b; i++)
{
2024-09-24 07:24:06 +00:00
p = p == i % 2 ? 1 : 0;
if (p == 0)
{
buffer.Append(' ');
}
else
{
buffer.Append('-');
}
2024-08-25 23:58:04 +00:00
}
Console.Write(buffer);
2024-08-25 23:51:27 +00:00
Console.ResetColor();
// 停止计时并计算本帧的处理时间
stopwatch.Stop();
double elapsedMilliseconds = stopwatch.Elapsed.TotalMilliseconds;
totalTime += elapsedMilliseconds;
frameCount++;
// 计算帧率
double fps = frameCount / (totalTime / 1000.0);
Console.SetCursorPosition(0, 0);// 设定光标位置到第二行
2024-08-28 04:19:44 +00:00
Console.WriteLine($" {fps:0} FPS 得分{xiaoLong.score}");
2024-08-25 23:51:27 +00:00
// 保持每秒约16.67ms即大约60 FPS的间隔
// Thread.Sleep(Math.Max(0, 60 - (int)elapsedMilliseconds));
//死方法 不知道为什么 控制台限制60 帧这样闪烁不会太严重
Thread.Sleep(100);
}
}
2024-08-25 23:58:04 +00:00
public class XiaoLong
{
2024-09-24 07:24:06 +00:00
public char Char = '웃';//小龙的形状
2024-08-25 23:58:04 +00:00
public ConsoleColor Color = ConsoleColor.Red;//小龙的颜色
public int x;//小龙X坐标
public int y;//小龙Y坐标
public bool istiao = false;//是否跳
2024-08-29 01:05:13 +00:00
public bool islong = false;//是否长按跳
2024-08-28 04:19:44 +00:00
//分数
public int score = 0;
2024-08-25 23:58:04 +00:00
}
public XiaoLong xiaoLong = new();
int charsy = Program.chars.GetLength(0);
/// <summary>
/// 初始化小龙
/// </summary>
public void ResetLong()
{
int x = Program.chars.GetLength(1);
xiaoLong.x = x / 6;
xiaoLong.y = charsy - 1;
Program.chars[xiaoLong.y, xiaoLong.x].Char = xiaoLong.Char;
}
/// <summary>
/// 全局键盘监听
/// </summary>
public void KeyThread()
{
new Thread(() =>
{
while (Program.IsWhile)
{
ConsoleKeyInfo consoleKeyInfo = Console.ReadKey(true);
if (consoleKeyInfo.Key == ConsoleKey.Escape)
{
Program.IsWhile = false;
Thread.Sleep(500);
Console.Clear();
Environment.Exit(0);
return;
}
// Console.WriteLine(consoleKeyInfo.Key);
2024-10-12 02:47:46 +00:00
if (Program.IsWhile == false)
{
if (consoleKeyInfo.Key == ConsoleKey.Enter)
{
Console.Clear();
Program.IsWhile = true;
_ = new MinLong.Main();
}
return;
}
2024-08-25 23:58:04 +00:00
switch (consoleKeyInfo.Key)
{
//空格
case ConsoleKey.Spacebar:
2024-09-24 07:24:06 +00:00
case ConsoleKey.PageUp:
2024-08-29 01:05:13 +00:00
if (!xiaoLong.istiao) { new Thread(() => tiaoyue(xiaoLong)).Start(); break; }
xiaoLong.islong = true;
2024-08-25 23:58:04 +00:00
break;
}
}
// 等待用户按下任意键后退出
}).Start();
}
/// <summary>
/// 跳跃
/// </summary>
2024-08-28 04:19:44 +00:00
public static void tiaoyue(XiaoLong xiaoLong)
2024-08-25 23:58:04 +00:00
{
if (xiaoLong.istiao) return;
xiaoLong.istiao = true;
// Console.WriteLine("跳跃");
//设置离地高度
for (int i = 0; i < 3; i++)
{
Program.chars[xiaoLong.y, xiaoLong.x].Char = Program.ShowChar;
xiaoLong.y -= 1;
Program.chars[xiaoLong.y, xiaoLong.x].Char = xiaoLong.Char;
Thread.Sleep(100);
}
for (int i = 0; i < 3; i++)
{
2024-08-29 01:05:13 +00:00
if (xiaoLong.islong)
{
Thread.Sleep(50);
}
2024-08-25 23:58:04 +00:00
Program.chars[xiaoLong.y, xiaoLong.x].Char = Program.ShowChar;
xiaoLong.y += 1;
Program.chars[xiaoLong.y, xiaoLong.x].Char = xiaoLong.Char;
Thread.Sleep(100);
}
2024-08-29 01:05:13 +00:00
xiaoLong.islong = false;
2024-08-25 23:58:04 +00:00
xiaoLong.istiao = false;
}
public static void ResetChars()
2024-08-25 23:51:27 +00:00
{
2024-08-25 23:58:04 +00:00
for (int i = 0; i < Program.chars.GetLength(0); i++)
2024-08-25 23:51:27 +00:00
{
2024-08-25 23:58:04 +00:00
for (int j = 0; j < Program.chars.GetLength(1); j++)
2024-08-25 23:51:27 +00:00
{
2024-08-25 23:58:04 +00:00
Program.chars[i, j] = new ModelingMusic.Point();
2024-08-25 23:51:27 +00:00
}
}
}
}