Question Simple Snake Game - Snake Head is not moving after ReadKey()

Joined
Sep 26, 2022
Messages
16
Programming Experience
1-3
The snake head ```0``` does not move anywhere when ```Console.ReadKey()``` happens.
Here is the full code:

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimpleSnakeGame_ConsoleApp
{
    internal class Program
    {
        public bool gameOver = true;
        public int width = 20;
        public int height = 20;

        //HEAD POS
        public int x, y;

        //FRUIT POS
        public int fruitX, fruitY;

        public int score;

        //bir kere basınca oraya gitmeni sağlayacak enum
        enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };
        eDirection dir; //enum class gibi çalışıyor enum'dan dir isimli bir object yarattık

        static void Main(string[] args)
        {

            Program oyun = new Program();

                oyun.Setup();

                oyun.Draw();
                oyun.Input();
                oyun.Logic();


            Console.ReadLine();
        }

        //Setting Up the MAP
        public void Setup()
        {
            gameOver = false;
            string a = "!!!!! SİMPLE SNAKE GAME !!!!!";
            Console.WriteLine(gameOver.ToString() + " " + a, "{0}" + "{1}");
            dir = eDirection.STOP;
            x = width / 2;
            y = height / 2;

            Random rnd = new Random();
            fruitX = rnd.Next(1, 19);
            fruitY = rnd.Next(1, 19);
            score = 0;

        }
        void Draw()
        {
            for (int j = 0; j < height; j++)
            {
                for (int i = 0; i < width; i++)
                {
                    if (i == y && j == x)
                    {
                        Console.Write("0");
                    }
                    else if (i == fruitY && j == fruitX)
                    {
                        Console.Write("F");
                    }
                    else if (j > 0 && j < height - 1 && i > 0 && i < width - 1)
                    {
                        Console.Write(" ");
                    }
                    else
                    {
                        Console.Write("#");
                    }

                }

                Console.WriteLine();
            }

            Console.WriteLine();
        }
        void Input()
        {
            ConsoleKey key;

            // Key is available - read it
            key = Console.ReadKey(true).Key;

            if (key == ConsoleKey.A)
            {
                dir = eDirection.LEFT;
            }
            else if (key == ConsoleKey.D)
            {
                dir = eDirection.RIGHT;
            }
            else if (key == ConsoleKey.W)
            {
                dir = eDirection.UP;
            }
            else if (key == ConsoleKey.S)
            {
                dir = eDirection.DOWN;
            }
            else if (key == ConsoleKey.X)
            {
                gameOver=true;
            }
        }
        void Logic()
        {
            switch (dir)
            {
                case eDirection.LEFT:
                    x--;
                    break;
                case eDirection.RIGHT:
                    x++;
                    break;
                case eDirection.UP:
                    y--;
                    break;
                case eDirection.DOWN:
                    y++;
                    break;
                default:
                    break;
            }
        }
    }

}

I guess the problem is ```Console.ReadKey()``` function here:

C#:
void Input()
        {
            ConsoleKey key;

            // Key is available - read it
            key = Console.ReadKey(true).Key;

            if (key == ConsoleKey.A)
            {
                dir = eDirection.LEFT;
            }
            else if (key == ConsoleKey.D)
            {
                dir = eDirection.RIGHT;
            }
            else if (key == ConsoleKey.W)
            {
                dir = eDirection.UP;
            }
            else if (key == ConsoleKey.S)
            {
                dir = eDirection.DOWN;
            }
            else if (key == ConsoleKey.X)
            {
                gameOver=true;
            }
        }
However I do not know what to replace
C#:
Console.ReadKey()
with and how to do it.

Here is the OUTPUT:

1666256815642.png
 
Looks like the OP also cross posted to Stack Overflow, and Reddit. Someone on Reddit fixed the code for him. It's likely why he has bothered going back to the other places that he spammed.
 
Looks like the OP also cross posted to Stack Overflow, and Reddit. Someone on Reddit fixed the code for him. It's likely why he has bothered going back to the other places that he spammed.
And also on C# Corner and Microsoft Q&A... This is certainly one guy I'll never again bother to respond to.
 
Back
Top Bottom