• entries
19
164
• views
3988

# c# console snake game

3666 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;

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

Console.Clear();
}
}
}
}

Posted (edited)

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

using System;

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

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

here is my updated code

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

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

Console.Clear();
}
}
}
}

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

4 hours ago, CrazyCdn said:

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

🙄

I had a bit of a chuckle.

Please, when you publish your code select this option in right-bottom corner of forum code editor:

8 hours ago, Rutin said:

🙄

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.

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;

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

Console.Clear();
}
}
}
}

2 hours ago, CrazyCdn said:

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

Or the most successful Turing test to date.

Posted (edited)

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

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.

well I have decided to take observers advice and work on a winform gdi+ snake game. I am able to make the snake head to move to the  right in the screen. here is my code so far, I am just starting on my project.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void Form1_Paint(object sender, PaintEventArgs e)
{

}

int x = 0, y = 0;

private void timer1_Tick(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
SolidBrush greenBrush = new SolidBrush(Color.Green);
SolidBrush blackBrush = new SolidBrush(Color.Black);
Rectangle rect_green = new Rectangle(340 + x, 280 + y, 10, 10);
Rectangle rect_black = new Rectangle(330 + x, 280 + y, 10, 10);
g.FillRectangle(greenBrush, rect_green);
g.FillRectangle(blackBrush, rect_black);
if (x >= 340)
{
x = 340;
}
x += 5;
greenBrush.Dispose();
g.Dispose();

if (MouseButtons == MouseButtons.Left)
{

}
}

private void Form1_MouseDown(object sender, MouseEventArgs e)
{

}
}
}

Posted (edited)

I don't think there's a problem using the console. I think it's an interesting challenge. Plus also haven't you already written the GDI one?

ed: ok after a quick look, it doesn't seem that much fun. It's just too slow without some advanced console IO.

Edited by duke_meister

Posted (edited)

8 hours ago, phil67rpg said:

I am able to make the snake head to move to the  right in the screen. here is my code so far, I am just starting on my project.

I ran your project but I do not see the snake head which moves to the right.

I see three problems in your code:

1. You need to call drawing from the Form1_Paint() method
2. You do not call the Invalidate() method in the timer1_Tick() method
3. You have too big offset for the snake head:
Rectangle rect_green = new Rectangle(340 + x, 280 + y, 10, 10);
Rectangle rect_black = new Rectangle(330 + x, 280 + y, 10, 10);

Default size of windows is 300x300.

I solved these problems:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace SnakeByPhil_WinFormsGDI
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
CenterToScreen();
}

int x = 0, y = 0;

private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = this.CreateGraphics();
SolidBrush greenBrush = new SolidBrush(Color.Green);
SolidBrush blackBrush = new SolidBrush(Color.Black);
Rectangle rect_green = new Rectangle(20 + x, 20 + y, 10, 10);
Rectangle rect_black = new Rectangle(10 + x, 20 + y, 10, 10);
g.FillRectangle(greenBrush, rect_green);
g.FillRectangle(blackBrush, rect_black);
greenBrush.Dispose();
g.Dispose();
}

private void timer1_Tick(object sender, EventArgs e)
{
if (x >= 340)
{
x = 340;
}
x += 5;

if (MouseButtons == MouseButtons.Left)
{

}
Invalidate();
}
}
}

Edited by 8Observer8

well duke thanks for the code but I cant learn by copying someone else code. let me work on what observer  has sent me. I can only digest so much code at one time.

Posted (edited)

You said you'd changed your mind from doing console, so I went for it

Take a piece and analyse it. I'm happy to go over each aspect one at a time. Want me to do that? If so I'll add comments in the code.

Much of it is relevant to your GDI project.

Edited by duke_meister

Posted (edited)

