Skip to content

Instantly share code, notes, and snippets.

@Shaun420
Created June 11, 2024 13:34
Show Gist options
  • Save Shaun420/f4c398a760a26f1b48391afc3b3c2d02 to your computer and use it in GitHub Desktop.
Save Shaun420/f4c398a760a26f1b48391afc3b3c2d02 to your computer and use it in GitHub Desktop.
C# console game
using System;
Random random = new Random();
Console.CursorVisible = false;
int height = Console.WindowHeight - 1;
int width = Console.WindowWidth - 5;
Console.WriteLine($"Height: {height}, Width: {width}");
bool shouldExit = false;
// Console position of the player
int playerX = 0;
int playerY = 0;
// Console position of the food
int foodX = 0;
int foodY = 0;
// Available player and food strings
string[] states = {"('-')", "(^-^)", "(X_X)"};
string[] foods = {"@@@@@", "$$$$$", "#####"};
// Current player string displayed in the Console
string player = states[0];
// Index of the current food
int food = 0;
InitializeGame();
while (!shouldExit)
{
Move(true, true);
if (TerminalResized()) {
Console.Clear();
shouldExit = true;
Console.WriteLine("Console was resized. Program exiting.");
}
}
// Returns true if the Terminal was resized
bool TerminalResized()
{
return height != Console.WindowHeight - 1 || width != Console.WindowWidth - 5;
}
// Displays random food at a random location
void ShowFood()
{
// Update food to a random index
food = random.Next(0, foods.Length);
// Update food position to a random location
foodX = random.Next(0, width - player.Length);
foodY = random.Next(0, height - 1);
// Display the food at the location
Console.SetCursorPosition(foodX, foodY);
Console.Write(foods[food]);
}
// Changes the player to match the food consumed
void ChangePlayer()
{
player = states[food];
Console.SetCursorPosition(playerX, playerY);
Console.Write(player);
if (IsPlayerUnconscious()) {
FreezePlayer();
}
}
// Temporarily stops the player from moving
void FreezePlayer()
{
System.Threading.Thread.Sleep(1000);
player = states[0];
}
bool PlayerConsumedFood() {
return (playerX == foodX && playerY == foodY);
}
bool IsPlayerUnconscious() {
return (player == "(X_X)");
}
bool IsPlayerHappy() {
return (player == "(^-^)");
}
// Reads directional input from the Console and moves the player
void Move(bool nondirectional = false, bool fastSpeed = false)
{
int lastX = playerX;
int lastY = playerY;
int movespeed;
if (fastSpeed && (IsPlayerHappy())) {
movespeed = 3;
} else {
movespeed = 1;
}
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.UpArrow:
playerY--;
break;
case ConsoleKey.DownArrow:
playerY++;
break;
case ConsoleKey.LeftArrow:
playerX -= movespeed;
break;
case ConsoleKey.RightArrow:
playerX += movespeed;
break;
case ConsoleKey.Escape:
shouldExit = true;
break;
default:
if (nondirectional) {
shouldExit = true;
}
break;
}
// Clear the characters at the previous position
Console.SetCursorPosition(lastX, lastY);
for (int i = 0; i < player.Length; i++)
{
Console.Write(" ");
}
// Keep player position within the bounds of the Terminal window
playerX = (playerX < 0) ? 0 : (playerX >= width ? width : playerX);
playerY = (playerY < 0) ? 0 : (playerY >= height ? height : playerY);
// Draw the player at the new location
Console.SetCursorPosition(playerX, playerY);
Console.Write(player);
if (PlayerConsumedFood()) {
ChangePlayer();
ShowFood();
}
}
// Clears the console, displays the food and player
void InitializeGame()
{
Console.Clear();
ShowFood();
Console.SetCursorPosition(0, 0);
Console.Write(player);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment