@echo off :Start set /p name= Enter program name: echo. :\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe "%name%.cs" echo. goto Start
using System; using System.Threading; using System.Collections.Generic; using System.Linq; namespace SnakeGame { class Game { static readonly int x = 80; static readonly int y = 26; static Walls walls; static Snake snake; static FoodFactory foodFactory; static Timer time; static void Main() { Console.SetWindowSize(x + 1, y + 1); Console.SetBufferSize(x + 1, y + 1); Console.CursorVisible = false; walls = new Walls(x, y, '#'); snake = new Snake(x / 2, y / 2, 3); foodFactory = new FoodFactory(x, y, '@'); foodFactory.CreateFood(); time = new Timer(Loop, null, 0, 200); while (true) { if (Console.KeyAvailable) { ConsoleKeyInfo key = Console.ReadKey(); snake.Rotation(key.Key); } } }// Main() static void Loop(object obj) { if (walls.IsHit(snake.GetHead()) || snake.IsHit(snake.GetHead())) { time.Change(0, Timeout.Infinite); } else if (snake.Eat(foodFactory.food)) { foodFactory.CreateFood(); } else { snake.Move(); } }// Loop() }// class Game struct Point { public int x { get; set; } public int y { get; set; } public char ch { get; set; } public static implicit operator Point((int, int, char) value) => new Point {x = value.Item1, y = value.Item2, ch = value.Item3}; public static bool operator ==(Point a, Point b) => (ax == bx && ay == by) ? true : false; public static bool operator !=(Point a, Point b) => (ax != bx || ay != by) ? true : false; public void Draw() { DrawPoint(ch); } public void Clear() { DrawPoint(' '); } private void DrawPoint(char _ch) { Console.SetCursorPosition(x, y); Console.Write(_ch); } } class Walls { private char ch; private List<Point> wall = new List<Point>(); public Walls(int x, int y, char ch) { this.ch = ch; DrawHorizontal(x, 0); DrawHorizontal(x, y); DrawVertical(0, y); DrawVertical(x, y); } private void DrawHorizontal(int x, int y) { for (int i = 0; i < x; i++) { Point p = (i, y, ch); p.Draw(); wall.Add(p); } } private void DrawVertical(int x, int y) { for (int i = 0; i < y; i++) { Point p = (x, i, ch); p.Draw(); wall.Add(p); } } public bool IsHit(Point p) { foreach (var w in wall) { if (p == w) { return true; } } return false; } }// class Walls enum Direction { LEFT, RIGHT, UP, DOWN } class Snake { private List<Point> snake; private Direction direction; private int step = 1; private Point tail; private Point head; bool rotate = true; public Snake(int x, int y, int length) { direction = Direction.RIGHT; snake = new List<Point>(); for (int i = x - length; i < x; i++) { Point p = (i, y, '*'); snake.Add(p); p.Draw(); } } public Point GetHead() => snake.Last(); public void Move() { head = GetNextPoint(); snake.Add(head); tail = snake.First(); snake.Remove(tail); tail.Clear(); head.Draw(); rotate = true; } public bool Eat(Point p) { head = GetNextPoint(); if (head == p) { snake.Add(head); head.Draw(); return true; } return false; } public Point GetNextPoint () { Point p = GetHead (); switch (direction) { case Direction.LEFT: px -= step; break; case Direction.RIGHT: px += step; break; case Direction.UP: py -= step; break; case Direction.DOWN: py += step; break; } return p; } public void Rotation (ConsoleKey key) { if (rotate) { switch (direction) { case Direction.LEFT: case Direction.RIGHT: if (key == ConsoleKey.DownArrow) direction = Direction.DOWN; else if (key == ConsoleKey.UpArrow) direction = Direction.UP; break; case Direction.UP: case Direction.DOWN: if (key == ConsoleKey.LeftArrow) direction = Direction.LEFT; else if (key == ConsoleKey.RightArrow) direction = Direction.RIGHT; break; } rotate = false; } } public bool IsHit(Point p) { for (int i = snake.Count - 2; i > 0; i--) { if (snake[i] == p) { return true; } } return false; } }//class Snake class FoodFactory { int x; int y; char ch; public Point food { get; private set; } Random random = new Random(); public FoodFactory(int x, int y, char ch) { this.x = x; this.y = y; this.ch = ch; } public void CreateFood() { food = (random.Next(2, x - 2), random.Next(2, y - 2), ch); food.Draw(); } } }
using System; using System.Collections.Generic; using System.Linq; class Game{ static readonly int x = 80; static readonly int y = 26; static void Main(){ Console.SetWindowSize(x + 1, y + 1); Console.SetBufferSize(x + 1, y + 1); Console.CursorVisible = false; }// Main() }// class Game
struct Point{ public int x { get; set; } public int y { get; set; } public char ch { get; set; } public static implicit operator Point((int, int, char) value) => new Point {x = value.Item1, y = value.Item2, ch = value.Item3}; public void Draw(){ DrawPoint(ch); } public void Clear(){ DrawPoint(' '); } private void DrawPoint(char _ch){ Console.SetCursorPosition(x, y); Console.Write(_ch); } }
It is interesting!
The => operator is called a lambda operator, it is used as a definition of anonymous lambda expressions, and as a body consisting of a single expression , syntactic sugar, replacing the return operator. The above method for redefining an operator (about its purpose below) can be rewritten as follows:public static bool operator == (Point a, Point b) { if (ax == bx && ay == by) { return true; } else { return false; } }
class Walls{ private char ch; private List<Point> wall = new List<Point>(); public Walls(int x, int y, char ch){ this.ch = ch; DrawHorizontal(x, 0); DrawHorizontal(x, y); DrawVertical(0, y); DrawVertical(x, y); } private void DrawHorizontal(int x, int y){ for (int i = 0; i < x; i++){ Point p = (i, y, ch); p.Draw(); wall.Add(p); } } private void DrawVertical(int x, int y) { for (int i = 0; i < y; i++) { Point p = (x, i, ch); p.Draw(); wall.Add(p); } } }// class Walls
It is interesting!
As you might have noticed, the form Point p = (x, y, ch) is used to initialize the Point data type; as with built-in types, this becomes possible when overriding the implicit operator, which describes how variables are set.
Important!
The construction (int, int, char) is called a tuple, and works only with .net 4.7+, so if you have not installed visual studio, then you only have the v4.0.30319 compiler and you need to use standard initialization through the new operator.
class Game{ static Walls walls; static void Main(){ walls = new Walls(x, y, '#'); ...
class FoodFactory { int x; int y; char ch; public Point food { get; private set; } Random random = new Random(); public FoodFactory(int x, int y, char ch) { this.x = x; this.y = y; this.ch = ch; } public void CreateFood() { food = (random.Next(2, x - 2), random.Next(2, y - 2), ch); food.Draw(); } }
class Game{ static FoodFactory foodFactory; static void Main(){ foodFactory = new FoodFactory(x, y, '@'); foodFactory.CreateFood(); ...
enum Direction{ LEFT, RIGHT, UP, DOWN }
class Snake{ private List<Point> snake; private Direction direction; private int step = 1; private Point tail; private Point head; bool rotate = true; public Snake(int x, int y, int length){ direction = Direction.RIGHT; snake = new List<Point>(); for (int i = x - length; i < x; i++) { Point p = (i, y, '*'); snake.Add(p); p.Draw(); } } // . public Point GetHead() => snake.Last(); public void Move(){ head = GetNextPoint(); snake.Add(head); tail = snake.First(); snake.Remove(tail); tail.Clear(); head.Draw(); rotate = true; } public Point GetNextPoint() { Point p = GetHead(); switch (direction) { case Direction.LEFT: px -= step; break; case Direction.RIGHT: px += step; break; case Direction.UP: py -= step; break; case Direction.DOWN: py += step; break; } return p; } public void Rotation(ConsoleKey key) { if (rotate) { switch (direction) { case Direction.LEFT: case Direction.RIGHT: if (key == ConsoleKey.DownArrow) direction = Direction.DOWN; else if (key == ConsoleKey.UpArrow) direction = Direction.UP; break; case Direction.UP: case Direction.DOWN: if (key == ConsoleKey.LeftArrow) direction = Direction.LEFT; else if (key == ConsoleKey.RightArrow) direction = Direction.RIGHT; break; } rotate = false; } } }//class Snake
class Game{ static Snake snake; static void Main(){ snake = new Snake(x / 2, y / 2, 3); ...
class Game { static void Main () { while (true) { if (Console.KeyAvailable) { ConsoleKeyInfo key = Console.ReadKey (); snake.Rotation(key.Key); } ...
using System.Threading; class Game { static Timer time; static void Main () { time = new Timer (Loop, null, 0, 200); ...
struct Point { public static bool operator == (Point a, Point b) => (ax == bx && ay == by) ? true : false; public static bool operator != (Point a, Point b) => (ax != bx || ay != by) ? true : false; ...
class Walls { public bool IsHit (Point p) { foreach (var w in wall) { if (p == w) { return true; } } return false; } ...
class Snake { public bool IsHit (Point p) { for (int i = snake.Count - 2; i > 0; i--) { if (snake[i] == p) { return true; } } return false; } ...
class Snake { public bool Eat (Point p) { head = GetNextPoint (); if (head == p) { snake.Add (head); head.Draw (); return true; } return false; } ...
class Snake { static void Loop (object obj) { if (walls.IsHit (snake.GetHead ()) || snake.IsHit (snake.GetHead ())) { time.Change (0, Timeout.Infinite); } else if (snake.Eat (foodFactory.food)) { foodFactory.CreateFood (); } else { snake.Move (); } } ...
Source: https://habr.com/ru/post/348262/
All Articles