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

Cem Tuğanlı 1 Reputation point
2022-10-20T10:05:00.687+00:00

The snake head 0 does not move anywhere when Console.ReadKey() happens.
Here is the full 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 basinca oraya gitmeni saglayacak enum  
               enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };  
               eDirection dir; //enum class gibi çalisiyor enum'dan dir isimli bir object yarattik  

               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 = "!!!!! SIMPLE 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:

   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.

![252406-image.png]1

.NET CLI
.NET CLI
A cross-platform toolchain for developing, building, running, and publishing .NET applications.
326 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,582 questions
{count} votes

1 answer

Sort by: Most helpful
  1. P a u l 10,416 Reputation points
    2022-10-20T15:40:22.763+00:00

    There were a few issues with the snippet:

    • The code in the main method is only ran once, so to keep the game running you need to stick it in a loop. In the example below I've made it run every 10 times a second (1000 / 10) milliseconds
    • Console.ReadKey is a blocking call, so if you were to put it inside a loop the game wouldn't animate because the program's waiting for you to enter a key before it re-renders to the terminal. To fix this the Console.ReadKey needs to run in a separate thread to make sure the rest of your game loop runs uninterrupted.
    • Inside your Draw method the x and y comparisons were reversed.
    • The terminal wasn't being cleared before re-drawing.

    Here's a diff of the bits I've changed:
    https://www.diffchecker.com/fUm8q5Wf

    One thing worth noting is that terminals aren't the best canvases for games (resulting in some flickering due to the clearing/re-drawing to a single text buffer) but I'm guessing this is for education.

    0 comments No comments