Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Simple Snake Game - Snake Head is not moving after ReadKey()

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

Scheduled Pinned Locked Moved C#
csharplinqgame-devloungeworkspace
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Programmable Physics
    wrote on last edited by
    #1

    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 availab
    
    J 1 Reply Last reply
    0
    • P Programmable Physics

      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 availab
      
      J Offline
      J Offline
      jsc42
      wrote on last edited by
      #2

      At a quick glance, your program reads a key but does not redraw the image. After a key is read (your Input() routine), you call your Logic() routine and then exit. I guess that what you wanted to do was to loop and redo the Draw(), Input(), Logic() steps until the end of the game. Suggestion: Encapsulate those steps in a do ... while loop.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups