Advertisement

c# console snake game

Started by February 09, 2019 11:06 PM
4 comments, last by phil67rpg 5 years, 7 months ago

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++;
            }
        }
    }
}

 

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.

Developer since 1994. Programming since '80. Still like it.

Advertisement

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++;
            }
        }
    }
}

 

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.

Developer since 1994. Programming since '80. Still like it.

I have worked on my program and posted it on my blog.

This topic is closed to new replies.

Advertisement