using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
///█ ■
////https://www.youtube.com/watch?v=SGZgvMwjq2U
namespace Snake
{
class Program
{
static void Main(string[] args)
{
Console.WindowHeight = 16;
Console.WindowWidth = 32;
int screenwidth = Console.WindowWidth;
int screenheight = Console.WindowHeight;
Random randomnummer = new Random();
int score = 5;
int gameover = 0;
pixel hoofd = new pixel();
hoofd.xpos = screenwidth / 2;
hoofd.ypos = screenheight / 2;
hoofd.schermkleur = ConsoleColor.Red;
string movement = "RIGHT";
List<int> xposlijf = new List<int>();
List<int> yposlijf = new List<int>();
int berryx = randomnummer.Next(0, screenwidth);
int berryy = randomnummer.Next(0, screenheight);
DateTime tijd = DateTime.Now;
DateTime tijd2 = DateTime.Now;
string buttonpressed = "no";
// We only draw the border once. It doesn't change.
DrawBorder(screenwidth, screenheight);
while (true)
{
ClearConsoleDrawBorder(screenwidth, screenheight);
if (hoofd.xpos == screenwidth - 1 || hoofd.xpos == 0 || hoofd.ypos == screenheight - 1 || hoofd.ypos == 0)
{
gameover = 1;
}
Console.ForegroundColor = ConsoleColor.Green;
if (berryx == hoofd.xpos && berryy == hoofd.ypos)
{
score++;
berryx = randomnummer.Next(1, screenwidth - 2);
berryy = randomnummer.Next(1, screenheight - 2);
}
for (int i = 0; i <while xposlijf.Count(); i++true)
{
Console.SetCursorPositionClearConsole(xposlijf[i]screenwidth, yposlijf[i]screenheight);
Console.Write("■");
if (hoofd.xpos == screenwidth - 1 || hoofd.xpos == 0 || hoofd.ypos == screenheight if- (xposlijf[i]1 ==|| hoofd.xpos && yposlijf[i]ypos == hoofd.ypos0)
{
gameover = 1;
}
}
if (gameover == 1)
{
break;
}
Console.SetCursorPosition(hoofd.xpos, hoofd.ypos);
Console.ForegroundColor = hoofd.schermkleur;
Console.Write("■");
Console.SetCursorPosition(berryx, berryy);
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write("■");
Console.CursorVisible = false;
tijd = DateTime.Now;
buttonpressed = "no";
while (true)
{
tijd2 = DateTime.Now;Green;
if (tijd2.Subtract(tijd).TotalMilliseconds > 500) { break; }
berryx == hoofd.xpos && berryy if== (Consolehoofd.KeyAvailableypos)
{
ConsoleKeyInfo toets = Console.ReadKey(true);score++;
//Console.WriteLine(toets.Key.ToString());
berryx if= (toets.Keyrandomnummer.EqualsNext(ConsoleKey.UpArrow) && movement != "DOWN" &&1, buttonpressedscreenwidth ==- "no"2)
{;
movementberryy = "UP";
randomnummer.Next(1, buttonpressedscreenheight =- "yes";2);
}
iffor (toets.Key.Equals(ConsoleKey.DownArrow) &&int movementi != "UP"0; &&i buttonpressed< ==xposlijf.Count(); "no"i++)
{
movement = "DOWN";
buttonpressed =Console.SetCursorPosition(xposlijf[i], "yes";yposlijf[i]);
}Console.Write("¦");
if (toets.Key.Equals(ConsoleKey.LeftArrow) && movementxposlijf[i] !=== "RIGHT"hoofd.xpos && buttonpressedyposlijf[i] == "no"hoofd.ypos)
{
movement = "LEFT";
buttonpressedgameover = "yes";1;
}
}
if (toetsgameover == 1)
{
break;
}
Console.KeySetCursorPosition(hoofd.Equalsxpos, hoofd.ypos);
Console.ForegroundColor = hoofd.schermkleur;
Console.Write(ConsoleKey"■");
Console.RightArrowSetCursorPosition(berryx, berryy);
&& movement ! Console.ForegroundColor = "LEFT"ConsoleColor.Cyan;
&& Console.Write("■");
Console.CursorVisible = false;
tijd = DateTime.Now;
buttonpressed === "no""no";
while (true)
{
tijd2 = DateTime.Now;
if (tijd2.Subtract(tijd).TotalMilliseconds > 500) { break; }
if (Console.KeyAvailable)
{
ConsoleKeyInfo toets = Console.ReadKey(true);
//Console.WriteLine(toets.Key.ToString());
if (toets.Key.Equals(ConsoleKey.UpArrow) && movement != "DOWN" && buttonpressed == "no")
{
movement = "UP";
buttonpressed = "yes";
}
if (toets.Key.Equals(ConsoleKey.DownArrow) && movement != "UP" && buttonpressed == "no")
{
movement = "DOWN";
buttonpressed = "yes";
}
if (toets.Key.Equals(ConsoleKey.LeftArrow) && movement != "RIGHT" && buttonpressed == "no")
{
movement = "LEFT";
buttonpressed = "yes";
}
if (toets.Key.Equals(ConsoleKey.RightArrow) && movement != "LEFT" && buttonpressed == "no")
{
movement = "RIGHT";
buttonpressed = "yes";
}
}
}
}
xposlijf.Add(hoofd.xpos);
yposlijf.Add(hoofd.ypos);
switch (movement)
{
case "UP":
hoofd.ypos--;
break;
case "DOWN":
hoofd.ypos++;
break;
case "LEFT":
hoofd.xpos--;
break;
case "RIGHT":
hoofd.xpos++;
break;
}
if (xposlijf.Count() > score)
{
xposlijf.RemoveAt(0);
yposlijf.RemoveAt(0);
}
}
ifConsole.SetCursorPosition(screenwidth / 5, screenheight / 2);
Console.WriteLine(xposlijf"Game over, Score: " + score);
Console.CountSetCursorPosition(screenwidth / 5, screenheight / 2 + 1);
> score }
private static void ClearConsole(int screenwidth, int screenheight)
{
var blackLine = string.Join("", new byte[screenwidth - 2].Select(b => " ").ToArray());
Console.ForegroundColor = ConsoleColor.Black;
for (int i = 1; i < screenheight - 1; i++)
{
xposlijfConsole.RemoveAtSetCursorPosition(01, i);
yposlijfConsole.RemoveAtWrite(0blackLine);
}
}
Console.SetCursorPosition(screenwidth / 5, screenheight / 2);
Console.WriteLine("Game over, Score: " + score);
Console.SetCursorPosition(screenwidth / 5, screenheight / 2 + 1);
}
private static void ClearConsoleDrawBorder(int screenwidth, int screenheight)
{
var blackLine = string.Join("", new byte[screenwidth - 2].Select(b => " ").ToArray());
Console.ForegroundColor = ConsoleColor.Black;
for (int i = 1; i < screenheight - 1; i++)
{
Console.SetCursorPosition(1, i);
var horizontalBar = string.Join("", new byte[screenwidth].Select(b => Console"■").WriteToArray(blackLine));
}
}
private static void DrawBorder(int screenwidth, int screenheight Console.SetCursorPosition(0, 0);
{ Console.Write(horizontalBar);
var horizontalBar = string Console.JoinSetCursorPosition(""0, new byte[screenwidth].Select(bscreenheight =>- "■"1);
Console.ToArrayWrite()horizontalBar);
for (int i = 0; i < screenheight; i++)
{
Console.SetCursorPosition(0, 0i);
Console.Write(horizontalBar"■");
Console.SetCursorPosition(0, screenheightscreenwidth - 1, i);
Console.Write(horizontalBar"■");
}
}
for (int i = 0; i < screenheight;class i++)pixel
{
Console.SetCursorPosition(0,public i);
int xpos { get; set; }
Console.Write("■");
public int ypos { get; set; }
Console.SetCursorPosition(screenwidth - 1, i);
public ConsoleColor schermkleur { get; set; Console.Write("■");}
}
}
class pixel
{
public int xpos { get; set; }
public int ypos { get; set; }
public ConsoleColor schermkleur { get; set; }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
///█ ■
////https://www.youtube.com/watch?v=SGZgvMwjq2U
namespace Snake
{
class Program
{
static void Main(string[] args)
{
Console.WindowHeight = 16;
Console.WindowWidth = 32;
int screenwidth = Console.WindowWidth;
int screenheight = Console.WindowHeight;
Random randomnummer = new Random();
int score = 5;
int gameover = 0;
pixel hoofd = new pixel();
hoofd.xpos = screenwidth / 2;
hoofd.ypos = screenheight / 2;
hoofd.schermkleur = ConsoleColor.Red;
string movement = "RIGHT";
List<int> xposlijf = new List<int>();
List<int> yposlijf = new List<int>();
int berryx = randomnummer.Next(0, screenwidth);
int berryy = randomnummer.Next(0, screenheight);
DateTime tijd = DateTime.Now;
DateTime tijd2 = DateTime.Now;
string buttonpressed = "no";
// We only draw the border once. It doesn't change.
DrawBorder(screenwidth, screenheight);
while (true)
{
ClearConsole(screenwidth, screenheight);
if (hoofd.xpos == screenwidth - 1 || hoofd.xpos == 0 || hoofd.ypos == screenheight - 1 || hoofd.ypos == 0)
{
gameover = 1;
}
Console.ForegroundColor = ConsoleColor.Green;
if (berryx == hoofd.xpos && berryy == hoofd.ypos)
{
score++;
berryx = randomnummer.Next(1, screenwidth - 2);
berryy = randomnummer.Next(1, screenheight - 2);
}
for (int i = 0; i < xposlijf.Count(); i++)
{
Console.SetCursorPosition(xposlijf[i], yposlijf[i]);
Console.Write("■");
if (xposlijf[i] == hoofd.xpos && yposlijf[i] == hoofd.ypos)
{
gameover = 1;
}
}
if (gameover == 1)
{
break;
}
Console.SetCursorPosition(hoofd.xpos, hoofd.ypos);
Console.ForegroundColor = hoofd.schermkleur;
Console.Write("■");
Console.SetCursorPosition(berryx, berryy);
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write("■");
Console.CursorVisible = false;
tijd = DateTime.Now;
buttonpressed = "no";
while (true)
{
tijd2 = DateTime.Now;
if (tijd2.Subtract(tijd).TotalMilliseconds > 500) { break; }
if (Console.KeyAvailable)
{
ConsoleKeyInfo toets = Console.ReadKey(true);
//Console.WriteLine(toets.Key.ToString());
if (toets.Key.Equals(ConsoleKey.UpArrow) && movement != "DOWN" && buttonpressed == "no")
{
movement = "UP";
buttonpressed = "yes";
}
if (toets.Key.Equals(ConsoleKey.DownArrow) && movement != "UP" && buttonpressed == "no")
{
movement = "DOWN";
buttonpressed = "yes";
}
if (toets.Key.Equals(ConsoleKey.LeftArrow) && movement != "RIGHT" && buttonpressed == "no")
{
movement = "LEFT";
buttonpressed = "yes";
}
if (toets.Key.Equals(ConsoleKey.RightArrow) && movement != "LEFT" && buttonpressed == "no")
{
movement = "RIGHT";
buttonpressed = "yes";
}
}
}
xposlijf.Add(hoofd.xpos);
yposlijf.Add(hoofd.ypos);
switch (movement)
{
case "UP":
hoofd.ypos--;
break;
case "DOWN":
hoofd.ypos++;
break;
case "LEFT":
hoofd.xpos--;
break;
case "RIGHT":
hoofd.xpos++;
break;
}
if (xposlijf.Count() > score)
{
xposlijf.RemoveAt(0);
yposlijf.RemoveAt(0);
}
}
Console.SetCursorPosition(screenwidth / 5, screenheight / 2);
Console.WriteLine("Game over, Score: " + score);
Console.SetCursorPosition(screenwidth / 5, screenheight / 2 + 1);
}
private static void ClearConsole(int screenwidth, int screenheight)
{
var blackLine = string.Join("", new byte[screenwidth - 2].Select(b => " ").ToArray());
Console.ForegroundColor = ConsoleColor.Black;
for (int i = 1; i < screenheight - 1; i++)
{
Console.SetCursorPosition(1, i);
Console.Write(blackLine);
}
}
private static void DrawBorder(int screenwidth, int screenheight)
{
var horizontalBar = string.Join("", new byte[screenwidth].Select(b => "■").ToArray());
Console.SetCursorPosition(0, 0);
Console.Write(horizontalBar);
Console.SetCursorPosition(0, screenheight - 1);
Console.Write(horizontalBar);
for (int i = 0; i < screenheight; i++)
{
Console.SetCursorPosition(0, i);
Console.Write("■");
Console.SetCursorPosition(screenwidth - 1, i);
Console.Write("■");
}
}
class pixel
{
public int xpos { get; set; }
public int ypos { get; set; }
public ConsoleColor schermkleur { get; set; }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
///█ ■
////https://www.youtube.com/watch?v=SGZgvMwjq2U
namespace Snake
{
class Program
{
static void Main(string[] args)
{
Console.WindowHeight = 16;
Console.WindowWidth = 32;
int screenwidth = Console.WindowWidth;
int screenheight = Console.WindowHeight;
Random randomnummer = new Random();
int score = 5;
int gameover = 0;
pixel hoofd = new pixel();
hoofd.xpos = screenwidth / 2;
hoofd.ypos = screenheight / 2;
hoofd.schermkleur = ConsoleColor.Red;
string movement = "RIGHT";
List<int> xposlijf = new List<int>();
List<int> yposlijf = new List<int>();
int berryx = randomnummer.Next(0, screenwidth);
int berryy = randomnummer.Next(0, screenheight);
DateTime tijd = DateTime.Now;
DateTime tijd2 = DateTime.Now;
string buttonpressed = "no";
// We only draw the border once. It doesn't change.
DrawBorder(screenwidth, screenheight);
while (true)
{
ClearConsole(screenwidth, screenheight);
if (hoofd.xpos == screenwidth - 1 || hoofd.xpos == 0 || hoofd.ypos == screenheight - 1 || hoofd.ypos == 0)
{
gameover = 1;
}
Console.ForegroundColor = ConsoleColor.Green;
if (berryx == hoofd.xpos && berryy == hoofd.ypos)
{
score++;
berryx = randomnummer.Next(1, screenwidth - 2);
berryy = randomnummer.Next(1, screenheight - 2);
}
for (int i = 0; i < xposlijf.Count(); i++)
{
Console.SetCursorPosition(xposlijf[i], yposlijf[i]);
Console.Write("¦");
if (xposlijf[i] == hoofd.xpos && yposlijf[i] == hoofd.ypos)
{
gameover = 1;
}
}
if (gameover == 1)
{
break;
}
Console.SetCursorPosition(hoofd.xpos, hoofd.ypos);
Console.ForegroundColor = hoofd.schermkleur;
Console.Write("■");
Console.SetCursorPosition(berryx, berryy);
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write("■");
Console.CursorVisible = false;
tijd = DateTime.Now;
buttonpressed = "no";
while (true)
{
tijd2 = DateTime.Now;
if (tijd2.Subtract(tijd).TotalMilliseconds > 500) { break; }
if (Console.KeyAvailable)
{
ConsoleKeyInfo toets = Console.ReadKey(true);
//Console.WriteLine(toets.Key.ToString());
if (toets.Key.Equals(ConsoleKey.UpArrow) && movement != "DOWN" && buttonpressed == "no")
{
movement = "UP";
buttonpressed = "yes";
}
if (toets.Key.Equals(ConsoleKey.DownArrow) && movement != "UP" && buttonpressed == "no")
{
movement = "DOWN";
buttonpressed = "yes";
}
if (toets.Key.Equals(ConsoleKey.LeftArrow) && movement != "RIGHT" && buttonpressed == "no")
{
movement = "LEFT";
buttonpressed = "yes";
}
if (toets.Key.Equals(ConsoleKey.RightArrow) && movement != "LEFT" && buttonpressed == "no")
{
movement = "RIGHT";
buttonpressed = "yes";
}
}
}
xposlijf.Add(hoofd.xpos);
yposlijf.Add(hoofd.ypos);
switch (movement)
{
case "UP":
hoofd.ypos--;
break;
case "DOWN":
hoofd.ypos++;
break;
case "LEFT":
hoofd.xpos--;
break;
case "RIGHT":
hoofd.xpos++;
break;
}
if (xposlijf.Count() > score)
{
xposlijf.RemoveAt(0);
yposlijf.RemoveAt(0);
}
}
Console.SetCursorPosition(screenwidth / 5, screenheight / 2);
Console.WriteLine("Game over, Score: " + score);
Console.SetCursorPosition(screenwidth / 5, screenheight / 2 + 1);
}
private static void ClearConsole(int screenwidth, int screenheight)
{
var blackLine = string.Join("", new byte[screenwidth - 2].Select(b => " ").ToArray());
Console.ForegroundColor = ConsoleColor.Black;
for (int i = 1; i < screenheight - 1; i++)
{
Console.SetCursorPosition(1, i);
Console.Write(blackLine);
}
}
private static void DrawBorder(int screenwidth, int screenheight)
{
var horizontalBar = string.Join("", new byte[screenwidth].Select(b => "■").ToArray());
Console.SetCursorPosition(0, 0);
Console.Write(horizontalBar);
Console.SetCursorPosition(0, screenheight - 1);
Console.Write(horizontalBar);
for (int i = 0; i < screenheight; i++)
{
Console.SetCursorPosition(0, i);
Console.Write("■");
Console.SetCursorPosition(screenwidth - 1, i);
Console.Write("■");
}
}
class pixel
{
public int xpos { get; set; }
public int ypos { get; set; }
public ConsoleColor schermkleur { get; set; }
}
}
}
First up:
Good job on this game. Your logic is good. Yes, there are some improvements in the code and so on, and all of the above comments are spot on, so I'm not going to hammer on the same points. First up:
Simple Enhancements:
When Good job on this game. Your logic is good. Yes, there are some improvements in the code and so on, and all of the above comments are spot on, so I'm not going to hammer on the same points.
Simple Enhancements:
When I ran the game I found it a bit laggy. The constant
Console.Clear();
operations made the screen flicker quite a bit. Also, the drawing of the border was a bit long winded. For example, you are on a row at 0, 0... You can write the entire horizontal bar in one go with oneConsole.Write("■■■■■■■■■■■■■■");
statement. The same applies for the bottom row of the border.Console.Clear();
operations made the screen flicker quite a bit. Also, the drawing of the border was a bit long winded. For example, you are on a row at 0, 0... You can write the entire horizontal bar in one go with one Console.Write("■■■■■■■■■■■■■■");
statement. The same applies for the bottom row of the border.Console.Clear();
we can just create a method namedClearConsole();
that writes black over the inside of the frame.Console.Clear();
we can just create a method named ClearConsole();
that writes black over the inside of the frame.using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
///█ ■
////https://www.youtube.com/watch?v=SGZgvMwjq2U
namespace Snake
{
class Program
{
static void Main(string[] args)
{
Console.WindowHeight = 16;
Console.WindowWidth = 32;
int screenwidth = Console.WindowWidth;
int screenheight = Console.WindowHeight;
Random randomnummer = new Random();
int score = 5;
int gameover = 0;
pixel hoofd = new pixel();
hoofd.xpos = screenwidth / 2;
hoofd.ypos = screenheight / 2;
hoofd.schermkleur = ConsoleColor.Red;
string movement = "RIGHT";
List<int> xposlijf = new List<int>();
List<int> yposlijf = new List<int>();
int berryx = randomnummer.Next(0, screenwidth);
int berryy = randomnummer.Next(0, screenheight);
DateTime tijd = DateTime.Now;
DateTime tijd2 = DateTime.Now;
string buttonpressed = "no";
// We only draw the border once. It doesn't change.
DrawBorder(screenwidth, screenheight);
while (true)
{
ClearConsole(screenwidth, screenheight);
if (hoofd.xpos == screenwidth - 1 || hoofd.xpos == 0 || hoofd.ypos == screenheight - 1 || hoofd.ypos == 0)
{
gameover = 1;
}
Console.ForegroundColor = ConsoleColor.Green;
if (berryx == hoofd.xpos && berryy == hoofd.ypos)
{
score++;
berryx = randomnummer.Next(1, screenwidth - 2);
berryy = randomnummer.Next(1, screenheight - 2);
}
for (int i = 0; i < xposlijf.Count(); i++)
{
Console.SetCursorPosition(xposlijf[i], yposlijf[i]);
Console.Write("■");
if (xposlijf[i] == hoofd.xpos && yposlijf[i] == hoofd.ypos)
{
gameover = 1;
}
}
if (gameover == 1)
{
break;
}
Console.SetCursorPosition(hoofd.xpos, hoofd.ypos);
Console.ForegroundColor = hoofd.schermkleur;
Console.Write("■");
Console.SetCursorPosition(berryx, berryy);
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write("■");
Console.CursorVisible = false;
tijd = DateTime.Now;
buttonpressed = "no";
while (true)
{
tijd2 = DateTime.Now;
if (tijd2.Subtract(tijd).TotalMilliseconds > 500) { break; }
if (Console.KeyAvailable)
{
ConsoleKeyInfo toets = Console.ReadKey(true);
//Console.WriteLine(toets.Key.ToString());
if (toets.Key.Equals(ConsoleKey.UpArrow) && movement != "DOWN" && buttonpressed == "no")
{
movement = "UP";
buttonpressed = "yes";
}
if (toets.Key.Equals(ConsoleKey.DownArrow) && movement != "UP" && buttonpressed == "no")
{
movement = "DOWN";
buttonpressed = "yes";
}
if (toets.Key.Equals(ConsoleKey.LeftArrow) && movement != "RIGHT" && buttonpressed == "no")
{
movement = "LEFT";
buttonpressed = "yes";
}
if (toets.Key.Equals(ConsoleKey.RightArrow) && movement != "LEFT" && buttonpressed == "no")
{
movement = "RIGHT";
buttonpressed = "yes";
}
}
}
xposlijf.Add(hoofd.xpos);
yposlijf.Add(hoofd.ypos);
switch (movement)
{
case "UP":
hoofd.ypos--;
break;
case "DOWN":
hoofd.ypos++;
break;
case "LEFT":
hoofd.xpos--;
break;
case "RIGHT":
hoofd.xpos++;
break;
}
if (xposlijf.Count() > score)
{
xposlijf.RemoveAt(0);
yposlijf.RemoveAt(0);
}
}
Console.SetCursorPosition(screenwidth / 5, screenheight / 2);
Console.WriteLine("Game over, Score: " + score);
Console.SetCursorPosition(screenwidth / 5, screenheight / 2 + 1);
}
private static void ClearConsole(int screenwidth, int screenheight)
{
var blackLine = string.Join("", new byte[screenwidth - 2].Select(b => " ").ToArray());
Console.ForegroundColor = ConsoleColor.Black;
for (int i = 1; i < screenheight - 1; i++)
{
Console.SetCursorPosition(1, i);
Console.Write(blackLine);
}
}
private static void DrawBorder(int screenwidth, int screenheight)
{
var horizontalBar = string.Join("", new byte[screenwidth].Select(b => "■").ToArray());
Console.SetCursorPosition(0, 0);
Console.Write(horizontalBar);
Console.SetCursorPosition(0, screenheight - 1);
Console.Write(horizontalBar);
for (int i = 0; i < screenheight; i++)
{
Console.SetCursorPosition(0, i);
Console.Write("■");
Console.SetCursorPosition(screenwidth - 1, i);
Console.Write("■");
}
}
class pixel
{
public int xpos { get; set; }
public int ypos { get; set; }
public ConsoleColor schermkleur { get; set; }
}
}
}
}
First up:
Good job on this game. Your logic is good. Yes, there are some improvements in the code and so on, and all of the above comments are spot on, so I'm not going to hammer on the same points.
Simple Enhancements:
When I ran the game I found it a bit laggy. The constant
Console.Clear();
operations made the screen flicker quite a bit. Also, the drawing of the border was a bit long winded. For example, you are on a row at 0, 0... You can write the entire horizontal bar in one go with oneConsole.Write("■■■■■■■■■■■■■■");
statement. The same applies for the bottom row of the border.Console.Clear();
we can just create a method namedClearConsole();
that writes black over the inside of the frame.using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
///█ ■
////https://www.youtube.com/watch?v=SGZgvMwjq2U
namespace Snake
{
class Program
{
static void Main(string[] args)
{
Console.WindowHeight = 16;
Console.WindowWidth = 32;
int screenwidth = Console.WindowWidth;
int screenheight = Console.WindowHeight;
Random randomnummer = new Random();
int score = 5;
int gameover = 0;
pixel hoofd = new pixel();
hoofd.xpos = screenwidth / 2;
hoofd.ypos = screenheight / 2;
hoofd.schermkleur = ConsoleColor.Red;
string movement = "RIGHT";
List<int> xposlijf = new List<int>();
List<int> yposlijf = new List<int>();
int berryx = randomnummer.Next(0, screenwidth);
int berryy = randomnummer.Next(0, screenheight);
DateTime tijd = DateTime.Now;
DateTime tijd2 = DateTime.Now;
string buttonpressed = "no";
// We only draw the border once. It doesn't change.
DrawBorder(screenwidth, screenheight);
while (true)
{
ClearConsole(screenwidth, screenheight);
if (hoofd.xpos == screenwidth - 1 || hoofd.xpos == 0 || hoofd.ypos == screenheight - 1 || hoofd.ypos == 0)
{
gameover = 1;
}
Console.ForegroundColor = ConsoleColor.Green;
if (berryx == hoofd.xpos && berryy == hoofd.ypos)
{
score++;
berryx = randomnummer.Next(1, screenwidth - 2);
berryy = randomnummer.Next(1, screenheight - 2);
}
for (int i = 0; i < xposlijf.Count(); i++)
{
Console.SetCursorPosition(xposlijf[i], yposlijf[i]);
Console.Write("■");
if (xposlijf[i] == hoofd.xpos && yposlijf[i] == hoofd.ypos)
{
gameover = 1;
}
}
if (gameover == 1)
{
break;
}
Console.SetCursorPosition(hoofd.xpos, hoofd.ypos);
Console.ForegroundColor = hoofd.schermkleur;
Console.Write("■");
Console.SetCursorPosition(berryx, berryy);
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write("■");
Console.CursorVisible = false;
tijd = DateTime.Now;
buttonpressed = "no";
while (true)
{
tijd2 = DateTime.Now;
if (tijd2.Subtract(tijd).TotalMilliseconds > 500) { break; }
if (Console.KeyAvailable)
{
ConsoleKeyInfo toets = Console.ReadKey(true);
//Console.WriteLine(toets.Key.ToString());
if (toets.Key.Equals(ConsoleKey.UpArrow) && movement != "DOWN" && buttonpressed == "no")
{
movement = "UP";
buttonpressed = "yes";
}
if (toets.Key.Equals(ConsoleKey.DownArrow) && movement != "UP" && buttonpressed == "no")
{
movement = "DOWN";
buttonpressed = "yes";
}
if (toets.Key.Equals(ConsoleKey.LeftArrow) && movement != "RIGHT" && buttonpressed == "no")
{
movement = "LEFT";
buttonpressed = "yes";
}
if (toets.Key.Equals(ConsoleKey.RightArrow) && movement != "LEFT" && buttonpressed == "no")
{
movement = "RIGHT";
buttonpressed = "yes";
}
}
}
xposlijf.Add(hoofd.xpos);
yposlijf.Add(hoofd.ypos);
switch (movement)
{
case "UP":
hoofd.ypos--;
break;
case "DOWN":
hoofd.ypos++;
break;
case "LEFT":
hoofd.xpos--;
break;
case "RIGHT":
hoofd.xpos++;
break;
}
if (xposlijf.Count() > score)
{
xposlijf.RemoveAt(0);
yposlijf.RemoveAt(0);
}
}
Console.SetCursorPosition(screenwidth / 5, screenheight / 2);
Console.WriteLine("Game over, Score: " + score);
Console.SetCursorPosition(screenwidth / 5, screenheight / 2 + 1);
}
private static void ClearConsole(int screenwidth, int screenheight)
{
var blackLine = string.Join("", new byte[screenwidth - 2].Select(b => " ").ToArray());
Console.ForegroundColor = ConsoleColor.Black;
for (int i = 1; i < screenheight - 1; i++)
{
Console.SetCursorPosition(1, i);
Console.Write(blackLine);
}
}
private static void DrawBorder(int screenwidth, int screenheight)
{
var horizontalBar = string.Join("", new byte[screenwidth].Select(b => "■").ToArray());
Console.SetCursorPosition(0, 0);
Console.Write(horizontalBar);
Console.SetCursorPosition(0, screenheight - 1);
Console.Write(horizontalBar);
for (int i = 0; i < screenheight; i++)
{
Console.SetCursorPosition(0, i);
Console.Write("■");
Console.SetCursorPosition(screenwidth - 1, i);
Console.Write("■");
}
}
class pixel
{
public int xpos { get; set; }
public int ypos { get; set; }
public ConsoleColor schermkleur { get; set; }
}
}
}
First up:
Good job on this game. Your logic is good. Yes, there are some improvements in the code and so on, and all of the above comments are spot on, so I'm not going to hammer on the same points.
Simple Enhancements:
When I ran the game I found it a bit laggy. The constant Console.Clear();
operations made the screen flicker quite a bit. Also, the drawing of the border was a bit long winded. For example, you are on a row at 0, 0... You can write the entire horizontal bar in one go with one Console.Write("■■■■■■■■■■■■■■");
statement. The same applies for the bottom row of the border.
Console.Clear();
we can just create a method named ClearConsole();
that writes black over the inside of the frame.using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
///█ ■
////https://www.youtube.com/watch?v=SGZgvMwjq2U
namespace Snake
{
class Program
{
static void Main(string[] args)
{
Console.WindowHeight = 16;
Console.WindowWidth = 32;
int screenwidth = Console.WindowWidth;
int screenheight = Console.WindowHeight;
Random randomnummer = new Random();
int score = 5;
int gameover = 0;
pixel hoofd = new pixel();
hoofd.xpos = screenwidth / 2;
hoofd.ypos = screenheight / 2;
hoofd.schermkleur = ConsoleColor.Red;
string movement = "RIGHT";
List<int> xposlijf = new List<int>();
List<int> yposlijf = new List<int>();
int berryx = randomnummer.Next(0, screenwidth);
int berryy = randomnummer.Next(0, screenheight);
DateTime tijd = DateTime.Now;
DateTime tijd2 = DateTime.Now;
string buttonpressed = "no";
// We only draw the border once. It doesn't change.
DrawBorder(screenwidth, screenheight);
while (true)
{
ClearConsole(screenwidth, screenheight);
if (hoofd.xpos == screenwidth - 1 || hoofd.xpos == 0 || hoofd.ypos == screenheight - 1 || hoofd.ypos == 0)
{
gameover = 1;
}
Console.ForegroundColor = ConsoleColor.Green;
if (berryx == hoofd.xpos && berryy == hoofd.ypos)
{
score++;
berryx = randomnummer.Next(1, screenwidth - 2);
berryy = randomnummer.Next(1, screenheight - 2);
}
for (int i = 0; i < xposlijf.Count(); i++)
{
Console.SetCursorPosition(xposlijf[i], yposlijf[i]);
Console.Write("■");
if (xposlijf[i] == hoofd.xpos && yposlijf[i] == hoofd.ypos)
{
gameover = 1;
}
}
if (gameover == 1)
{
break;
}
Console.SetCursorPosition(hoofd.xpos, hoofd.ypos);
Console.ForegroundColor = hoofd.schermkleur;
Console.Write("■");
Console.SetCursorPosition(berryx, berryy);
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write("■");
Console.CursorVisible = false;
tijd = DateTime.Now;
buttonpressed = "no";
while (true)
{
tijd2 = DateTime.Now;
if (tijd2.Subtract(tijd).TotalMilliseconds > 500) { break; }
if (Console.KeyAvailable)
{
ConsoleKeyInfo toets = Console.ReadKey(true);
//Console.WriteLine(toets.Key.ToString());
if (toets.Key.Equals(ConsoleKey.UpArrow) && movement != "DOWN" && buttonpressed == "no")
{
movement = "UP";
buttonpressed = "yes";
}
if (toets.Key.Equals(ConsoleKey.DownArrow) && movement != "UP" && buttonpressed == "no")
{
movement = "DOWN";
buttonpressed = "yes";
}
if (toets.Key.Equals(ConsoleKey.LeftArrow) && movement != "RIGHT" && buttonpressed == "no")
{
movement = "LEFT";
buttonpressed = "yes";
}
if (toets.Key.Equals(ConsoleKey.RightArrow) && movement != "LEFT" && buttonpressed == "no")
{
movement = "RIGHT";
buttonpressed = "yes";
}
}
}
xposlijf.Add(hoofd.xpos);
yposlijf.Add(hoofd.ypos);
switch (movement)
{
case "UP":
hoofd.ypos--;
break;
case "DOWN":
hoofd.ypos++;
break;
case "LEFT":
hoofd.xpos--;
break;
case "RIGHT":
hoofd.xpos++;
break;
}
if (xposlijf.Count() > score)
{
xposlijf.RemoveAt(0);
yposlijf.RemoveAt(0);
}
}
Console.SetCursorPosition(screenwidth / 5, screenheight / 2);
Console.WriteLine("Game over, Score: " + score);
Console.SetCursorPosition(screenwidth / 5, screenheight / 2 + 1);
}
private static void ClearConsole(int screenwidth, int screenheight)
{
var blackLine = string.Join("", new byte[screenwidth - 2].Select(b => " ").ToArray());
Console.ForegroundColor = ConsoleColor.Black;
for (int i = 1; i < screenheight - 1; i++)
{
Console.SetCursorPosition(1, i);
Console.Write(blackLine);
}
}
private static void DrawBorder(int screenwidth, int screenheight)
{
var horizontalBar = string.Join("", new byte[screenwidth].Select(b => "■").ToArray());
Console.SetCursorPosition(0, 0);
Console.Write(horizontalBar);
Console.SetCursorPosition(0, screenheight - 1);
Console.Write(horizontalBar);
for (int i = 0; i < screenheight; i++)
{
Console.SetCursorPosition(0, i);
Console.Write("■");
Console.SetCursorPosition(screenwidth - 1, i);
Console.Write("■");
}
}
class pixel
{
public int xpos { get; set; }
public int ypos { get; set; }
public ConsoleColor schermkleur { get; set; }
}
}
}
First up:
Good job on this game. Your logic is good. Yes, there are some improvements in the code and so on, and all of the above comments are spot on, so I'm not going to hammer on the same points.
Simple Enhancements:
When I ran the game I found it a bit laggy. The constant
Console.Clear();operations made the screen flicker quite a bit. Also, the drawing of the border was a bit long winded. For example, you are on a row at 0, 0... You can write the entire horizontal bar in one go with one
Console.Write("■■■■■■■■■■■■■■");statement. The same applies for the bottom row of the border.
On that note: The border doesn't change. Right? So we don't have to keep on drawing the border. We can draw it at the beginning and "pseudo-clear" the console. What I mean by that: the console is already black, so instead of saying
Console.Clear();we can just create a method named
ClearConsole();that writes black over the inside of the frame.
I've made those minor changes, and the game runs a lot smoother, without the flickering on the screen. Here is the modified code, with your variable names and all of that intact:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
///█ ■
////https://www.youtube.com/watch?v=SGZgvMwjq2U
namespace Snake
{
class Program
{
static void Main(string[] args)
{
Console.WindowHeight = 16;
Console.WindowWidth = 32;
int screenwidth = Console.WindowWidth;
int screenheight = Console.WindowHeight;
Random randomnummer = new Random();
int score = 5;
int gameover = 0;
pixel hoofd = new pixel();
hoofd.xpos = screenwidth / 2;
hoofd.ypos = screenheight / 2;
hoofd.schermkleur = ConsoleColor.Red;
string movement = "RIGHT";
List<int> xposlijf = new List<int>();
List<int> yposlijf = new List<int>();
int berryx = randomnummer.Next(0, screenwidth);
int berryy = randomnummer.Next(0, screenheight);
DateTime tijd = DateTime.Now;
DateTime tijd2 = DateTime.Now;
string buttonpressed = "no";
// We only draw the border once. It doesn't change.
DrawBorder(screenwidth, screenheight);
while (true)
{
ClearConsole(screenwidth, screenheight);
if (hoofd.xpos == screenwidth - 1 || hoofd.xpos == 0 || hoofd.ypos == screenheight - 1 || hoofd.ypos == 0)
{
gameover = 1;
}
Console.ForegroundColor = ConsoleColor.Green;
if (berryx == hoofd.xpos && berryy == hoofd.ypos)
{
score++;
berryx = randomnummer.Next(1, screenwidth - 2);
berryy = randomnummer.Next(1, screenheight - 2);
}
for (int i = 0; i < xposlijf.Count(); i++)
{
Console.SetCursorPosition(xposlijf[i], yposlijf[i]);
Console.Write("■");
if (xposlijf[i] == hoofd.xpos && yposlijf[i] == hoofd.ypos)
{
gameover = 1;
}
}
if (gameover == 1)
{
break;
}
Console.SetCursorPosition(hoofd.xpos, hoofd.ypos);
Console.ForegroundColor = hoofd.schermkleur;
Console.Write("■");
Console.SetCursorPosition(berryx, berryy);
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write("■");
Console.CursorVisible = false;
tijd = DateTime.Now;
buttonpressed = "no";
while (true)
{
tijd2 = DateTime.Now;
if (tijd2.Subtract(tijd).TotalMilliseconds > 500) { break; }
if (Console.KeyAvailable)
{
ConsoleKeyInfo toets = Console.ReadKey(true);
//Console.WriteLine(toets.Key.ToString());
if (toets.Key.Equals(ConsoleKey.UpArrow) && movement != "DOWN" && buttonpressed == "no")
{
movement = "UP";
buttonpressed = "yes";
}
if (toets.Key.Equals(ConsoleKey.DownArrow) && movement != "UP" && buttonpressed == "no")
{
movement = "DOWN";
buttonpressed = "yes";
}
if (toets.Key.Equals(ConsoleKey.LeftArrow) && movement != "RIGHT" && buttonpressed == "no")
{
movement = "LEFT";
buttonpressed = "yes";
}
if (toets.Key.Equals(ConsoleKey.RightArrow) && movement != "LEFT" && buttonpressed == "no")
{
movement = "RIGHT";
buttonpressed = "yes";
}
}
}
xposlijf.Add(hoofd.xpos);
yposlijf.Add(hoofd.ypos);
switch (movement)
{
case "UP":
hoofd.ypos--;
break;
case "DOWN":
hoofd.ypos++;
break;
case "LEFT":
hoofd.xpos--;
break;
case "RIGHT":
hoofd.xpos++;
break;
}
if (xposlijf.Count() > score)
{
xposlijf.RemoveAt(0);
yposlijf.RemoveAt(0);
}
}
Console.SetCursorPosition(screenwidth / 5, screenheight / 2);
Console.WriteLine("Game over, Score: " + score);
Console.SetCursorPosition(screenwidth / 5, screenheight / 2 + 1);
}
private static void ClearConsole(int screenwidth, int screenheight)
{
var blackLine = string.Join("", new byte[screenwidth - 2].Select(b => " ").ToArray());
Console.ForegroundColor = ConsoleColor.Black;
for (int i = 1; i < screenheight - 1; i++)
{
Console.SetCursorPosition(1, i);
Console.Write(blackLine);
}
}
private static void DrawBorder(int screenwidth, int screenheight)
{
var horizontalBar = string.Join("", new byte[screenwidth].Select(b => "■").ToArray());
Console.SetCursorPosition(0, 0);
Console.Write(horizontalBar);
Console.SetCursorPosition(0, screenheight - 1);
Console.Write(horizontalBar);
for (int i = 0; i < screenheight; i++)
{
Console.SetCursorPosition(0, i);
Console.Write("■");
Console.SetCursorPosition(screenwidth - 1, i);
Console.Write("■");
}
}
class pixel
{
public int xpos { get; set; }
public int ypos { get; set; }
public ConsoleColor schermkleur { get; set; }
}
}
}