ModelingDebug/ModelingMusic/MusicFrom.cs

117 lines
3.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Diagnostics;
using System.Text;
using Debug;
using Debug.ModelingMusic;
namespace ModelingMusic;
public class MusicFrom
{
private long time = 0;
public MusicFrom()
{
setCharArry();
new Thread(PrintCharArray).Start();
}
char[] jianpan = { 'D', 'F', 'G', ' ', 'J', 'K', 'L' };
public void PrintCharArray()
{
var stopwatch = new Stopwatch();
int frameCount = 0;
double totalTime = 0;
int i; int j;
// 获取字符数组的行数和列数
var a = Program.chars.GetLength(0);
var b = Program.chars.GetLength(1);
var buffer = new StringBuilder();
ConsoleColor xiantiaoColor = Program.random.Next(0, 2) == 0 ? ConsoleColor.Red : ConsoleColor.Blue;//线条的颜色
while (Program.IsWhile)
{
stopwatch.Restart();
Console.SetCursorPosition(0, 0);// 设定光标位置
Console.Clear();
buffer.Clear();
for (i = 0; i < a; i++)
{
for (j = 0; j < b; j++)
{
//Console.ForegroundColor = Program.chars[i, j].Color;
//Console.Write($" {Program.chars[i, j].Char} ");
buffer.Append(Program.chars[i, j].Char);
time++;
if (time > 100000)
{
GC.Collect(); time = 0;
}
}
buffer.AppendLine();
//Console.WriteLine();
}
Console.ForegroundColor = ConsoleColor.DarkBlue;
Console.Write(buffer.ToString());
if (time % 1000 == 0)
{
xiantiaoColor = Program.random.Next(0, 2) == 0 ? ConsoleColor.Red : ConsoleColor.Blue;
}
// Console.ForegroundColor = (ConsoleColor)random.Next(0, 16);
Console.ForegroundColor = xiantiaoColor;
buffer.Clear();
for (i = 0; i < b; i++)
{
buffer.Append('-');
}
// 打印ASDF HJKL
buffer.AppendLine();
// int totalLength = 50;
int totalAs = jianpan.Length;
int spaceBetweenAs = (b - totalAs) / (totalAs - 1);
for (i = 0; i < totalAs; i++)
{
for (j = 0; j < spaceBetweenAs / 2; j++)
{
buffer.Append(' ');
}
buffer.Append(jianpan[i]);
for (j = 0; j < spaceBetweenAs / 2; j++)
{
buffer.Append(' ');
}
}
Console.Write(buffer);
// 停止计时并计算本帧的处理时间
stopwatch.Stop();
double elapsedMilliseconds = stopwatch.Elapsed.TotalMilliseconds;
totalTime += elapsedMilliseconds;
frameCount++;
// 计算帧率
double fps = frameCount / (totalTime / 1000.0);
// Console.ResetColor();
Console.ForegroundColor = ConsoleColor.White;
buffer.Clear();
buffer.Append($" {fps:0} FPS ");
Console.WriteLine(buffer);
// 保持每秒约16.67ms即大约60 FPS的间隔
// Thread.Sleep(Math.Max(0, 60 - (int)elapsedMilliseconds));
//这样闪烁不会太严重
Thread.Sleep(100);
}
}
private void setCharArry()
{
for (int i = 0; i < Program.chars.GetLength(0); i++)
{
for (int j = 0; j < Program.chars.GetLength(1); j++)
{
Program.chars[i, j] = new Point(); // 初始化每个元素
}
// Console.WriteLine();
}
}
}