Added a volue slider and code for a Highscore system

This commit is contained in:
CreepyCrafter24 2019-11-10 16:42:48 +01:00
parent e65562f91a
commit 1bdc3cebba
3 changed files with 121 additions and 24 deletions

View File

@ -4,6 +4,7 @@ using System.Diagnostics;
using System.Media;
using System.Reflection;
using static System.Console;
using System.Runtime.InteropServices;
//┌─┐
//│ │
@ -28,6 +29,11 @@ namespace testexetrisathlon
{
class Program
{
[DllImport("winmm.dll")]
static extern int waveOutGetVolume(IntPtr hwo, out uint dwVolume);
[DllImport("winmm.dll")]
static extern int waveOutSetVolume(IntPtr hwo, uint dwVolume);
public static string sqr = "■";
public static int[,] grid = new int[23, 10];
public static int[,] droppedtetrominoeLocationGrid = new int[23, 10];
@ -69,8 +75,31 @@ namespace testexetrisathlon
Clear();
}
enum GameState { exit, menu, game, gameOver }
void DrawSymbol()
{
SetCursorPosition(0, 1);
Write(
" ▀▀▀██████▄▄▄\r\n" +
" ▀▀▀████▄\r\n" +
" ▄███████▀ ▀███▄\r\n" +
" ▄███████▀ ▀███▄\r\n" +
" ▄████████ ███▄\r\n" +
" ██████████▄ ███▌\r\n" +
" ▀█████▀ ▀███▄ ▐███\r\n" +
" ▀█▀ ▀███▄ ▐███\r\n" +
" ▀███▄ ███▌\r\n" +
" ▄██▄ ▀███▄ ▐███\r\n" +
" ▄██████▄ ▀███▄███\r\n" +
" █████▀▀████▄▄ ▄█████\r\n" +
" ████▀ ▀▀█████▄▄▄▄█████████▄\r\n" +
" ▀▀ ▀▀██████▀▀ ▀▀██\r\n\r\n" +
" testexetrisathlon v." + assembly.GetName().Version.ToString());
}
void MainN(SoundPlayer intro, SoundPlayer inGame, SoundPlayer gameOver)
{
int NewVolume = (ushort.MaxValue / 10) * SettingsMan.Volume;
waveOutSetVolume(IntPtr.Zero, ((uint)NewVolume & 0x0000ffff) | ((uint)NewVolume << 16));
bool playing = true;
GameState state = GameState.menu;
try
@ -83,36 +112,21 @@ namespace testexetrisathlon
Clear();
gameOver.Stop();
intro.PlayLooping();
SetCursorPosition(0, 1);
Write(
" ▀▀▀██████▄▄▄\r\n" +
" ▀▀▀████▄\r\n" +
" ▄███████▀ ▀███▄\r\n" +
" ▄███████▀ ▀███▄\r\n" +
" ▄████████ ███▄\r\n" +
" ██████████▄ ███▌\r\n" +
" ▀█████▀ ▀███▄ ▐███\r\n" +
" ▀█▀ ▀███▄ ▐███\r\n" +
" ▀███▄ ███▌\r\n" +
" ▄██▄ ▀███▄ ▐███\r\n" +
" ▄██████▄ ▀███▄███\r\n" +
" █████▀▀████▄▄ ▄█████\r\n" +
" ████▀ ▀▀█████▄▄▄▄█████████▄\r\n" +
" ▀▀ ▀▀██████▀▀ ▀▀██\r\n\r\n" +
" testexetrisathlon v." + assembly.GetName().Version.ToString());
DrawSymbol();
SetCursorPosition(10, 18);
WriteLine("Controls: Space");
Write("Controls: Space");
SetCursorPosition(11, 19);
WriteLine("Up, Down, Right");
Write("Up, Down, Right");
SetCursorPosition(11, 20);
WriteLine("Left");
Write("Left");
SetCursorPosition(10, 22);
WriteLine("Press s to start");
Write("Press s to start");
SetCursorPosition(10, 23);
WriteLine("Press x to exit");
Write("Press x to exit");
SetCursorPosition(10, 24);
Write("Press v for settings");
SetCursorPosition(0, 26);
WriteLine("Icon made by Freepik from www.flaticon.com");
Write("Icon made by Freepik from www.flaticon.com");
string tmp = ReadKey(true).KeyChar.ToString().ToLower();
switch (tmp)
{
@ -125,6 +139,30 @@ namespace testexetrisathlon
case "x":
state = GameState.exit;
break;
case "v":
Clear();
DrawSymbol();
bool barActive = true;
while (barActive)
{
SetCursorPosition(3, 20);
Write("Volume: " + new string('=', SettingsMan.Volume * 2) + "[" + SettingsMan.Volume.ToString("00") + "]" + new string('=', 20 - (SettingsMan.Volume * 2)));
switch (ReadKey().Key)
{
case ConsoleKey.LeftArrow:
SettingsMan.Volume--;
break;
case ConsoleKey.RightArrow:
SettingsMan.Volume++;
break;
case ConsoleKey.Enter:
barActive = false;
break;
}
NewVolume = (ushort.MaxValue / 10) * SettingsMan.Volume;
waveOutSetVolume(IntPtr.Zero, ((uint)NewVolume & 0x0000ffff) | ((uint)NewVolume << 16));
}
break;
}
break;
case GameState.game:

View File

@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace testexetrisathlon
{
static class SettingsMan
{
static XElement doc;
static string xmlfile = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\Save.xml";
public static int Volume
{
get {
if (doc == null)
Load();
return toRange(int.Parse(doc.Element("Save").Element("Volume").Value), 0, 10);
}
set {
doc.Element("Save").Element("Volume").Value = toRange(value, 0, 10).ToString();
Save();
}
}
public static int HighScore
{
get {
if (doc == null)
Load();
return int.Parse(doc.Element("Save").Element("HighScore").Value);
}
set {
doc.Element("Save").Element("HighScore").Value = value.ToString();
Save();
}
}
static void Save() => doc.Save(xmlfile);
static void Load()
{
if (!File.Exists(xmlfile))
new XElement("Save", new XElement("Volume", 10), new XElement("HighScore", 0)).Save(xmlfile);
doc = XDocument.Load(xmlfile).Root;
if (doc.Element("Save") == null)
doc.Add(new XElement("Save"));
if (doc.Element("Save").Element("Volume") == null)
doc.Element("Save").Add(new XElement("Volume", 10));
if (doc.Element("Save").Element("HighScore") == null)
doc.Element("Save").Add(new XElement("HighScore", 10));
}
static int toRange(int value, int rangeStart, int rangeEnd) => Math.Min(Math.Max(value, rangeStart), rangeEnd);
}
}

View File

@ -71,6 +71,7 @@
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SettingsMan.cs" />
<Compile Include="Tetrominoe.cs" />
</ItemGroup>
<ItemGroup>
@ -83,6 +84,8 @@
</ItemGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
</ItemGroup>
<ItemGroup>
<Content Include="tetris_yUxH6t_256px.ico" />