SmalConsolApp / ConsolenGame / Game.cs
Game.cs
Raw
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsolenGame
{
    internal class Game
    {
        Items snake = new Items("S");
        Items gold = new Items("G");
        Items tor = new Items("T");
        Items player = new Items("P");
        bool play = true;
        bool haveGold = true;

        // create a game
        public void DesignGame()
        {
           while(play)
            {
                Console.Title = "A small console game";
                Console.Clear();
                Console.WriteLine("Welcome to my little console game." +
                    "\nYou are the \"P\" on the board and you must collect the \"G\" and get to the \"T\". " +
                    "\nBut be careful, the \"S\" follows and eats you.\nYou can use arrow keys or \"W, A, S, D\". Have fun playing....\n\n");
                    
                // draw the playing field
                for (int x = 0; x < 10; x++)
                {
                    for (int y = 0; y < 30; y++)
                    {
                        if (snake.getPosX() == x && snake.getPosY() == y)
                        {
                            Console.Write(snake.getChar());
                        }
                        else if (gold.getPosX() == x && gold.getPosY() == y)
                        {
                            Console.Write(gold.getChar());
                        }
                        else if (tor.getPosX() == x && tor.getPosY() == y)
                        {
                            Console.Write(tor.getChar());
                        }
                        else if (player.getPosX() == x && player.getPosY() == y)
                        {
                            Console.Write(player.getChar());
                        }
                        else
                            Console.Write(".");
                    }
                    Console.WriteLine();
                }
                playerRun((int)Console.ReadKey().Key);
                winTheGame();
            }
            newGame();
        }
        // Player input
        public void playerRun(int input)
        {
            // W=87,A=65,S=83,D=68,Left=37,UP=38,Right=39,Down=40
            if(input == 87 || input == 38)
            {
                if (player.getPosX() > 0)
                    player.Up();
            }
            else if(input == 83 || input == 40)
            {
                if (player.getPosX() < 9)
                    player.Down();
            }
            else if (input == 65 || input == 37)
            {
                if (player.getPosY() > 0)
                    player.Left();
            }
            else if (input == 68 || input == 39)
            {
                if (player.getPosY() < 29)
                    player.Right();
            }

            if(input == 87 || input == 65 || input == 83  || input == 68 ||
               input == 37 || input == 38 || input == 39 || input == 40)
            {
                snakeRun();
            }
        }
        // win condition
        public void winTheGame()
        {
            if(player.getPosX() == gold.getPosX() && player.getPosY() == gold.getPosY())
            {
                haveGold = true;
                gold.callGold();
            }

            if(player.getPosX() == tor.getPosX() && player.getPosY() == tor.getPosY() && haveGold)
            {
                play = false;
                Console.WriteLine("\n\n\nCongratulation You Win the Game! \n\n");
            }

            if (player.getPosX() == snake.getPosX() && player.getPosY() == snake.getPosY())
            {
                play = false;
                Console.WriteLine("\n\n\nSorry You Lose the Game! \n\n");
            }
        }
        // move snake
        public void snakeRun()
        {
            if(player.getPosX() > snake.getPosX()){ snake.Down(); }
            else if(player.getPosX() < snake.getPosX()){ snake.Up(); }
            else if(player.getPosY() > snake.getPosY()) { snake.Right(); }
            else if(player.getPosY() < snake.getPosY()) { snake.Left(); }
        }
        // new game
        public void newGame()
        {
            if(play == false)
            {
                Console.WriteLine("\nStart a new Game?  J/N");
                int answer = (int)Console.ReadKey().Key;

                if(answer ==  74)
                {
                    snake = new Items("S");
                    gold = new Items("G");
                    tor = new Items("T");
                    player = new Items("P");
                    play = true;
                    haveGold = false;
                    DesignGame();
                }
            }
        }
    }
}