diff options
| author | Syndamia <kami02882@gmail.com> | 2019-04-18 22:29:54 +0300 |
|---|---|---|
| committer | Syndamia <kami02882@gmail.com> | 2019-04-18 22:29:54 +0300 |
| commit | d6133872475adc6eb367f96b81a30637dd5f77b4 (patch) | |
| tree | c200ed85eb46b96998e1c69dc7dfcc6860972c6e | |
| parent | 2d703ea3fe66ddcecd9425db5b24c44ec34d6724 (diff) | |
| download | Shower-d6133872475adc6eb367f96b81a30637dd5f77b4.tar Shower-d6133872475adc6eb367f96b81a30637dd5f77b4.tar.gz Shower-d6133872475adc6eb367f96b81a30637dd5f77b4.zip | |
Combined key bindings into a dictionary (they are now more modular), key bindings are now customizable in settings tab
| -rw-r--r-- | Bitspace/Bitspace/Program.cs | 130 |
1 files changed, 113 insertions, 17 deletions
diff --git a/Bitspace/Bitspace/Program.cs b/Bitspace/Bitspace/Program.cs index 00508a3..3524218 100644 --- a/Bitspace/Bitspace/Program.cs +++ b/Bitspace/Bitspace/Program.cs @@ -32,8 +32,17 @@ namespace Game { "Stick", new Weapon("Stick", 5, '-', '|') } }; + //these are all the player controls, these hold the value of which button for what is used + private static Dictionary<string, ConsoleKey> keysDict = new Dictionary<string, ConsoleKey>() + { + { "forward", ConsoleKey.W }, { "backward", ConsoleKey.S }, { "left", ConsoleKey.A }, { "right", ConsoleKey.D }, + { "swingUp", ConsoleKey.UpArrow }, { "swingDown", ConsoleKey.DownArrow }, { "swingLeft", ConsoleKey.LeftArrow }, { "swingRight", ConsoleKey.RightArrow } + }; + static void Main(string[] args) { + + Console.BackgroundColor = ConsoleColor.Black; StartMenu(); int s = MapSize(); @@ -51,18 +60,30 @@ namespace Game var keyPressed = Console.ReadKey().Key; //changes player position depending on pressed key - switch (keyPressed) - { - case ConsoleKey.W: if (mobDict["Player"].YPos > 0) mobDict["Player"].YPos--; break; - case ConsoleKey.S: if (mobDict["Player"].YPos < map.Length - 1) mobDict["Player"].YPos++; break; - case ConsoleKey.A: if (mobDict["Player"].XPos > 0) mobDict["Player"].XPos--; break; - case ConsoleKey.D: if (mobDict["Player"].XPos < map.Length * 2 - 1) mobDict["Player"].XPos++; break; - - case ConsoleKey.LeftArrow: mobDict["Player"].WeaponsList.First().Direction = 1; break; - case ConsoleKey.RightArrow: mobDict["Player"].WeaponsList.First().Direction = 2; break; - case ConsoleKey.UpArrow: mobDict["Player"].WeaponsList.First().Direction = 3; break; - case ConsoleKey.DownArrow: mobDict["Player"].WeaponsList.First().Direction = 4; break; - } + //we can't use switch, because it requires constant values, i.e. not variables, so with switch you can't customise buttons + if (keyPressed == keysDict["forward"]) + { if (mobDict["Player"].YPos > 0) mobDict["Player"].YPos--; } + + else if (keyPressed == keysDict["backward"]) + { if (mobDict["Player"].YPos < map.Length - 1) mobDict["Player"].YPos++; } + + else if (keyPressed == keysDict["left"]) + { if (mobDict["Player"].XPos > 0) mobDict["Player"].XPos--; } + + else if (keyPressed == keysDict["right"]) + { if (mobDict["Player"].XPos < map.Length * 2 - 1) mobDict["Player"].XPos++; } + + else if (keyPressed == keysDict["swingLeft"]) + { mobDict["Player"].WeaponsList.First().Direction = 1; } + + else if (keyPressed == keysDict["swingRight"]) + { mobDict["Player"].WeaponsList.First().Direction = 2; } + + else if (keyPressed == keysDict["swingUp"]) + { mobDict["Player"].WeaponsList.First().Direction = 3; } + + else if (keyPressed == keysDict["swingDown"]) + { mobDict["Player"].WeaponsList.First().Direction = 4; } } if(gameTick % 2 == 0) @@ -112,6 +133,7 @@ namespace Game } Console.WriteLine("GAME OVER. YOU DIED."); + Console.WriteLine($"\r\nPress any key to end the program."); ConsoleKey endProgram = Console.ReadKey().Key; } @@ -176,11 +198,13 @@ namespace Game switch (keyPressed) { - case ConsoleKey.UpArrow: if (y > 0) y--; break; - case ConsoleKey.DownArrow: if (y < 1) y++; break; - case ConsoleKey.Enter: if(y == 0) { stop = true; } else { Settings(); } break; + case ConsoleKey.UpArrow: if (y == 1) y--; break; + case ConsoleKey.DownArrow: if (y == 0) y++; break; + case ConsoleKey.Enter: stop = true; break; } } + + if (y == 1) Settings(); } private static void Settings() @@ -197,6 +221,7 @@ namespace Game Console.WriteLine("Settings:"); Console.WriteLine($" Player char: {player.Body}"); Console.WriteLine($" Player color: {player.Color}"); + Console.WriteLine(" Key Bindings Settings"); Console.WriteLine(" Back"); Console.SetCursorPosition(0, y); @@ -209,7 +234,7 @@ namespace Game switch (keyPressed) { case ConsoleKey.UpArrow: if (y > 1) y--; break; - case ConsoleKey.DownArrow: if (y < 3) y++; break; + case ConsoleKey.DownArrow: if (y < 4) y++; break; case ConsoleKey.Enter: { switch (y) @@ -229,12 +254,83 @@ namespace Game { player.Color = ColorPicker(player.Color); } break; - case 3: stop = true; StartMenu(); break; + case 3: + case 4: stop = true; break; + } + } + break; + } + } + + if (y == 3) KeyBindings(); + else StartMenu(); + } + + private static void KeyBindings() + { + bool stop = false; + int y = 1; + int x = 0; + string key = null; + + while (!stop) + { + Console.Clear(); + Console.ForegroundColor = ConsoleColor.Gray; + + Console.WriteLine("Key Bindings"); + Console.WriteLine($" Move forward: {keysDict["forward"]}"); + Console.WriteLine($" Move backward: {keysDict["backward"]}"); + Console.WriteLine($" Move left: {keysDict["left"]}"); + Console.WriteLine($" Move right: {keysDict["right"]}"); + Console.WriteLine($" Swing weapon up: {keysDict["swingUp"]}"); + Console.WriteLine($" Swing weapon down: {keysDict["swingDown"]}"); + Console.WriteLine($" Swing weapon left: {keysDict["swingLeft"]}"); + Console.WriteLine($" Swing weapon right: {keysDict["swingRight"]}"); + Console.WriteLine(" Back"); + + Console.SetCursorPosition(0, y); + Console.ForegroundColor = ConsoleColor.Red; + Console.Write('>'); + Console.CursorVisible = false; + + var keyPressed = Console.ReadKey().Key; + + switch (keyPressed) + { + case ConsoleKey.UpArrow: if (y > 1) y--; break; + case ConsoleKey.DownArrow: if (y < 9) y++; break; + case ConsoleKey.Enter: + { + switch (y) + { + case 1: x = 15; key = "forward"; break; + case 2: x = 16; key = "backward"; break; + case 3: x = 12; key = "left"; break; + case 4: x = 13; key = "right"; break; + case 5: x = 18; key = "swingUp"; break; + case 6: x = 20; key = "swingDown"; break; + case 7: x = 20; key = "swingLeft"; break; + case 8: x = 21; key = "swingRight"; break; + case 9: stop = true; break; + } + + if (y != 9) + { + Console.SetCursorPosition(x, y); + Console.CursorVisible = true; + + keysDict[key] = Console.ReadKey().Key; + + Console.CursorVisible = false; + Console.SetCursorPosition(0, y); } } break; } } + + Settings(); } private static int MapSize() |
