2015.02.12

This commit is contained in:
Norbi Peti 2017-01-07 23:14:03 +01:00
parent 7068b4cc22
commit 921ac407e1
6 changed files with 118 additions and 51 deletions

2
.gitignore vendored
View file

@ -132,7 +132,7 @@ publish/
# NuGet Packages Directory # NuGet Packages Directory
## TODO: If you have NuGet Package Restore enabled, uncomment the next line ## TODO: If you have NuGet Package Restore enabled, uncomment the next line
#packages/ packages/
# Windows Azure Build Output # Windows Azure Build Output
csx csx

View file

@ -42,6 +42,7 @@
this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 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.Left)
| System.Windows.Forms.AnchorStyles.Right))); | System.Windows.Forms.AnchorStyles.Right)));
this.panel1.BackColor = System.Drawing.Color.Black;
this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panel1.Location = new System.Drawing.Point(12, 41); this.panel1.Location = new System.Drawing.Point(12, 41);
this.panel1.Name = "panel1"; this.panel1.Name = "panel1";
@ -76,6 +77,7 @@
this.newSingleplayerGameToolStripMenuItem.Name = "newSingleplayerGameToolStripMenuItem"; this.newSingleplayerGameToolStripMenuItem.Name = "newSingleplayerGameToolStripMenuItem";
this.newSingleplayerGameToolStripMenuItem.Size = new System.Drawing.Size(197, 22); this.newSingleplayerGameToolStripMenuItem.Size = new System.Drawing.Size(197, 22);
this.newSingleplayerGameToolStripMenuItem.Text = "New singleplayer game"; this.newSingleplayerGameToolStripMenuItem.Text = "New singleplayer game";
this.newSingleplayerGameToolStripMenuItem.Click += new System.EventHandler(this.newSingleplayerGameToolStripMenuItem_Click);
// //
// newMultiplayerGameToolStripMenuItem // newMultiplayerGameToolStripMenuItem
// //

View file

@ -12,7 +12,21 @@ namespace SnakeGame
{ {
public partial class Form1 : Form 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() public Form1()
{ {
InitializeComponent(); InitializeComponent();
@ -32,9 +46,10 @@ namespace SnakeGame
Timer.Stop(); Timer.Stop();
Game.Refresh(); Game.Refresh();
Timer.Interval = Game.UpdateTime; Timer.Interval = Game.UpdateTime;
if (TimerEnabled)
Timer.Start(); Timer.Start();
}; };
Timer.Start(); //Timer.Start();
} }
private void Form1_Load(object sender, EventArgs e) private void Form1_Load(object sender, EventArgs e)
@ -58,5 +73,10 @@ namespace SnakeGame
else if (e.KeyCode == Keys.Right) else if (e.KeyCode == Keys.Right)
Game.MoveDirection = Direction.Right; Game.MoveDirection = Direction.Right;
} }
private void newSingleplayerGameToolStripMenuItem_Click(object sender, EventArgs e)
{
Game.Start(GameStartMode.SinglePlayer);
}
} }
} }

View file

