diff --git a/.gitignore b/.gitignore
index 035f367..74ad14a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -132,7 +132,7 @@ publish/
# NuGet Packages Directory
## TODO: If you have NuGet Package Restore enabled, uncomment the next line
-#packages/
+packages/
# Windows Azure Build Output
csx
diff --git a/SnakeGame/Form1.Designer.cs b/SnakeGame/Form1.Designer.cs
index 8a32683..a4658a3 100644
--- a/SnakeGame/Form1.Designer.cs
+++ b/SnakeGame/Form1.Designer.cs
@@ -42,6 +42,7 @@
this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
+ this.panel1.BackColor = System.Drawing.Color.Black;
this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panel1.Location = new System.Drawing.Point(12, 41);
this.panel1.Name = "panel1";
@@ -76,6 +77,7 @@
this.newSingleplayerGameToolStripMenuItem.Name = "newSingleplayerGameToolStripMenuItem";
this.newSingleplayerGameToolStripMenuItem.Size = new System.Drawing.Size(197, 22);
this.newSingleplayerGameToolStripMenuItem.Text = "New singleplayer game";
+ this.newSingleplayerGameToolStripMenuItem.Click += new System.EventHandler(this.newSingleplayerGameToolStripMenuItem_Click);
//
// newMultiplayerGameToolStripMenuItem
//
diff --git a/SnakeGame/Form1.cs b/SnakeGame/Form1.cs
index e22509b..c31829f 100644
--- a/SnakeGame/Form1.cs
+++ b/SnakeGame/Form1.cs
@@ -12,7 +12,21 @@ namespace SnakeGame
{
public partial class Form1 : Form
{
- public static Timer Timer;
+ private static Timer Timer;
+ private static bool timerenabled = false;
+ public static bool TimerEnabled
+ {
+ get
+ {
+ return timerenabled;
+ }
+ set
+ {
+ if (value && !timerenabled) //Only start if not running already
+ Timer.Start();
+ timerenabled = value;
+ }
+ }
public Form1()
{
InitializeComponent();
@@ -32,9 +46,10 @@ namespace SnakeGame
Timer.Stop();
Game.Refresh();
Timer.Interval = Game.UpdateTime;
- Timer.Start();
+ if (TimerEnabled)
+ Timer.Start();
};
- Timer.Start();
+ //Timer.Start();
}
private void Form1_Load(object sender, EventArgs e)
@@ -58,5 +73,10 @@ namespace SnakeGame
else if (e.KeyCode == Keys.Right)
Game.MoveDirection = Direction.Right;
}
+
+ private void newSingleplayerGameToolStripMenuItem_Click(object sender, EventArgs e)
+ {
+ Game.Start(GameStartMode.SinglePlayer);
+ }
}
}
diff --git a/SnakeGame/Game.cs b/SnakeGame/Game.cs
index d42a3d6..3832629 100644
--- a/SnakeGame/Game.cs
+++ b/SnakeGame/Game.cs
@@ -10,11 +10,17 @@ namespace SnakeGame
{
public static class Game
{
+ ///
+ /// Plans:
+ /// Replace MessageBox with own GUI; Add GUI to get GameSize...
+ /// Maybe select region to set one square's size and/or visualize changes when user stops interaction
+ ///
public static SqCoord GameSize;
- public static List GameField;
+ //public static List GameField;
+ public static int[,] GameField;
public static SqCoord PlayerPos;
public static int Length = 4;
- public static int UpdateTime = 2000;
+ public static int UpdateTime = 1000;
public static Direction MoveDirection;
public static void Start(GameStartMode mode)
@@ -22,6 +28,8 @@ namespace SnakeGame
switch(mode)
{
case GameStartMode.SinglePlayer:
+ Game.Reset();
+ Form1.TimerEnabled = true;
break;
default:
throw new ArgumentException();
@@ -31,21 +39,11 @@ namespace SnakeGame
public static void Load(Size size)
{
//GameSize = size;
- GameSize = SquareCoord.PointToSqCoord(new Point(size));
- GameField = new List(GameSize.X * GameSize.Y);
- PlayerPos = new SqCoord { X = GameSize.X / 2, Y = 1 };
- for (int i = 0; i < GameSize.X; i++)
- {
- for (int j = 0; j < GameSize.Y; j++)
- {
- SqCoord coord = new SqCoord { X = i, Y = j, Tick = 0 };
- if (i == 0 || j == 0 || i == GameSize.X - 1 || j == GameSize.Y - 1)
- coord.Tick = -1;
- else if (i == PlayerPos.X && j == PlayerPos.Y)
- coord.Tick = 4;
- GameField.Add(coord);
- }
- }
+ //GameSize = SquareCoord.PointToSqCoord(new Point(size));
+ //GameSize = new SqCoord { X = size.Width / 20, Y = size.Height / 20 };
+ //GameField = new List(GameSize.X * GameSize.Y);
+ //GameField = new int[GameSize.X, GameSize.Y];
+ Game.Reset();
//GameField.Single(entry => entry.X == PlayerPos.X && entry.Y == PlayerPos.Y).Tick;
/*for (int i = 0; i < GameField.Count; i++)
{
@@ -54,8 +52,34 @@ namespace SnakeGame
GameField[i].Tick = Length;
}
}*/
+ //GameRenderer.Render(); - It has no effect in loading part
+ }
+
+ public static void Reset()
+ {
+ Size size = GameRenderer.Panel.Size;
+ GameSize = new SqCoord { X = size.Width / 20, Y = size.Height / 20 };
+ GameField = new int[GameSize.X, GameSize.Y];
+ PlayerPos = new SqCoord { X = GameSize.X / 2, Y = 1 };
+ for (int i = 0; i < GameSize.X; i++)
+ {
+ for (int j = 0; j < GameSize.Y; j++)
+ {
+ /*SqCoord coord = new SqCoord { X = i, Y = j, Tick = 0 };
+ if (i == 0 || j == 0 || i == GameSize.X - 1 || j == GameSize.Y - 1)
+ coord.Tick = -1;
+ else if (i == PlayerPos.X && j == PlayerPos.Y)
+ coord.Tick = 4;
+ GameField.Add(coord);*/
+ if (i == 0 || j == 0 || i == GameSize.X - 1 || j == GameSize.Y - 1)
+ GameField[i, j] = -1;
+ else if (i == PlayerPos.X && j == PlayerPos.Y)
+ GameField[i, j] = 4;
+ else
+ GameField[i, j] = 0;
+ }
+ }
MoveDirection = Direction.Down;
- GameRenderer.Render();
}
public static void Refresh()
@@ -63,10 +87,16 @@ namespace SnakeGame
//Decrease any positive Ticks; if next player position is other than zero, game over
//Otherwise set next player position and set Tick on player position to current Length
//Console.WriteLine("Refreshing...");
- for (int i = 0; i < GameField.Count; i++)
+ //for (int i = 0; i < GameField.Count; i++)
+ for (int i = 0; i < GameField.GetLength(0); i++)
{
- if (GameField[i].Tick > 0)
- GameField[i] = new SqCoord { X = GameField[i].X, Y = GameField[i].Y, Tick = GameField[i].Tick - 1 };
+ for (int j = 0; j < GameField.GetLength(1); j++)
+ {
+ /*if (GameField[i].Tick > 0)
+ GameField[i] = new SqCoord { X = GameField[i].X, Y = GameField[i].Y, Tick = GameField[i].Tick - 1 };*/
+ if (GameField[i, j] > 0)
+ GameField[i, j]--;
+ }
}
SqCoord nextcoord;
switch (MoveDirection)
@@ -87,27 +117,35 @@ namespace SnakeGame
nextcoord = PlayerPos;
break;
}
- if (Game.GetCoord(nextcoord).Tick != 0)
+ //if (Game.GetCoord(nextcoord).Tick != 0)
+ if (Game.GameField[nextcoord.X, nextcoord.Y] != 0)
Stop();
- for (int i = 0; i < GameField.Count; i++)
+ else
{
- if (GameField[i].X == nextcoord.X && GameField[i].Y == nextcoord.Y)
+ for (int i = 0; i < GameField.GetLength(0); i++)
{
- GameField[i] = new SqCoord { X = nextcoord.X, Y = nextcoord.Y, Tick = Length };
- PlayerPos = GameField[i];
+ for (int j = 0; j < GameField.GetLength(1); j++)
+ {
+ if (i == nextcoord.X && j == nextcoord.Y)
+ {
+ GameField[nextcoord.X, nextcoord.Y] = Length;
+ PlayerPos = new SqCoord { X = i, Y = j };
+ }
+ }
}
+ GameRenderer.Render();
}
- GameRenderer.Render();
}
- public static SqCoord GetCoord(SqCoord nextcoord)
+ /*public static SqCoord GetCoord(SqCoord nextcoord)
{
return GameField.Single(entry => entry.X == nextcoord.X && entry.Y == nextcoord.Y);
- }
+ }*/
public static void Stop()
{
- Form1.Timer.Stop();
+ //Form1.Timer.Stop();
+ Form1.TimerEnabled = false;
MessageBox.Show("Game over!");
}
}
diff --git a/SnakeGame/GameRenderer.cs b/SnakeGame/GameRenderer.cs
index d658a8a..5dc4f64 100644
--- a/SnakeGame/GameRenderer.cs
+++ b/SnakeGame/GameRenderer.cs
@@ -13,20 +13,25 @@ namespace SnakeGame
public static Panel Panel;
public static void Render()
{
- foreach(var coord in Game.GameField)
+ //foreach(var coord in Game.GameField)
+ for (int i = 0; i < Game.GameField.GetLength(0); i++)
{
- if (coord.Tick == -1)
- RenderSquare(coord, Color.Red);
- else if (coord.Tick == 0)
- RenderSquare(coord, Color.Black);
- else
- RenderSquare(coord, Color.Green);
+ for (int j = 0; j < Game.GameField.GetLength(1); j++)
+ {
+ //if (coord.Tick == -1)
+ if (Game.GameField[i, j] == -1)
+ RenderSquare(new SqCoord { X = i, Y = j }, Color.Red);
+ else if (Game.GameField[i, j] == 0)
+ RenderSquare(new SqCoord { X = i, Y = j }, Color.Black);
+ else
+ RenderSquare(new SqCoord { X = i, Y = j }, Color.Green);
+ }
}
}
private static void RenderSquare(SqCoord coord, Color color)
{
Graphics gr = Panel.CreateGraphics();
- gr.FillRectangle(new SolidBrush(color), SquareCoord.SqCoordToRect(coord));
+ gr.FillRectangle(new SolidBrush(color), SquareCoord.SqCoordToRect(coord, Panel.Size));
}
}
}
diff --git a/SnakeGame/SquareCoord.cs b/SnakeGame/SquareCoord.cs
index ce347f1..4a55ee2 100644
--- a/SnakeGame/SquareCoord.cs
+++ b/SnakeGame/SquareCoord.cs
@@ -9,7 +9,7 @@ namespace SnakeGame
{
public static class SquareCoord
{
- private static int x;
+ /*private static int x;
public static int X
{
get
@@ -34,20 +34,22 @@ namespace SnakeGame
y = value;
Game.Refresh();
}
- }
- private const int res = 50;
+ }*/
+ //private const int res = 20;
- public static Point SqCoordToPoint(SqCoord coord)
+ /*public static Point SqCoordToPoint(SqCoord coord)
{
return new Point(coord.X * res, coord.Y * res);
- }
- public static SqCoord PointToSqCoord(Point point)
+ }*/
+ /*public static SqCoord PointToSqCoord(Point point)
{
return new SqCoord { X = point.X / res, Y = point.Y / res };
- }
- public static Rectangle SqCoordToRect(SqCoord coord)
+ }*/
+ public static Rectangle SqCoordToRect(SqCoord coord, Size size)
{
- return new Rectangle(coord.X * res, coord.Y * res, coord.X * res + res, coord.Y * res + res);
+ int resx = size.Width / Game.GameSize.X;
+ int resy = size.Height / Game.GameSize.Y;
+ return new Rectangle(coord.X * resx, coord.Y * resy, resx, resy);
}
}
public struct SqCoord