Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

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

The snake head ```0``` does not move anywhere when ```Console.ReadKey()``` happens.
Here is the full code:

```
Code:
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:

```
Code:
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 ```Console.ReadKey()``` with and how to do it.

Here is the OUTPUT:

1666256963579.png
 
You need to use Console.ReadKey() instead of Console.ReadKey(true).
For some reason, which I cannot understand from the documentation, the latter blocks until you press enter.

Then if you wonder why nothing is moving (such as a 'snakehead'), have a good look at your program logic and realize that it's not going to move by its own 😉
 
It turns out my advice about not using Console.ReadKey(true) was wrong. It works as expected. I must have had a bad hair day yesterday.
The other advice still stands though. Look at your program logic. So far you seem unaware that you need a loop, and actual actions, to make a game actually do something.
Think about the program logic first. Write it up in pseudo code or even plain language. Then worry about details and implementation.
But I'm probably wasting my time here, as you are not replying either here or on C# forums where you cross posted.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom