Created
March 26, 2018 04:01
-
-
Save GokselKUCUKSAHIN/e102caf2ce1aaad81b72007c59ca9743 to your computer and use it in GitHub Desktop.
C# Snake Game Clone
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Snake_2 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Snake Yilan = new Snake(); | |
Yem Elma = new Yem(); | |
Elma.Create(true); | |
ConsoleKeyInfo Tus = Console.ReadKey(); | |
Console.CursorVisible = false; | |
bool isKeyPressed = false; | |
int Score = 0; | |
ScoreDraw(Score); | |
while (true) | |
{ | |
DrawFrame(); | |
if (Console.KeyAvailable) | |
{ | |
Tus = Console.ReadKey(); | |
isKeyPressed = true; | |
} | |
else | |
isKeyPressed = false; | |
Yilan.Move(Tus,isKeyPressed); | |
Elma.Draw(); | |
if(!Yilan.Die()) | |
{ | |
Score = 0; | |
ScoreDraw(Score); | |
} | |
Yilan.Draw(); | |
if (Yilan.isEat(Elma.X, Elma.Y)) | |
{ | |
Yilan.Grow(); | |
Elma.Create(Yilan.X,Yilan.Y); | |
Elma.Draw(); | |
Score += 5; | |
ScoreDraw(Score); | |
} | |
System.Threading.Thread.Sleep(80) ; //80 ideal | |
//Console.Clear(); | |
} | |
} | |
static void DrawFrame() | |
{ | |
// | |
//CHARS | |
// | |
char Cubuk = '█'; | |
char Kare = '▄'; | |
Console.ForegroundColor = ConsoleColor.Blue; | |
for (int i = 0; i <= 41; i++) | |
{ | |
Console.SetCursorPosition(i, 0); | |
Console.Write(Kare); | |
Console.SetCursorPosition(i, 21); | |
Console.Write(Kare); | |
} | |
for (int i = 1; i <= 21; i++) | |
{ | |
Console.SetCursorPosition(0, i); | |
Console.Write(Cubuk); | |
Console.SetCursorPosition(41, i); | |
Console.Write(Cubuk); | |
} | |
Console.ForegroundColor = ConsoleColor.White; | |
} | |
static void ScoreDraw(int Gain) | |
{ | |
Console.ForegroundColor = ConsoleColor.White; | |
Console.SetCursorPosition(0, 22); | |
Console.Write(" "); | |
Console.SetCursorPosition(0, 22); | |
Console.Write("Score = " + Gain); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Snake_2 | |
{ | |
class Snake | |
{ | |
char Kare = '▄'; | |
public List<int> X = new List<int>() { 17, 19, 21, 23, 25 }; | |
public List<int> Y = new List<int>() { 10, 10, 10, 10, 10 }; | |
int Blockside = 2; | |
byte Pos = 4; | |
public void Grow() | |
{ | |
int Tx = X[X.Count - 1]; | |
int Ty = Y[Y.Count - 1]; | |
X.Add(Tx); | |
Y.Add(Ty); | |
} | |
public bool isEat(int Ax, int Ay) | |
{ | |
if ((Ax == X[X.Count - 1]) && (Ay == Y[Y.Count - 1])) | |
{ | |
return true; | |
} | |
else | |
return false; | |
} | |
private void GoWhile(byte Pos) | |
{ | |
if (Pos == 1) | |
{ | |
Blockside = 3; | |
int Nx = X[X.Count - 1]; | |
int Ny = Y[Y.Count - 1] - 1; | |
if (Ny < 1) | |
{ | |
Ny = 20; | |
} | |
X.Add(Nx); | |
Y.Add(Ny); | |
Console.SetCursorPosition(X[0], Y[0]); | |
Console.Write(" "); | |
X.RemoveAt(0); | |
Y.RemoveAt(0); | |
} | |
else if (Pos == 2) | |
{ | |
Blockside = 4; | |
int Nx = X[X.Count - 1] - 2; | |
int Ny = Y[Y.Count - 1]; | |
if (Nx < 1) | |
{ | |
Nx = 39; | |
} | |
X.Add(Nx); | |
Y.Add(Ny); | |
Console.SetCursorPosition(X[0], Y[0]); | |
Console.Write(" "); | |
X.RemoveAt(0); | |
Y.RemoveAt(0); | |
} | |
else if (Pos == 3) | |
{ | |
Blockside = 1; | |
int Nx = X[X.Count - 1]; | |
int Ny = Y[Y.Count - 1] + 1; | |
if (Ny > 20) | |
{ | |
Ny = 1; | |
} | |
X.Add(Nx); | |
Y.Add(Ny); | |
Console.SetCursorPosition(X[0], Y[0]); | |
Console.Write(" "); | |
X.RemoveAt(0); | |
Y.RemoveAt(0); | |
} | |
else if (Pos == 4) | |
{ | |
Blockside = 2; | |
int Nx = X[X.Count - 1] + 2; | |
int Ny = Y[Y.Count - 1]; | |
if (Nx > 39) | |
{ | |
Nx = 1; | |
} | |
X.Add(Nx); | |
Y.Add(Ny); | |
Console.SetCursorPosition(X[0], Y[0]); | |
Console.Write(" "); | |
X.RemoveAt(0); | |
Y.RemoveAt(0); | |
} | |
} | |
public void Move(ConsoleKeyInfo Tus,bool OK) | |
{ | |
if((Tus.Key == ConsoleKey.UpArrow) && (Blockside != 1)) | |
{ | |
Pos = 1; | |
GoWhile(Pos); | |
} | |
else if((Tus.Key == ConsoleKey.LeftArrow) && (Blockside != 2)) | |
{ | |
Pos = 2; | |
GoWhile(Pos); | |
} | |
else if ((Tus.Key == ConsoleKey.DownArrow) && (Blockside != 3)) | |
{ | |
Pos = 3; | |
GoWhile(Pos); | |
} | |
else if ((Tus.Key == ConsoleKey.RightArrow) && (Blockside != 4)) | |
{ | |
Pos = 4; | |
GoWhile(Pos); | |
} | |
else | |
{ | |
if (Blockside == 4) | |
{ | |
GoWhile(2); | |
} | |
else if (Blockside == 2) | |
{ | |
GoWhile(4); | |
} | |
else if (Blockside == 1) | |
{ | |
GoWhile(3); | |
} | |
else if (Blockside == 3) | |
{ | |
GoWhile(1); | |
} | |
} | |
} | |
public void Draw() | |
{ | |
for (int i = 0; i < X.Count; i++) | |
{ | |
if (i == X.Count - 1) | |
{ | |
Console.ForegroundColor = ConsoleColor.Green; | |
} | |
Console.SetCursorPosition(X[i],Y[i]); | |
Console.Write(Kare); | |
} | |
} | |
public bool Die() | |
{ | |
bool Live = true; | |
int Leng = X.Count - 1; | |
int Hx = X[X.Count - 1]; | |
int Hy = Y[Y.Count - 1]; | |
for (int i = 0; (i < Leng - 3) && (Live != false); i++) | |
{ | |
if ((Hx == X[i]) && (Hy == Y[i])) | |
{ | |
Live = false; | |
} | |
} | |
if(!Live) | |
{ | |
int Len = X.Count - 5; | |
for (int i = 0; i < Len; i++) | |
{ | |
Console.SetCursorPosition(X[i],Y[i]); | |
Console.Write(" "); | |
} | |
X.RemoveRange(0, Len); | |
Y.RemoveRange(0, Len); | |
} | |
return Live; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Snake_2 | |
{ | |
class Yem | |
{ | |
public int X; | |
public int Y; | |
private char Dot = '¤'; | |
Random r = new Random(); | |
public void Draw() | |
{ | |
Console.SetCursorPosition(X, Y); | |
Console.ForegroundColor = ConsoleColor.Magenta; | |
Console.Write(Dot); | |
Console.ForegroundColor = ConsoleColor.White; | |
} | |
public void Create(List<int> Sx, List<int> Sy) | |
{ | |
int Tx = r.Next(1, 21); | |
X = (2 * Tx) - 1; | |
Y = r.Next(1, 21); | |
for (int i = 0; i < Sx.Count; i++) | |
{ | |
if ((Sx[i] == X) && (Sy[i] == Y)) | |
{ | |
try | |
{ | |
Create(Sx, Sy); | |
} | |
catch | |
{ | |
Console.SetCursorPosition(0, 23); | |
Console.Write("Catch!!!"); | |
} | |
} | |
} | |
} | |
public void Create(bool Hello) | |
{ | |
X = 17; | |
Y = 7; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
25/03/2018 : 03.45