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; }
}
}
}
- 61
- 1
- 1