From 20b82beed0aafca70d0a2c07dec5e6ff9df90d10 Mon Sep 17 00:00:00 2001 From: ipvg Date: Thu, 15 Aug 2024 21:54:33 -0400 Subject: [PATCH] end 031 --- .gitignore | 1 + 031_Challenge_mini_game/031_csharp.md | 23 ++- .../Project_minigame/Own/Own.csproj | 10 ++ .../Project_minigame/Own/Program.cs | 141 ++++++++++++++++++ .../Project_minigame/Starter/Program.cs | 4 +- 5 files changed, 171 insertions(+), 8 deletions(-) create mode 100644 031_Challenge_mini_game/Project_minigame/Own/Own.csproj create mode 100644 031_Challenge_mini_game/Project_minigame/Own/Program.cs diff --git a/.gitignore b/.gitignore index 6920c16..08a2456 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +tst/ obj/ bin/ *.sln diff --git a/031_Challenge_mini_game/031_csharp.md b/031_Challenge_mini_game/031_csharp.md index 2c1b775..6bae566 100644 --- a/031_Challenge_mini_game/031_csharp.md +++ b/031_Challenge_mini_game/031_csharp.md @@ -49,7 +49,7 @@ skills; there’s little guidance and no step-by-step instructions. In this module, you'll demonstrate your ability to: -- Use Visual Studio Code to develop a C# console application that uses methods +- Develop a C# console application that uses methods to implement logical workflows. - Understand existing code and make informed changes to design. - Create return values and methods with required and optional parameters. @@ -58,10 +58,10 @@ to implement logical workflows. ## Prepare for challenge -You'll be using Visual Studio Code to develop a small mini-game. Your -application should establish the basics of the game, including updating player -state, manipulating player movement, and consuming and regenerating a food -object. You'll develop each of those features and run a simplified game test. +You'll be developing a small mini-game. Your application should establish the +basics of the game, including updating player state, manipulating player +movement, and consuming and regenerating a food object. You'll develop each of +those features and run a simplified game test. ### Project specification @@ -293,5 +293,18 @@ Verify that movement is normal when the player appearance is `(^-^)`. Congratulations if you succeeded in this challenge! +- [Program.cs](./Project_minigame/Own/Program.cs) + --- +### Summary + +Your goal was to demonstrate the ability to develop features of an application +that achieve a design specification. You needed to decipher existing game code +to make informed decisions to make modifications and additions to the code. + +By creating new methods, adding optional parameters, and using existing code, +you built a functional and fun mini-game! + +Your ability to implement features of this mini-game application based on a +design specification demonstrates your ability to create and call methods. diff --git a/031_Challenge_mini_game/Project_minigame/Own/Own.csproj b/031_Challenge_mini_game/Project_minigame/Own/Own.csproj new file mode 100644 index 0000000..2150e37 --- /dev/null +++ b/031_Challenge_mini_game/Project_minigame/Own/Own.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/031_Challenge_mini_game/Project_minigame/Own/Program.cs b/031_Challenge_mini_game/Project_minigame/Own/Program.cs new file mode 100644 index 0000000..945c0d4 --- /dev/null +++ b/031_Challenge_mini_game/Project_minigame/Own/Program.cs @@ -0,0 +1,141 @@ +Console.CursorVisible = false; +Random random = new Random(); +bool keep_playing = true; +int term_x = Console.WindowWidth - 5; +int term_y = Console.WindowHeight - 1; +int player_x = 0; +int player_y = 0; +int speed = 1; +int[] food_pos = { 0, 0 }; +string[] states = { "('-')", "(^-^)", "(X_x)" }; +string[] foods = { "@@@@@", "$$$$$", "*****" }; +string curr_status = states[0]; +string curr_food = foods[0]; + +void print(string msg, bool new_line = true) { + if (new_line) { + Console.WriteLine(msg); + } else { + Console.Write(msg); + } +} + +void print_player(int pos_x, int pos_y, string status) { + Console.SetCursorPosition(player_x, player_y); + print(status, false); +} + +start_game(); + +void start_game() { + Console.Clear(); + print(curr_status, false); + Console.SetCursorPosition(0, 0); + show_food(); + while (keep_playing) { + move(); + check_food(); + } + Console.Clear(); + Console.CursorVisible = true; +} + +void check_food() { + if (player_x == food_pos[0] && player_y == food_pos[1]) { + change_player(); + show_food(); + } +} + +void check_near() { + //char at_x_pos = + // bool check_x = player_x-1 != " " && player_x+5 != " "; + // if (){ + + // } +} + +// Returns true if the Terminal was resized +bool terminal_resized() { + return term_y != Console.WindowHeight - 1 || + term_x != Console.WindowWidth - 5; +} + +// Displays random food at a random location +void show_food() { + // Update food to a random index + curr_food = foods[random.Next(0, foods.Length)]; + + // Update food position to a random location + food_pos[0] = random.Next(0, term_x - curr_status.Length); + food_pos[1] = random.Next(0, term_y - 1); + + // Display the food at the location + Console.SetCursorPosition(food_pos[0], food_pos[1]); + Console.Write(curr_food); +} + +// Changes the player to match the food consumed +void change_player() { + int status_indx = Array.IndexOf(foods, curr_food); + curr_status = states[status_indx]; + print_player(player_x, player_y, curr_status); + switch (status_indx) { + case 1: + speed += 1; + break; + case 2: + speed = (speed > 1) ? speed - 1 : 1; + freeze_ms(1000); + break; + } +} + +// Temporarily stops the player from moving +void freeze_ms(int millis) { + System.Threading.Thread.Sleep(millis); + curr_status = states[0]; +} + +// Reads directional input from the Console and moves the player +void move() { + int last_x = player_x; + int last_y = player_y; + + switch (Console.ReadKey(true).Key) { + case ConsoleKey.K: + player_y--; + break; + case ConsoleKey.J: + player_y++; + break; + case ConsoleKey.H: + player_x -= speed; + break; + case ConsoleKey.L: + player_x += speed; + break; + case ConsoleKey.Escape: + keep_playing = false; + break; + default: + keep_playing = false; + break; + } + // Clear the characters at the previous position + Console.SetCursorPosition(last_x, last_y); + for (int i = 0; i < curr_status.Length; i++) { + print(" ", false); + } + // Keep player position within the bounds of the Terminal window + player_x = (player_x < 0) ? 0 : (player_x >= term_x ? term_x : player_x); + player_y = (player_y < 0) ? 0 : (player_y >= term_y ? term_y : player_y); + Console.SetCursorPosition(player_x, player_y); + print(curr_status, false); + if (terminal_resized()) { + keep_playing = false; + print("Console was resized. Program exiting."); + freeze_ms(2000); + return; + } +} diff --git a/031_Challenge_mini_game/Project_minigame/Starter/Program.cs b/031_Challenge_mini_game/Project_minigame/Starter/Program.cs index b7dca71..0f9107d 100644 --- a/031_Challenge_mini_game/Project_minigame/Starter/Program.cs +++ b/031_Challenge_mini_game/Project_minigame/Starter/Program.cs @@ -1,6 +1,4 @@ -using System; - -Random random = new Random(); +Random random = new Random(); Console.CursorVisible = false; int height = Console.WindowHeight - 1; int width = Console.WindowWidth - 5;