Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .vs/Snake/DesignTimeBuild/.dtbcache
Binary file not shown.
Binary file modified .vs/Snake/v16/.suo
Binary file not shown.
Binary file modified .vs/Snake/v16/Server/sqlite3/storage.ide
Binary file not shown.
5 changes: 2 additions & 3 deletions .vs/Snake/xs/UserPrefs.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
<Properties StartupConfiguration="{69E59B32-AC27-441B-93F1-69B727F0BC9E}|Default">
<MonoDevelop.Ide.Workbench ActiveDocument="snake/Snake.cs">
<MonoDevelop.Ide.Workbench ActiveDocument="snake/Program.cs">
<Files>
<File FileName="Snake/Program.cs" Line="1" Column="1" />
<File FileName="Snake/Snake.cs" Line="1" Column="1" />
</Files>
<Pads>
<Pad Id="ProjectPad">
<State name="__root__">
<Node name="Snake" expanded="True">
<Node name="Snake" expanded="True">
<Node name="Snake.cs" selected="True" />
<Node name="Program.cs" selected="True" />
</Node>
</Node>
</State>
Expand Down
2 changes: 1 addition & 1 deletion .vs/Snake/xs/project-cache/Snake-Debug.json

Large diffs are not rendered by default.

Binary file modified .vs/Snake/xs/sqlite3/storage.ide
Binary file not shown.
Binary file modified .vs/Snake/xs/sqlite3/storage.ide-shm
Binary file not shown.
Binary file modified .vs/Snake/xs/sqlite3/storage.ide-wal
Binary file not shown.
85 changes: 84 additions & 1 deletion snake/Apple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,90 @@

namespace Snake
{
class Apple
public class Apple
{
private int n;
public int X;
public int Y;
public Apple(int height)
{
this.n = height;

}
public void generate()
{
Random apple = new Random();
X = apple.Next(1, 2 * n - 1);
Y = apple.Next(1, n);
Console.SetCursorPosition(X, Y);
Console.Write("@");
}
}
}



//using System;
//using System.Collections.Generic;
//using System.Text;

//namespace Snake
//{
// public class Apple
// {
// int x;
// int y;
// int minX;
// int minY;
// int maxX;
// int maxY;
// Random rand;

// public int X
// {
// get
// {
// return x;
// }
// set
// {
// x = value;
// }

// }

// public int Y
// {
// get
// {
// return y;
// }
// set
// {
// y = value;
// }

// }

// public Apple(int minY, int minX, int maxX, int maxY)
// {
// this.minX = minX;
// this.maxX = maxX;
// this.minY = minY;
// this.maxY = maxY;
// this.rand = new Random();

// }

// public void generate()
// {
// x = rand.Next(minX, maxX);
// y = rand.Next(minY, maxY);
// Console.SetCursorPosition(x, y);
// Console.Write('*');
// }



// }
//}
85 changes: 85 additions & 0 deletions snake/Board.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Snake
{
class Board
{
int n;
public Board(int size)
{
this.n = size;
}
public void drawBoard()
{
// upper
for (int i = 0; i < 2 * n; i++) Console.Write("*");
Console.Write("\n");
// middle
for (int i = 0; i < n - 2; i++)
{
Console.Write("*");
for (int j = 0; j < 2 * n - 2; j++) Console.Write(" ");
Console.Write("*");
Console.Write("\n");
}
//lower
for (int i = 0; i < 2 * n; i++) Console.Write("*");
Console.Write("\n");
}
}

}





//using System;
//namespace Snake
//{
// public class Board
// {
// private int height;
// private int width;
// private int startX;
// private int startY;

// public Board(int height, int width, int startX, int startY)
// {
// this.height = height;
// this.width = width;
// this.startX = startX;
// this.startY = startY;
// }

// public void DrawBoard()
// {
// for (int i = startY; i < height; i++)
// {
// if (i == 0 || i == height - 1)
// for (int j = 0; j < width; j++)
// {
// Console.Write("-");
// }
// else
// {
// for (int j = 0; j < width; j++)
// {
// if (j == 0 || j == width - 1)
// {
// Console.Write("|");
// }
// else
// {
// Console.Write(" ");
// }
// }
// }
// Console.WriteLine();
// }