thanks duke I am doing the graphics c# because observer said it is easier. I am able to get the snake to move up and down and left and right but I want it to move smoother animation.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = this.CreateGraphics();
SolidBrush greenBrush = new SolidBrush(Color.Green);
SolidBrush blackBrush = new SolidBrush(Color.Black);
Rectangle rect_green = new Rectangle(340 + x, 280 + y, 10, 10);
Rectangle rect_black = new Rectangle(330 + x, 280 + y, 10, 10);
g.FillRectangle(greenBrush, rect_green);
g.FillRectangle(blackBrush, rect_black);
greenBrush.Dispose();
g.Dispose();
}

int x = 0, y = 0;

Random rnd = new Random();

private void timer1_Tick(object sender, EventArgs e)
{
int move = rnd.Next(1, 5);

switch (move)
{
case 1:
for (int i = 0; i <= 20; i++)
{
x++;
}
break;
case 2:
for (int i = 0; i <= 20; i++)
{
x--;
}
break;
case 3:
for (int i = 0; i <= 20; i++)
{
y++;
}
break;
case 4:
for (int i = 0; i <= 20; i++)
{
y--;
}
break;
}

if (MouseButtons == MouseButtons.Left)
{

}
Invalidate();
}

private void Form1_MouseDown(object sender, MouseEventArgs e)
{

}
}
}

Edited by phil67rpg

No problem. I've created a blog for it, so as not to confuse yours. See some updated code there.

As for 'easier'.... that's a subjective concept

well I am making a graphics c# program. can you give me just a hint as how to make the snake smooth animation.

Posted (edited)

8 hours ago, phil67rpg said:

well I am making a graphics c# program. can you give me just a hint as how to make the snake smooth animation.

There's not enough there yet to comment on.

Idea: have a look at the structure of the game on my blog. Maybe an exercise would be to take that structure and turn it into a GDI version. I could help with that.

Or if you're interested in learning gradually, I could start another blog and build the game up step by step?

By the way, a GDI snake game is more difficult in my opinion.

Anyway. Draw on a Panel. Make sure double buffering is set on the form/panel.

BTW My blog is

Edited by duke_meister

Posted (edited)

2 hours ago, duke_meister said:

By the way, a GDI snake game is more difficult in my opinion.

I think "more difficult"... that's a subjective concept. For me making a ASCII snake game is more difficult than a snake game using: GDI/WinForms, OpenGL/OpenTK, WPF or Unity.

14 hours ago, phil67rpg said:

but I want it to move smoother animation.

I will add the smooth animation later. I think how to make it simpler. The classic game game does not have the smooth animation. But it is very interesting.

Edited by 8Observer8

I added timer2 which sets a new random direction every 500 milliseconds:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace SnakeByPhil_WinFormsGDI
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
CenterToScreen();

timer1.Interval = 200;

timer2.Interval = 500;
timer2.Start();
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = this.CreateGraphics();
SolidBrush greenBrush = new SolidBrush(Color.Green);
SolidBrush blackBrush = new SolidBrush(Color.Black);
//Rectangle rect_green = new Rectangle(340 + x, 280 + y, 10, 10);
//Rectangle rect_black = new Rectangle(330 + x, 280 + y, 10, 10);
Rectangle rect_green = new Rectangle(150 + x, 150 + y, 10, 10);
Rectangle rect_black = new Rectangle(140 + x, 150 + y, 10, 10);
g.FillRectangle(greenBrush, rect_green);
g.FillRectangle(blackBrush, rect_black);
greenBrush.Dispose();
g.Dispose();
}

int x = 0, y = 0;

Random rnd = new Random();

int move = 1;

private void timer2_Tick(object sender, EventArgs e)
{
move = rnd.Next(1, 5);
Console.WriteLine(move);
}

private void timer1_Tick(object sender, EventArgs e)
{
switch (move)
{
case 1:
for (int i = 0; i <= 20; i++)
{
x++;
}
break;
case 2:
for (int i = 0; i <= 20; i++)
{
x--;
}
break;
case 3:
for (int i = 0; i <= 20; i++)
{
y++;
}
break;
case 4:
for (int i = 0; i <= 20; i++)
{
y--;
}
break;
}

if (MouseButtons == MouseButtons.Left)
{

}
Invalidate();
}
}
}

