-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathProgram.cs
204 lines (198 loc) · 4.32 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using System;
using System.Collections.Generic;
using System.Threading;
string[] frames = ["▌", "▐",];
string[] deathFrames = ["X", "X", "X", "X", "X",];
List<Note> notes;
List<Note> deadNotes;
TimeSpan delayTime = TimeSpan.FromMilliseconds(34);
TimeSpan spawnTimeMin = TimeSpan.FromMilliseconds(250);
TimeSpan spawnTimeMax = TimeSpan.FromMilliseconds(2000);
int targetLeft = 5;
int remainingMisses = 5;
(int Top, ConsoleKey Key)[] tracks =
[
(4, ConsoleKey.UpArrow),
(7, ConsoleKey.LeftArrow),
(10, ConsoleKey.DownArrow),
(13, ConsoleKey.RightArrow),
];
try
{
int bufferwidth = Console.BufferWidth;
Console.CursorVisible = false;
DateTime lastSpawn;
TimeSpan spawnTime;
int score;
int misses;
void NewNote()
{
notes.Add(new Note()
{
Top = tracks[Random.Shared.Next(tracks.Length)].Top,
Frame = 0,
Left = Console.BufferWidth - 1,
});
lastSpawn = DateTime.Now;
spawnTime = TimeSpan.FromMilliseconds(Random.Shared.Next((int)spawnTimeMin.TotalMilliseconds, (int)spawnTimeMax.TotalMilliseconds));
}
void RenderMisses()
{
Console.SetCursorPosition(0, tracks[^1].Top + 3);
Console.WriteLine("Remaining Misses: " + (remainingMisses - misses));
}
PlayAgain:
misses = 0;
score = 0;
notes = new List<Note>();
deadNotes = new List<Note>();
Console.Clear();
Console.WriteLine("Rythm");
Console.WriteLine();
Console.WriteLine("Press enter to play...");
{
GetInput:
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.Enter: break;
case ConsoleKey.Escape:
Console.Clear();
Console.WriteLine("RRythm Closed.");
return;
default: goto GetInput;
}
}
Console.Clear();
Console.WriteLine("Rythm");
Console.WriteLine();
Console.WriteLine("Time your button presses...");
foreach (var (Top, Key) in tracks)
{
Console.SetCursorPosition(0, Top - 1);
Console.Write(new string('_', Console.BufferWidth));
Console.SetCursorPosition(targetLeft, Top + 1);
Console.Write($"^ {Key}");
}
RenderMisses();
NewNote();
while (true)
{
NextKey:
while (Console.KeyAvailable)
{
ConsoleKey key = Console.ReadKey().Key;
if (key is ConsoleKey.Escape)
{
Console.Clear();
Console.WriteLine("RRythm Closed.");
return;
}
foreach (var (Top, Key) in tracks)
{
if (key == Key)
{
foreach (Note note in notes)
{
if (note.Left > targetLeft + 2)
{
break;
}
if (note.Top == Top && Math.Abs(note.Left - targetLeft) < 3)
{
notes.Remove(note);
note.Frame = deathFrames.Length - 1;
deadNotes.Add(note);
score++;
goto NextKey;
}
}
if (++misses >= remainingMisses)
{
goto GameOver;
}
RenderMisses();
break;
}
}
}
if (bufferwidth != Console.BufferWidth)
{
Console.Clear();
Console.Write("RRythm closed. Console was resized.");
return;
}
if (notes[0].Left <= 0)
{
Console.SetCursorPosition(0, notes[0].Top);
Console.Write(" ");
notes.RemoveAt(0);
if (++misses >= remainingMisses)
{
goto GameOver;
}
RenderMisses();
}
for (int i = 0; i < deadNotes.Count; i++)
{
if (deadNotes[i].Frame < 0)
{
Console.SetCursorPosition(deadNotes[i].Left, deadNotes[i].Top);
Console.Write(" ");
deadNotes.RemoveAt(i--);
}
else
{
Console.SetCursorPosition(deadNotes[i].Left, deadNotes[i].Top);
Console.Write(deathFrames[deadNotes[i].Frame]);
deadNotes[i].Frame--;
}
}
foreach (Note note in notes)
{
note.Frame--;
if (note.Frame < 0)
{
Console.SetCursorPosition(note.Left, note.Top);
Console.Write(" ");
note.Frame = frames.Length - 1;
note.Left--;
}
Console.SetCursorPosition(note.Left, note.Top);
Console.Write(frames[note.Frame]);
}
if (DateTime.Now - lastSpawn > spawnTime)
{
NewNote();
}
Thread.Sleep(delayTime);
}
GameOver:
Console.Clear();
Console.WriteLine("Rythm");
Console.WriteLine();
Console.WriteLine("Score: " + score);
Console.WriteLine();
Console.WriteLine("Play Again [enter], or quit [escape]?");
{
GetInput:
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.Enter: goto PlayAgain;
case ConsoleKey.Escape:
Console.Clear();
Console.WriteLine("RRythm Closed.");
return;
default: goto GetInput;
}
}
}
finally
{
Console.CursorVisible = true;
}
public class Note
{
public int Top;
public int Left;
public int Frame;
}