// }
// }
//}
54 changes: 54 additions & 0 deletions snake/Game.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Timers;

namespace Snake
{

class Game
{
Apple apple;
Board board;
Timer timer;
Snake snake;

public Game(int level,int timerms, int snakeStartX, int snakeStartY)
{
this.apple = new Apple(level);
this.board = new Board(level);
this.timer = new System.Timers.Timer(timerms);
this.snake = new Snake(snakeStartX, snakeStartY);
}

public void playGame()
{
timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
timer.Enabled = true;


board.drawBoard();
Console.CursorVisible = false;

apple.generate();

ConsoleKey action = ConsoleKey.F;
snake.draw();


while (action != ConsoleKey.Q)
{
action = Console.ReadKey().Key;
snake.changeDirection(action);

}
}

public void OnTimedEvent(Object source, ElapsedEventArgs e)
{
snake.slither();
snake.Eat(apple);
}

}
}
71 changes: 8 additions & 63 deletions snake/Program.cs
Original file line number Diff line number Diff line change
@@ -1,73 +1,18 @@
using System;
using System.Timers;

namespace Snake
{
class Program
{
static int height = 20;
static int ms = 200;
static int snakeStartX = 5;
static int snakeStartY = 5;
static void Main(string[] args)
{

int x = 1;
int y = 1;
Snake snake = new Snake(1,1,50);
snake.draw();
ConsoleKey action = ConsoleKey.F;

while (action != ConsoleKey.Q)
{
//quit = Console.ReadKey().Key;
//int oldX = x;
//int oldY = y;

//switch (quit)
//{
// case ConsoleKey.UpArrow:
// y = y - 1;
// if (pacMan.Equals(">") || pacMan.Equals("<") || pacMan.Equals("-") || pacMan.Equals("\u0C79") || pacMan.Equals("\u02C4")) pacMan = "\u02C5";
// else if (pacMan.Equals(">") || pacMan.Equals("<") || pacMan.Equals("-") || pacMan.Equals("\u02C5") || pacMan.Equals("\u02C4")) pacMan = "\u0C79";
// break;
// case ConsoleKey.DownArrow:
// y = y + 1;
// if (pacMan.Equals(">") || pacMan.Equals("<") || pacMan.Equals("-") || pacMan.Equals("\u0C79") || pacMan.Equals("\u02C5")) pacMan = "\u02C4";
// else if (pacMan.Equals(">") || pacMan.Equals("<") || pacMan.Equals("-") || pacMan.Equals("\u02C5") || pacMan.Equals("\u02C4")) pacMan = "\u0C79";
// break;
// case ConsoleKey.LeftArrow:
// x = x - 1;
// if (pacMan.Equals("\u02C4") || pacMan.Equals("<") || pacMan.Equals("-") || pacMan.Equals("\u0C79") || pacMan.Equals("\u02C5")) pacMan = ">";
// else if (pacMan.Equals(">") || pacMan.Equals("<") || pacMan.Equals("\u0C79") || pacMan.Equals("\u02C5") || pacMan.Equals("\u02C4")) pacMan = "-";
// break;
// case ConsoleKey.RightArrow:
// x = x + 1;
// if (pacMan.Equals(">") || pacMan.Equals("\u02C4") || pacMan.Equals("-") || pacMan.Equals("\u0C79") || pacMan.Equals("\u02C5")) pacMan = "<";
// else if (pacMan.Equals(">") || pacMan.Equals("<") || pacMan.Equals("\u0C79") || pacMan.Equals("\u02C5") || pacMan.Equals("\u02C4")) pacMan = "-";
// break;
//}

////if (pacMan.Equals("<")) pacMan = "-";
////else pacMan = "<";

//WriteAt(pacMan, x, y);
//WriteAt(" ", oldX, oldY);

while (action != ConsoleKey.Q)
{
snake.draw();

action = Console.ReadKey().Key;

if (action == ConsoleKey.UpArrow) snake.slither();

}
}

static void Apple()
{
Random apple = new Random();
int x = apple.Next(0, 20);
int y = apple.Next(0, 20);
Console.SetCursorPosition(x, y);
Console.Write("*");
}
}
Game game = new Game(height, ms, snakeStartX, snakeStartY);
game.playGame();
}
}
}
Loading