Jump to content
  • Advertisement
phil67rpg

c# console snake game

Recommended Posts

I have decided to work on a console based snake game. I am able to draw grid but the last grid I am  trying to draw one grid that animates the snake on it.

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)
        {
            char[,] board = new char[17, 17];
            int count = 5;
            for (int i = 0; i <= 16; i++) 
            {
                for (int j = 0; j <= 16; j++) 
                {
                    board[i, j] = '.';
                }
            }
            while (count < 11)
            {
                Console.Clear();
                board[8, count] = '*';
                board[8, count + 1] = '*';
                board[8, count + 2] = '*';
                board[8, count + 3] = '*';
                board[8, count + 4] = '*';
                board[8, count + 5] = '*';
                board[8, count + 6] = '*';

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

 

Share this post


Link to post
Share on other sites
Advertisement

Hi Phil,

Interesting you should choose to do this in a console app, but that's fine :)

I think create yourself a function called draw_board() and have this simply draw the state of the current board (array). I don't think clearing the console so often is a good idea. Just change the values of the board array, then call draw_board(). Proably using a c# collection class might be advisable.

Also, style-wise, a few suggestions (take it or leave it): 

- don't hard-code values like 17. Create a variable or constant for it, like board_size.

- Instead of looping <= 16 just use < 17. It's the most common convention.

- my preference is to use var where the type is obvious. Also in cases like

char [,] board

which can be hard to remember.

But those are minor things. Keep at it. Now you just need to work out your algorithm for calculating the position of the snake's body pieces.

P.S and make Main public.

Edited by duke_meister

Share this post


Link to post
Share on other sites

I have made some minor changes in my code, I will work on moving the snake.

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 count = 5;
            for (int i = 0; i < board_size; i++) 
            {
                for (int j = 0; j < board_size; j++) 
                {
                    board[i, j] = '.';
                }
            }
            while (count < 11)
            {
                board[8, count] = '*';
                board[8, count + 1] = '*';
                board[8, count + 2] = '*';
                board[8, count + 3] = '*';
                board[8, count + 4] = '*';
                board[8, count + 5] = '*';
                board[8, count + 6] = '*';

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

 

Share this post


Link to post
Share on other sites

Well, that's progress :)

Look at Console.SetCursorPosition(Int32, Int32)

You should be able to move the drawing position around so you can write a draw_board() function without having to clear the screen. I haven't used it before, but maybe look into it.

Taking your code:

                for (int i = 0; i < board_size; i++)
                {
                    for (int j = 0; j < board_size; j++)
                    {
			move cursor to i, j; // pseudocode
			// Look into WriteAt instead of Write
                        Console.Write(board[i, j]);
                    }
                }

draw_board() might be something like that. Perhaps use x and y so you know which way you're drawing the board.

Edited by duke_meister

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

  • 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!