Skip to main content
Code Review

Return to Question

Tweeted twitter.com/StackCodeReview/status/1602996830228340736
deleted 38 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

First C# program (Snake Gamegame)

I've just started out using C#. I've used Scratch (drag and drop programming for kids) for quite some time. Since Scratch didn't have classes and methods iI have a feeling this code could be a lot more streamlined, neat, efficient, more read-able, shorter and in general just better. Console Snake game in C#:

First C# program (Snake Game)

I've just started out using C#. I've used Scratch (drag and drop programming for kids) for quite some time. Since Scratch didn't have classes and methods i have a feeling this code could be a lot more streamlined, neat, efficient, more read-able, shorter and in general just better. Console Snake game in C#:

First C# program (Snake game)

I've just started out using C#. I've used Scratch (drag and drop programming for kids) for quite some time. Since Scratch didn't have classes and methods I have a feeling this code could be a lot more streamlined, neat, efficient, more read-able, shorter and in general just better.

added 422 characters in body
Source Link
Wagacca
  • 179
  • 1
  • 1
  • 7
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";
 while (true)
 {
 Console.Clear();
 if (hoofd.xpos == screenwidth-1 || hoofd.xpos == 0 ||hoofd.ypos == screenheight-1 || hoofd.ypos == 0)
 { 
 gameover = 1;
 }
 for (int i = 0;i< screenwidth; i++)
 {
 Console.SetCursorPosition(i, 0);
 Console.Write("しかく");
 }
 for (int i = 0; i < screenwidth; i++)
 {
 Console.SetCursorPosition(i, screenheight -1);
 Console.Write("しかく");
 }
 for (int i = 0; i < screenheight; i++)
 {
 Console.SetCursorPosition(0, i);
 Console.Write("しかく");
 }
 for (int i = 0; i < screenheight; i++)
 {
 Console.SetCursorPosition(screenwidth - 1, i);
 Console.Write("しかく");
 }
 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("しかく");
 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);
 }
 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;
 while (true)
 {
 Console.Clear();
 if (hoofd.xpos == screenwidth-1 || hoofd.xpos == 0 ||hoofd.ypos == screenheight-1 || hoofd.ypos == 0)
 { 
 gameover = 1;
 }
 for (int i = 0;i< screenwidth; i++)
 {
 Console.SetCursorPosition(i, 0);
 Console.Write("しかく");
 }
 for (int i = 0; i < screenwidth; i++)
 {
 Console.SetCursorPosition(i, screenheight -1);
 Console.Write("しかく");
 }
 for (int i = 0; i < screenheight; i++)
 {
 Console.SetCursorPosition(0, i);
 Console.Write("しかく");
 }
 for (int i = 0; i < screenheight; i++)
 {
 Console.SetCursorPosition(screenwidth - 1, i);
 Console.Write("しかく");
 }
 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("しかく");
 tijd = DateTime.Now;
 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")
 {
 movement = "UP";
 }
 if (toets.Key.Equals(ConsoleKey.DownArrow) && movement != "UP")
 {
 movement = "DOWN";
 }
 if (toets.Key.Equals(ConsoleKey.LeftArrow) && movement != "RIGHT")
 {
 movement = "LEFT";
 }
 if (toets.Key.Equals(ConsoleKey.RightArrow) && movement != "LEFT")
 {
 movement = "RIGHT";
 }
 }
 }
 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);
 }
 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";
 while (true)
 {
 Console.Clear();
 if (hoofd.xpos == screenwidth-1 || hoofd.xpos == 0 ||hoofd.ypos == screenheight-1 || hoofd.ypos == 0)
 { 
 gameover = 1;
 }
 for (int i = 0;i< screenwidth; i++)
 {
 Console.SetCursorPosition(i, 0);
 Console.Write("しかく");
 }
 for (int i = 0; i < screenwidth; i++)
 {
 Console.SetCursorPosition(i, screenheight -1);
 Console.Write("しかく");
 }
 for (int i = 0; i < screenheight; i++)
 {
 Console.SetCursorPosition(0, i);
 Console.Write("しかく");
 }
 for (int i = 0; i < screenheight; i++)
 {
 Console.SetCursorPosition(screenwidth - 1, i);
 Console.Write("しかく");
 }
 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("しかく");
 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);
 }
 class pixel
 {
 public int xpos { get; set; }
 public int ypos { get; set; }
 public ConsoleColor schermkleur { get; set; }
 }
 }
}
//¦
edited tags; edited title
Link
200_success
  • 145.5k
  • 22
  • 190
  • 479

First C# program (Snake Game): Did I overcomplicate things?

Source Link
Wagacca
  • 179
  • 1
  • 1
  • 7
Loading
lang-cs

AltStyle によって変換されたページ (->オリジナル) /