@ -10,11 +10,17 @@ namespace SnakeGame
{ {
public static class Game public static class Game
{ {
/// <summary>
/// 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
/// </summary>
public static SqCoord GameSize; public static SqCoord GameSize;
public static List<SqCoord> GameField; //public static List<SqCoord> GameField;
public static int[,] GameField;
public static SqCoord PlayerPos; public static SqCoord PlayerPos;
public static int Length = 4; public static int Length = 4;
public static int UpdateTime = 2000; public static int UpdateTime = 1000;
public static Direction MoveDirection; public static Direction MoveDirection;
public static void Start(GameStartMode mode) public static void Start(GameStartMode mode)
@ -22,6 +28,8 @@ namespace SnakeGame
switch(mode) switch(mode)
{ {
case GameStartMode.SinglePlayer: case GameStartMode.SinglePlayer:
Game.Reset();
Form1.TimerEnabled = true;
break; break;
default: default:
throw new ArgumentException(); throw new ArgumentException();
@ -31,21 +39,11 @@ namespace SnakeGame
public static void Load(Size size) public static void Load(Size size)
{ {
//GameSize = size; //GameSize = size;
GameSize = SquareCoord.PointToSqCoord(new Point(size)); //GameSize = SquareCoord.PointToSqCoord(new Point(size));
GameField = new List<SqCoord>(GameSize.X * GameSize.Y); //GameSize = new SqCoord { X = size.Width / 20, Y = size.Height / 20 };
PlayerPos = new SqCoord { X = GameSize.X / 2, Y = 1 }; //GameField = new List<SqCoord>(GameSize.X * GameSize.Y);
for (int i = 0; i < GameSize.X; i++) //GameField = new int[GameSize.X, GameSize.Y];
{ Game.Reset();
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);
}
}
//GameField.Single(entry => entry.X == PlayerPos.X && entry.Y == PlayerPos.Y).Tick; //GameField.Single(entry => entry.X == PlayerPos.X && entry.Y == PlayerPos.Y).Tick;
/*for (int i = 0; i < GameField.Count; i++) /*for (int i = 0; i < GameField.Count; i++)
{ {
@ -54,8 +52,34 @@ namespace SnakeGame
GameField[i].Tick = Length; 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; MoveDirection = Direction.Down;
GameRenderer.Render();
} }
public static void Refresh() public static void Refresh()
@ -63,10 +87,16 @@ namespace SnakeGame
//Decrease any positive Ticks; if next player position is other than zero, game over //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 //Otherwise set next player position and set Tick on player position to current Length
//Console.WriteLine("Refreshing..."); //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) for (int j = 0; j < GameField.GetLength(1); j++)
GameField[i] = new SqCoord { X = GameField[i].X, Y = GameField[i].Y, Tick = GameField[i].Tick - 1 }; {
/*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; SqCoord nextcoord;
switch (MoveDirection) switch (MoveDirection)
@ -87,27 +117,35 @@ namespace SnakeGame
nextcoord = PlayerPos; nextcoord = PlayerPos;
break; break;
} }
if (Game.GetCoord(nextcoord).Tick != 0) //if (Game.GetCoord(nextcoord).Tick != 0)
if (Game.GameField[nextcoord.X, nextcoord.Y] != 0)
Stop(); 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 }; for (int j = 0; j < GameField.GetLength(1); j++)
PlayerPos = GameField[i]; {
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); return GameField.Single(entry => entry.X == nextcoord.X && entry.Y == nextcoord.Y);
} }*/
public static void Stop() public static void Stop()
{ {
Form1.Timer.Stop(); //Form1.Timer.Stop();
Form1.TimerEnabled = false;
MessageBox.Show("Game over!"); MessageBox.Show("Game over!");
} }
} }

View file

@ -13,20 +13,25 @@ namespace SnakeGame
public static Panel Panel; public static Panel Panel;
public static void Render() 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) for (int j = 0; j < Game.GameField.GetLength(1); j++)
RenderSquare(coord, Color.Red); {
else if (coord.Tick == 0) //if (coord.Tick == -1)
RenderSquare(coord, Color.Black); 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 else
RenderSquare(coord, Color.Green); RenderSquare(new SqCoord { X = i, Y = j }, Color.Green);
}
} }
} }
private static void RenderSquare(SqCoord coord, Color color) private static void RenderSquare(SqCoord coord, Color color)
{ {
Graphics gr = Panel.CreateGraphics(); Graphics gr = Panel.CreateGraphics();
gr.FillRectangle(new SolidBrush(color), SquareCoord.SqCoordToRect(coord)); gr.FillRectangle(new SolidBrush(color), SquareCoord.SqCoordToRect(coord, Panel.Size));
} }
} }
} }

View file

@ -9,7 +9,7 @@ namespace SnakeGame
{ {
public static class SquareCoord public static class SquareCoord
{ {
private static int x; /*private static int x;
public static int X public static int X
{ {
get get
@ -34,20 +34,22 @@ namespace SnakeGame
y = value; y = value;
Game.Refresh(); 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); 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 }; 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 public struct SqCoord