1

I'm trying to create a simple console game where you take care of yourself by feeding yourself.

using System;

namespace Project1
{
static class Program
{
    static void Main(string[] args)
    {
        int hunger = 100;

        Console.WriteLine("Enter your name: ");

        string name = Console.ReadLine();

        Console.WriteLine("Good choice, " + name);

        Console.WriteLine("Press SPACE to continue");

        while (!(Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Spacebar))
        {
            // do something
            int h = hunger - 2;
        }
        Console.WriteLine("You are hungry," + name);
        Console.WriteLine("Press F to feed");

        while (!(Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.F))
        {
            // do something
            int food = hunger;
        }
    }

}
}    

How can I display the current hunger after a change is made too it? I want to display the food level so the player knows to feed it and does not accidentally over feed it. By any chance is there a way to go back to a earlier line so I don't need to copy and paste the thing forever and ever? Thanks.

Kara
  • 6,115
  • 16
  • 50
  • 57
Ravaen
  • 49
  • 1
  • 7

3 Answers3

0

You can modify the position of the cursor with Console.CursorLeft and Console.CursorTop and overwrite previous values.

Cursor.Left = 0;
Console.Write("Hunger: {0:0.00}", hunger);

EDIT: AS mentioned by "CodeMaster", you can do:

Console.Write("Test {0:0.0}".PadRight(20), hunger);

To make sure you have overwritten the previous data if it differs in length.

LueTm
  • 2,366
  • 21
  • 31
  • This really only answers a small portion of the question. When you do this, you need to make sure that when overwriting parts of the screen, you don't leave previously printed text there. – CodeCaster Aug 11 '15 at 09:43
0

It looks like this will be very broad to answer properly, so I'm going to leave it pretty abstract. You need to put reusable logic in methods. You need a "game loop" or "input loop". You need to learn about variables and passing them into methods.

You also may want to introduce a Player class as opposed to various variables to hold separate values.

Your game loop may look like this:

ConsoleKey input = null;
do
{
    var player = DoGameLogic(input, player);
    PrintGameInfo(player);
    input = ReadInput(player);
}
while (input != ConsoleKey.Q)

When you got all this in order and you want to make the output look nice, take a look at Writing string at the same position using Console.Write in C# 2.0, Advanced Console IO in .NET.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
0

is this something you wanted to achieve:-

static void Main(string[] args)
    {
        const int MAX_FEED_LEVEL = 3; //configure it as per your need
        const int HIGHEST_HUNGER_LEVEL = 0; //0 indicates the Extreme hunger

        int hunger = MAX_FEED_LEVEL, foodLevel = HIGHEST_HUNGER_LEVEL;
        string name = string.Empty;
        char finalChoice = 'N', feedChoice = 'N';

        Console.Write("Enter your name: ");
        name = Console.ReadLine();
        Console.Write("Good choice, {0}!", name);
        Console.WriteLine();
        do
        {
            Console.WriteLine();
            Console.WriteLine("current hunger level : {0}", hunger);
            if (hunger > 0)
                Console.WriteLine("You are hungry, {0}", name);

            Console.Write("Press F to feed : ");
            feedChoice = (char)Console.ReadKey(true).Key;
            if (feedChoice == 'F' || feedChoice == 'f')
            {

                if (foodLevel <= MAX_FEED_LEVEL && hunger > HIGHEST_HUNGER_LEVEL)
                {
                    hunger = hunger - 1; //decrementing hunger by 1 units
                    foodLevel += 1; //incrementing food level
                    Console.WriteLine();
                    Console.WriteLine("Feeded!");
                    Console.WriteLine("Current Food Level : {0}", foodLevel);
                }
                else
                {
                    Console.WriteLine();
                    Console.WriteLine("Well Done! {0} you're no more hungry!", name);
                    goto END_OF_PLAY;
                }
            }
            else
            {
                Console.Clear();
                Console.WriteLine();
                Console.Write("You chose not to feed !");
            }

            Console.WriteLine();
            Console.Write("want to continue the game ? (Y(YES) N(NO))");

            finalChoice = (char)Console.ReadKey(true).Key;

        } while (finalChoice == 'Y' || finalChoice == 'y');
    END_OF_PLAY:
        Console.WriteLine();
        Console.Write("===GAME OVER===");
        Console.ReadKey();
    }
manish
  • 1,450
  • 8
  • 13