Posted (edited)

@phil67rpg do you have a plan to start learning of WPF instead of WinForms in the future? WinForms uses GDI (CPU - Central Processor Unit on central processor for rendering graphics) and it is very slowly than WPF because WPF use a graphics card on the hood (GPU - Graphics Processor Unit on graphics card for rendering graphics).

Do not afraid XAML. You can create graphics from C# code like in WinForms. XAML you can study later. XAML is like HTML. It is not difficult and it is more confiniant than WinForms. I think it is better do not spend time with WinForms. It is ideal to embed OpenTK.GLControl to use OpenGL 3 in you WinForms or WPF application. Or you can create GameWindow from Console Project using OpenTK. Studying WPF Graphics is good to because we have a great book about computer graphics that use WPF C#:

Edited by 8Observer8

I want to finish my project before I pursue WPF. I made some changes to my code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
CenterToScreen();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = this.CreateGraphics();
SolidBrush greenBrush = new SolidBrush(Color.Green);
SolidBrush blackBrush = new SolidBrush(Color.Black);
Rectangle rect_green = new Rectangle(340 + x, 280 + y, 10, 10);
Rectangle rect_black = new Rectangle(330 + x, 280 + y, 10, 10);
g.FillRectangle(greenBrush, rect_green);
g.FillRectangle(blackBrush, rect_black);
greenBrush.Dispose();
g.Dispose();
}

int x = 0, y = 0;

Random rnd = new Random();

int move = 1;

private void timer2_Tick(object sender, EventArgs e)
{
move = rnd.Next(1, 5);
}

private void timer1_Tick(object sender, EventArgs e)
{
switch (move)
{
case 1:
for (int i = 0; i <= 20; i++)
{
if (x >= 440)
{
x = 0;
}
x++;
}
break;
case 2:
for (int i = 0; i <= 20; i++)
{
if (x <= -420)
{
x = 0;
}
x--;
}
break;
case 3:
for (int i = 0; i <= 20; i++)
{
if (y >= 280)
{
y = 0;
}
y++;
}
break;
case 4:
for (int i = 0; i <= 20; i++)
{
if (y <= -280)
{
y = 0;
}
y--;
}
break;
}

if (MouseButtons == MouseButtons.Left)
{

}
Invalidate();
}

private void Form1_MouseDown(object sender, MouseEventArgs e)
{

}
}
}

12 hours ago, 8Observer8 said:
14 hours ago, duke_meister said:

By the way, a GDI snake game is more difficult in my opinion.

I think "more﻿﻿﻿﻿﻿ difficult"... that's﻿ a subjective ﻿conce﻿p﻿t﻿﻿

Yes that's what I said in a previous post, so we agree.

I want to draw the food on the screen but what I get is the food is drawn randomly all over the screen.

        private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = this.CreateGraphics();
int food_x = rnd.Next(0, 770);
int food_y = rnd.Next(0, 550);
SolidBrush greenBrush = new SolidBrush(Color.Green);
SolidBrush blackBrush = new SolidBrush(Color.Black);
SolidBrush redBrush = new SolidBrush(Color.Red);
Rectangle rect_green = new Rectangle(340 + x, 280 + y, 10, 10);
Rectangle rect_black = new Rectangle(330 + x, 280 + y, 10, 10);
Rectangle rect_red = new Rectangle(food_x , food_y , 10, 10);
g.FillRectangle(greenBrush, rect_green);
g.FillRectangle(blackBrush, rect_black);
g.FillRectangle(redBrush, rect_red);
redBrush.Dispose();
blackBrush.Dispose();
greenBrush.Dispose();
g.Dispose();
}


## Create an account

Register a new account