Jump to content
  • Advertisement
Sign in to follow this  
  • entries
    9
  • comments
    109
  • views
    2209

c# console snake game

phil67rpg

787 views

I have decided to work on a console based snake game, I am doing this because I want to learn basic c# skills before move on to c#  graphics programming.

I am able to move the snake from the center to the edge of the screen, it is just a start, but I am learning.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace snakegame
{
    class Program
    {
        static void Main(string[] args)
        {
            const int board_size = 17;
            char[,] board = new char[board_size, board_size];
            int horizontal = 5, vertical = 8, count = 0;

            for (int i = 0; i < board_size; i++) 
            {
                for (int j = 0; j < board_size; j++) 
                {
                    board[i, j] = '.';
                }
            }

            void draw_board()
            {
                for (int i = 0; i < board_size; i++)
                {
                    for (int j = 0; j < board_size; j++)
                    {
                        Console.Write(board[i, j]);
                    }
                    Console.WriteLine();
                }
            }

            while (count < 6)
            {
                board[vertical, horizontal] = '*';
                board[vertical, horizontal + 1] = '*';
                board[vertical, horizontal + 2] = '*';
                board[vertical, horizontal + 3] = '*';
                board[vertical, horizontal + 4] = '*';
                board[vertical, horizontal + 5] = '*';
                board[vertical, horizontal + 6] = '*';

                draw_board();

                horizontal++;
                count++;

                System.Threading.Thread.Sleep(1000);
                Console.Clear();
            }
        }
    }
}

 



10 Comments


Recommended Comments

I think it is better to use Timer instead of Sleep. I found this example in the Internet:

using System;
using System.Threading;

namespace Snake_ASCIICSharp
{
    class Program
    {
        public static void Main()
        {
            // Create a Timer object that knows to call our TimerCallback
            // method once every 2000 milliseconds.
            Timer t = new Timer(TimerCallback, null, 0, 2000);
            // Wait for the user to hit <Enter>
            Console.ReadLine();
        }

        private static void TimerCallback(Object o)
        {
            // Display the date/time when this method got called.
            Console.WriteLine("In TimerCallback: " + DateTime.Now);
            // Force a garbage collection to occur for this demo.
            GC.Collect();
        }
    }
}

 

Edited by 8Observer8

Share this comment


Link to comment

here is my updated code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace snakegame
{
    class Program
    {
        static void Main(string[] args)
        {
            const int board_size = 17;
            char[,] board = new char[board_size, board_size];
            int horizontal = 5, vertical = 8, count = 0;

            for (int i = 0; i < board_size; i++) 
            {
                for (int j = 0; j < board_size; j++) 
                {
                    board[i, j] = '.';
                }
            }

            void draw_board()
            {
                for (int i = 0; i < board_size; i++)
                {
                    for (int j = 0; j < board_size; j++)
                    {
                        Console.Write(board[i, j]);
                    }
                    Console.WriteLine();
                }
            }

            while (count < 6)
            {
                board[vertical, horizontal] = '*';
                board[vertical, horizontal + 1] = '*';
                board[vertical, horizontal + 2] = '*';
                board[vertical, horizontal + 3] = '*';
                board[vertical, horizontal + 4] = '*';
                board[vertical, horizontal + 5] = '*';
                board[vertical, horizontal + 6] = '*';

                draw_board();

                horizontal++;
                count++;

                System.Threading.Thread.Sleep(1000);
                Console.Clear();
            }
        }
    }
}

 

Share this comment


Link to comment

Your updated source code is identical to your previous code, like literally line for line, character for character.

Share this comment


Link to comment
8 hours ago, Rutin said:

https://www.diffchecker.com/EdxR7ubb

🙄

I had a bit of a chuckle.

I did the exact same thing.  I swear he is a long term troll of gamedev.  People keep trying to help him, like @8Observer8, but he will say he understands what they're saying then post code a short bit later with none of the changes.  For a good laugh I've scrolled through his post history before, man I wish I had made popcorn.

Share this comment


Link to comment

crazycdn I am sorry I posted identical code everbody makes mistakes, thanks for all the input

This is my real updated code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace snakegame
{
    class Program
    {
        static void Main(string[] args)
        {
            const int board_size = 17;
            char[,] board = new char[board_size, board_size];
            int horizontal = 5, vertical = 8, count = 0;

            for (int i = 0; i < board_size; i++) 
            {
                for (int j = 0; j < board_size; j++) 
                {
                    board[i, j] = '.';
                }
            }

            void draw_board()
            {
                for (int i = 0; i < board_size; i++)
                {
                    for (int j = 0; j < board_size; j++)
                    {
                        Console.Write(board[i, j]);
                    }
                    Console.WriteLine();
                }
            }

            while (count < 12)
            {
                if (count >= 0 && count < 6)
                {
                    board[vertical, horizontal] = '*';
                    board[vertical, horizontal + 1] = '*';
                    board[vertical, horizontal + 2] = '*';
                    board[vertical, horizontal + 3] = '*';
                    board[vertical, horizontal + 4] = '*';
                    board[vertical, horizontal + 5] = '*';
                    board[vertical, horizontal + 6] = '*';

                    draw_board();

                    horizontal++;
                }

                if (count > 5 && count < 12)
                {
                    board[vertical, horizontal] = '*';
                    board[vertical+1, horizontal] = '*';
                    board[vertical+2, horizontal] = '*';
                    board[vertical+3, horizontal] = '*';
                    board[vertical+4, horizontal] = '*';
                    board[vertical+5, horizontal] = '*';
                    board[vertical+6, horizontal] = '*';

                    draw_board();

                    vertical++;
                }

                count++;

                System.Threading.Thread.Sleep(1000);
                Console.Clear();
            }
        }
    }
}

 

Share this comment


Link to comment
2 hours ago, CrazyCdn said:

... I swear he is a long term troll of gamedev...

Or the most successful Turing test to date.

Share this comment


Link to comment
1 hour ago, unbird said:

Or the most successful Turing test to date.

I actually had thought this too...  lol

I think phil is my hero.  

Edited by Awoken

Share this comment


Link to comment

My opinion that it is more difficult to use Console for the Snake Game. It is more simple to use WinForms Graphics class or graphics from WPF. Try to improve my example 101. Snake. WinForms, GDI+ Add lives, "game over", scores, sounds, music and so on. I like irrKlang library for music/sounds because it is more simple that OpenAL. Work hard and you will see your progress. Motivate yourself and have fun.

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!