2015.02.14

This commit is contained in:
Norbi Peti 2017-01-07 23:15:15 +01:00
parent 921ac407e1
commit 40f0dd7b4a
9 changed files with 382 additions and 46 deletions

View file

@ -34,6 +34,9 @@
this.newSingleplayerGameToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.newSingleplayerGameToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.newMultiplayerGameToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.newMultiplayerGameToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.joinMultiplayerGameToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.joinMultiplayerGameToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.scoreLabel = new System.Windows.Forms.Label();
this.livesLabel = new System.Windows.Forms.Label();
this.DialogPanel = new System.Windows.Forms.Panel();
this.menuStrip1.SuspendLayout(); this.menuStrip1.SuspendLayout();
this.SuspendLayout(); this.SuspendLayout();
// //
@ -44,9 +47,9 @@
| System.Windows.Forms.AnchorStyles.Right))); | System.Windows.Forms.AnchorStyles.Right)));
this.panel1.BackColor = System.Drawing.Color.Black; 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, 64);
this.panel1.Name = "panel1"; this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(600, 380); this.panel1.Size = new System.Drawing.Size(600, 357);
this.panel1.TabIndex = 0; this.panel1.TabIndex = 0;
this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint); this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
// //
@ -75,7 +78,8 @@
this.newSingleplayerGameToolStripMenuItem.BackColor = System.Drawing.Color.Black; this.newSingleplayerGameToolStripMenuItem.BackColor = System.Drawing.Color.Black;
this.newSingleplayerGameToolStripMenuItem.ForeColor = System.Drawing.Color.White; this.newSingleplayerGameToolStripMenuItem.ForeColor = System.Drawing.Color.White;
this.newSingleplayerGameToolStripMenuItem.Name = "newSingleplayerGameToolStripMenuItem"; this.newSingleplayerGameToolStripMenuItem.Name = "newSingleplayerGameToolStripMenuItem";
this.newSingleplayerGameToolStripMenuItem.Size = new System.Drawing.Size(197, 22); this.newSingleplayerGameToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.N)));
this.newSingleplayerGameToolStripMenuItem.Size = new System.Drawing.Size(240, 22);
this.newSingleplayerGameToolStripMenuItem.Text = "New singleplayer game"; this.newSingleplayerGameToolStripMenuItem.Text = "New singleplayer game";
this.newSingleplayerGameToolStripMenuItem.Click += new System.EventHandler(this.newSingleplayerGameToolStripMenuItem_Click); this.newSingleplayerGameToolStripMenuItem.Click += new System.EventHandler(this.newSingleplayerGameToolStripMenuItem_Click);
// //
@ -84,29 +88,68 @@
this.newMultiplayerGameToolStripMenuItem.BackColor = System.Drawing.Color.Black; this.newMultiplayerGameToolStripMenuItem.BackColor = System.Drawing.Color.Black;
this.newMultiplayerGameToolStripMenuItem.ForeColor = System.Drawing.Color.White; this.newMultiplayerGameToolStripMenuItem.ForeColor = System.Drawing.Color.White;
this.newMultiplayerGameToolStripMenuItem.Name = "newMultiplayerGameToolStripMenuItem"; this.newMultiplayerGameToolStripMenuItem.Name = "newMultiplayerGameToolStripMenuItem";
this.newMultiplayerGameToolStripMenuItem.Size = new System.Drawing.Size(197, 22); this.newMultiplayerGameToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)(((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.Shift)
| System.Windows.Forms.Keys.N)));
this.newMultiplayerGameToolStripMenuItem.Size = new System.Drawing.Size(269, 22);
this.newMultiplayerGameToolStripMenuItem.Text = "New multiplayer game"; this.newMultiplayerGameToolStripMenuItem.Text = "New multiplayer game";
// //
// joinMultiplayerGameToolStripMenuItem // joinMultiplayerGameToolStripMenuItem
// //
this.joinMultiplayerGameToolStripMenuItem.BackColor = System.Drawing.Color.Black; this.joinMultiplayerGameToolStripMenuItem.BackColor = System.Drawing.Color.Black;
this.joinMultiplayerGameToolStripMenuItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
this.joinMultiplayerGameToolStripMenuItem.ForeColor = System.Drawing.Color.White; this.joinMultiplayerGameToolStripMenuItem.ForeColor = System.Drawing.Color.White;
this.joinMultiplayerGameToolStripMenuItem.Name = "joinMultiplayerGameToolStripMenuItem"; this.joinMultiplayerGameToolStripMenuItem.Name = "joinMultiplayerGameToolStripMenuItem";
this.joinMultiplayerGameToolStripMenuItem.Size = new System.Drawing.Size(197, 22); this.joinMultiplayerGameToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)(((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.Alt)
| System.Windows.Forms.Keys.N)));
this.joinMultiplayerGameToolStripMenuItem.Size = new System.Drawing.Size(269, 22);
this.joinMultiplayerGameToolStripMenuItem.Text = "Join multiplayer game"; this.joinMultiplayerGameToolStripMenuItem.Text = "Join multiplayer game";
// //
// scoreLabel
//
this.scoreLabel.AutoSize = true;
this.scoreLabel.Location = new System.Drawing.Point(14, 31);
this.scoreLabel.Name = "scoreLabel";
this.scoreLabel.Size = new System.Drawing.Size(35, 13);
this.scoreLabel.TabIndex = 2;
this.scoreLabel.Text = "Score";
//
// livesLabel
//
this.livesLabel.AutoSize = true;
this.livesLabel.Location = new System.Drawing.Point(14, 48);
this.livesLabel.Name = "livesLabel";
this.livesLabel.Size = new System.Drawing.Size(32, 13);
this.livesLabel.TabIndex = 3;
this.livesLabel.Text = "Lives";
//
// DialogPanel
//
this.DialogPanel.Anchor = System.Windows.Forms.AnchorStyles.None;
this.DialogPanel.AutoSize = true;
this.DialogPanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.DialogPanel.Location = new System.Drawing.Point(200, 228);
this.DialogPanel.Name = "DialogPanel";
this.DialogPanel.Size = new System.Drawing.Size(200, 100);
this.DialogPanel.TabIndex = 0;
this.DialogPanel.Visible = false;
//
// Form1 // Form1
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.Black; this.BackColor = System.Drawing.Color.Black;
this.ClientSize = new System.Drawing.Size(624, 442); this.ClientSize = new System.Drawing.Size(624, 442);
this.Controls.Add(this.DialogPanel);
this.Controls.Add(this.livesLabel);
this.Controls.Add(this.scoreLabel);
this.Controls.Add(this.menuStrip1); this.Controls.Add(this.menuStrip1);
this.Controls.Add(this.panel1); this.Controls.Add(this.panel1);
this.ForeColor = System.Drawing.Color.White; this.ForeColor = System.Drawing.Color.White;
this.Name = "Form1"; this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Snake Game"; this.Text = "Snake Game";
this.Activated += new System.EventHandler(this.Form1_Activated);
this.Deactivate += new System.EventHandler(this.Form1_Deactivate);
this.Load += new System.EventHandler(this.Form1_Load); this.Load += new System.EventHandler(this.Form1_Load);
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown); this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
this.menuStrip1.ResumeLayout(false); this.menuStrip1.ResumeLayout(false);
@ -124,6 +167,9 @@
private System.Windows.Forms.ToolStripMenuItem newSingleplayerGameToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem newSingleplayerGameToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem newMultiplayerGameToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem newMultiplayerGameToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem joinMultiplayerGameToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem joinMultiplayerGameToolStripMenuItem;
private System.Windows.Forms.Label scoreLabel;
private System.Windows.Forms.Label livesLabel;
private System.Windows.Forms.Panel DialogPanel;
} }
} }

View file

@ -13,7 +13,9 @@ namespace SnakeGame
public partial class Form1 : Form public partial class Form1 : Form
{ {
private static Timer Timer; private static Timer Timer;
private static Timer SpeedTimer;
private static bool timerenabled = false; private static bool timerenabled = false;
public static Form1 Instance;
public static bool TimerEnabled public static bool TimerEnabled
{ {
get get
@ -23,7 +25,10 @@ namespace SnakeGame
set set
{ {
if (value && !timerenabled) //Only start if not running already if (value && !timerenabled) //Only start if not running already
{
Timer.Start(); Timer.Start();
SpeedTimer.Start();
}
timerenabled = value; timerenabled = value;
} }
} }
@ -39,6 +44,10 @@ namespace SnakeGame
menuStrip1.Renderer = new ToolStripProfessionalRenderer(new MenuColorTable()); menuStrip1.Renderer = new ToolStripProfessionalRenderer(new MenuColorTable());
menuStrip1.ForeColor = Color.White; menuStrip1.ForeColor = Color.White;
GameRenderer.Panel = panel1; GameRenderer.Panel = panel1;
Game.ScoreLabel = scoreLabel;
Game.LivesLabel = livesLabel;
Game.DialogPanel = DialogPanel;
Instance = this;
Timer = new Timer(); Timer = new Timer();
Timer.Interval = Game.UpdateTime; Timer.Interval = Game.UpdateTime;
Timer.Tick += delegate Timer.Tick += delegate
@ -50,6 +59,16 @@ namespace SnakeGame
Timer.Start(); Timer.Start();
}; };
//Timer.Start(); //Timer.Start();
SpeedTimer = new Timer();
SpeedTimer.Interval = 10000;
SpeedTimer.Tick += delegate
{
if (Game.UpdateTime - 100 > 0)
Game.UpdateTime -= 100;
Game.Length++;
if (!TimerEnabled)
SpeedTimer.Stop();
};
} }
private void Form1_Load(object sender, EventArgs e) private void Form1_Load(object sender, EventArgs e)
@ -72,11 +91,30 @@ namespace SnakeGame
Game.MoveDirection = Direction.Left; Game.MoveDirection = Direction.Left;
else if (e.KeyCode == Keys.Right) else if (e.KeyCode == Keys.Right)
Game.MoveDirection = Direction.Right; Game.MoveDirection = Direction.Right;
else if (e.KeyCode == Keys.Enter)
{
MSGBox.CloseMSGBox();
}
Game.Refresh();
} }
private void newSingleplayerGameToolStripMenuItem_Click(object sender, EventArgs e) private void newSingleplayerGameToolStripMenuItem_Click(object sender, EventArgs e)
{ {
Game.Start(GameStartMode.SinglePlayer); Game.Start(GameStartMode.SinglePlayer);
} }
private bool formdeactivated = false;
private void Form1_Activated(object sender, EventArgs e)
{
if (formdeactivated)
Game.Paused = false;
formdeactivated = false;
}
private void Form1_Deactivate(object sender, EventArgs e)
{
formdeactivated = !Game.Paused;
Game.Paused = true;
}
} }
} }

View file

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
@ -15,21 +16,77 @@ namespace SnakeGame
/// Replace MessageBox with own GUI; Add GUI to get GameSize... /// 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 /// Maybe select region to set one square's size and/or visualize changes when user stops interaction
/// </summary> /// </summary>
public static SqCoord GameSize; public static Point GameSize;
//public static List<SqCoord> GameField; //public static List<SqCoord> GameField;
public static int[,] GameField; public static SqCoord[,] GameField;
public static SqCoord PlayerPos; public static Point PlayerPos;
public static int Length = 4; public static int Length = 4;
public static int UpdateTime = 1000; public static int UpdateTime = 800;
public static Direction MoveDirection; public static Direction MoveDirection;
public static Label ScoreLabel;
public static Label LivesLabel;
public static Panel DialogPanel;
private static int score;
public static int Score
{
get
{
return score;
}
set
{
score = value;
ScoreLabel.Text = "Score: " + score;
}
}
private static int lives;
public static int Lives
{
get
{
return lives;
}
set
{
lives = value;
LivesLabel.Text = "Lives: " + lives;
}
}
/// <summary>
/// Opposite of Form1.TimerEnabled
/// </summary>
public static bool Paused
{
get
{
return !Form1.TimerEnabled;
}
set
{
Form1.TimerEnabled = !value;
}
}
public static void Start(GameStartMode mode) public static void Start(GameStartMode mode)
{ {
switch(mode) switch(mode)
{ {
case GameStartMode.SinglePlayer: case GameStartMode.SinglePlayer:
MSGBox.ShowMSGBox("New singleplayer game", "Size (X Y):", MSGBoxType.SizeInput, new EventHandler<int>(delegate(object s, int input)
{
//string[] strs=input.Split(' ');
//int x, y;
//if (strs.Length == 2 && int.TryParse(strs[0], out x) && int.TryParse(strs[1], out y))
if (input > 5)
{
//GameSize = new Point(x, y);
GameRenderer.Panel.CreateGraphics().FillRectangle(new SolidBrush(Color.Black), new Rectangle(new Point(), GameRenderer.Panel.Size));
int xy=GameRenderer.Panel.Size.Width/input;
GameSize = new Point(xy, xy);
Game.Reset(); Game.Reset();
Form1.TimerEnabled = true; Form1.TimerEnabled = true;
}
}));
break; break;
default: default:
throw new ArgumentException(); throw new ArgumentException();
@ -43,6 +100,7 @@ namespace SnakeGame
//GameSize = new SqCoord { X = size.Width / 20, Y = size.Height / 20 }; //GameSize = new SqCoord { X = size.Width / 20, Y = size.Height / 20 };
//GameField = new List<SqCoord>(GameSize.X * GameSize.Y); //GameField = new List<SqCoord>(GameSize.X * GameSize.Y);
//GameField = new int[GameSize.X, GameSize.Y]; //GameField = new int[GameSize.X, GameSize.Y];
GameSize = new Point { X = size.Width / 20, Y = size.Height / 20 };
Game.Reset(); Game.Reset();
//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++)
@ -55,12 +113,19 @@ namespace SnakeGame
//GameRenderer.Render(); - It has no effect in loading part //GameRenderer.Render(); - It has no effect in loading part
} }
public static void Reset() public static void Reset(bool fullreset)
{ {
Size size = GameRenderer.Panel.Size; Size size = GameRenderer.Panel.Size;
GameSize = new SqCoord { X = size.Width / 20, Y = size.Height / 20 }; //GameSize = new Point { X = size.Width / 20, Y = size.Height / 20 };
GameField = new int[GameSize.X, GameSize.Y]; GameField = new SqCoord[GameSize.X, GameSize.Y];
PlayerPos = new SqCoord { X = GameSize.X / 2, Y = 1 }; PlayerPos = new Point { X = GameSize.X / 2, Y = 1 };
if (fullreset)
{
Score = 0;
Lives = 3;
UpdateTime = 800;
Length = 4;
}
for (int i = 0; i < GameSize.X; i++) for (int i = 0; i < GameSize.X; i++)
{ {
for (int j = 0; j < GameSize.Y; j++) for (int j = 0; j < GameSize.Y; j++)
@ -72,71 +137,110 @@ namespace SnakeGame
coord.Tick = 4; coord.Tick = 4;
GameField.Add(coord);*/ GameField.Add(coord);*/
if (i == 0 || j == 0 || i == GameSize.X - 1 || j == GameSize.Y - 1) if (i == 0 || j == 0 || i == GameSize.X - 1 || j == GameSize.Y - 1)
GameField[i, j] = -1; {
GameField[i, j].Type = SquareType.Wall;
GameField[i, j].Tick = -1;
}
else if (i == PlayerPos.X && j == PlayerPos.Y) else if (i == PlayerPos.X && j == PlayerPos.Y)
GameField[i, j] = 4; {
else GameField[i, j].Type = SquareType.Player;
GameField[i, j] = 0; GameField[i, j].Tick = Length;
} }
} }
}
Game.AddPoint();
MoveDirection = Direction.Down; MoveDirection = Direction.Down;
} }
public static void Reset()
{
Reset(true);
}
public static void Refresh() public static void Refresh()
{ {
//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++)
if (!Form1.TimerEnabled)
return; //Not playing currently
for (int i = 0; i < GameField.GetLength(0); i++) for (int i = 0; i < GameField.GetLength(0); i++)
{ {
for (int j = 0; j < GameField.GetLength(1); j++) for (int j = 0; j < GameField.GetLength(1); j++)
{ {
/*if (GameField[i].Tick > 0) /*if (GameField[i].Tick > 0)
GameField[i] = new SqCoord { X = GameField[i].X, Y = GameField[i].Y, Tick = GameField[i].Tick - 1 };*/ GameField[i] = new SqCoord { X = GameField[i].X, Y = GameField[i].Y, Tick = GameField[i].Tick - 1 };*/
if (GameField[i, j] > 0) if (GameField[i, j].Tick > 0)
GameField[i, j]--; GameField[i, j].Tick--;
} }
} }
SqCoord nextcoord; Point nextcoord;
switch (MoveDirection) switch (MoveDirection)
{ {
case Direction.Down: case Direction.Down:
nextcoord = new SqCoord { X = PlayerPos.X, Y = PlayerPos.Y + 1 }; nextcoord = new Point { X = PlayerPos.X, Y = PlayerPos.Y + 1 };
break; break;
case Direction.Left: case Direction.Left:
nextcoord = new SqCoord { X = PlayerPos.X - 1, Y = PlayerPos.Y }; nextcoord = new Point { X = PlayerPos.X - 1, Y = PlayerPos.Y };
break; break;
case Direction.Right: case Direction.Right:
nextcoord = new SqCoord { X = PlayerPos.X + 1, Y = PlayerPos.Y }; nextcoord = new Point { X = PlayerPos.X + 1, Y = PlayerPos.Y };
break; break;
case Direction.Up: case Direction.Up:
nextcoord = new SqCoord { X = PlayerPos.X, Y = PlayerPos.Y - 1 }; nextcoord = new Point { X = PlayerPos.X, Y = PlayerPos.Y - 1 };
break; break;
default: default:
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) if (Game.GameField[nextcoord.X, nextcoord.Y].Tick != 0 && Game.GameField[nextcoord.X, nextcoord.Y].Type != SquareType.Point)
{
Lives--;
if (Lives == 0)
Stop(); Stop();
else
Reset(false);
}
else else
{ {
for (int i = 0; i < GameField.GetLength(0); i++) /*for (int i = 0; i < GameField.GetLength(0); i++)
{ {
for (int j = 0; j < GameField.GetLength(1); j++) for (int j = 0; j < GameField.GetLength(1); j++)
{ {
if (i == nextcoord.X && j == nextcoord.Y) if (i == nextcoord.X && j == nextcoord.Y)
{*/
if (GameField[nextcoord.X, nextcoord.Y].Type == SquareType.Point)
{ {
GameField[nextcoord.X, nextcoord.Y] = Length; Score += 1000;
PlayerPos = new SqCoord { X = i, Y = j }; Game.AddPoint();
}
} }
GameField[nextcoord.X, nextcoord.Y].Tick = Length;
GameField[nextcoord.X, nextcoord.Y].Type = SquareType.Player;
PlayerPos = new Point { X = nextcoord.X, Y = nextcoord.Y };
if (Score > 0)
Score--;
/* }
} }
}*/
GameRenderer.Render(); GameRenderer.Render();
} }
} }
public static void AddPoint()
{
Random rand = new Random();
int x, y;
do
{
x = rand.Next(GameField.GetLength(0) - 1);
y = rand.Next(GameField.GetLength(1) - 1);
} while (GameField[x, y].Tick != 0 && GameField[x, y].Type == SquareType.Wall);
GameField[x, y].Tick = -1;
GameField[x, y].Type = SquareType.Point;
}
/*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);
@ -145,8 +249,9 @@ namespace SnakeGame
public static void Stop() public static void Stop()
{ {
//Form1.Timer.Stop(); //Form1.Timer.Stop();
Form1.TimerEnabled = false; //Form1.TimerEnabled = false;
MessageBox.Show("Game over!"); //MessageBox.Show("Game over!");
MSGBox.ShowMSGBox("Game over!", "", MSGBoxType.Text, pauseafter: true);
} }
} }
public enum GameStartMode public enum GameStartMode

View file

@ -19,16 +19,21 @@ namespace SnakeGame
for (int j = 0; j < Game.GameField.GetLength(1); j++) for (int j = 0; j < Game.GameField.GetLength(1); j++)
{ {
//if (coord.Tick == -1) //if (coord.Tick == -1)
if (Game.GameField[i, j] == -1) //if (Game.GameField[i, j].Tick == -1)
RenderSquare(new SqCoord { X = i, Y = j }, Color.Red); if (Game.GameField[i, j].Tick == 0)
else if (Game.GameField[i, j] == 0) RenderSquare(new Point { X = i, Y = j }, Color.Black);
RenderSquare(new SqCoord { X = i, Y = j }, Color.Black); else if (Game.GameField[i, j].Type == SquareType.Wall)
RenderSquare(new Point { X = i, Y = j }, Color.Red);
else if (Game.GameField[i, j].Type == SquareType.Player)
RenderSquare(new Point { X = i, Y = j }, Color.Green);
else if (Game.GameField[i, j].Type == SquareType.Point)
RenderSquare(new Point { X = i, Y = j }, Color.Blue);
else else
RenderSquare(new SqCoord { X = i, Y = j }, Color.Green); RenderSquare(new Point { X = i, Y = j }, Color.Gray);
} }
} }
} }
private static void RenderSquare(SqCoord coord, Color color) private static void RenderSquare(Point coord, Color color)
{ {
Graphics gr = Panel.CreateGraphics(); Graphics gr = Panel.CreateGraphics();
gr.FillRectangle(new SolidBrush(color), SquareCoord.SqCoordToRect(coord, Panel.Size)); gr.FillRectangle(new SolidBrush(color), SquareCoord.SqCoordToRect(coord, Panel.Size));

100
SnakeGame/MSGBox.cs Normal file
View file

@ -0,0 +1,100 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SnakeGame
{
public static class MSGBox
{
private static int slidervalue;
private static bool pause;
public static void ShowMSGBox(string text, string inputtext, MSGBoxType type, EventHandler<int> doneevent = null, bool pauseafter = false)
{
Game.DialogPanel.Controls.Add(new Label { Text = text, Location = new Point(10, 10), Font = new Font(FontFamily.GenericSansSerif, 10f), Size = new Size(200, 20) });
//TextBox input = null;
switch (type)
{
case MSGBoxType.Text:
Game.DialogPanel.Size = new Size(200, 100);
slidervalue = 0;
break;
case MSGBoxType.SizeInput:
Game.DialogPanel.Size = new Size(200, 200);
Game.DialogPanel.Controls.Add(new Label { Text = inputtext, Location = new Point(10, 40), Size = new Size(100, 20) });
slidervalue = 20;
/*Game.DialogPanel.Controls.Add(input = new TextBox
{
Text = "",
Location = new Point(10, 60),
Multiline = false,
Font = new Font(FontFamily.GenericSansSerif, 10f),
Size = new Size(100, 1)
});*/
Graphics gr = Game.DialogPanel.CreateGraphics();
gr.FillRectangle(new SolidBrush(Color.Blue), new Rectangle(10, 60, slidervalue, slidervalue));
Game.DialogPanel.Paint += delegate
{
if (slidervalue == 0)
return;
gr.FillRectangle(new SolidBrush(Color.Blue), new Rectangle(10, 60, slidervalue, slidervalue));
};
Game.DialogPanel.Click += delegate
{
if (slidervalue == 0)
return;
Point p = Game.DialogPanel.PointToClient(Cursor.Position);
if (p.Y > 50 && p.Y < 200)
{
if (p.X < 210) //p.X - 10 < 100
slidervalue = p.X - 10;
Game.DialogPanel.Refresh();
}
};
break;
default:
throw new NotImplementedException();
}
Game.DialogPanel.Location = new Point(GameRenderer.Panel.Width / 2 - Game.DialogPanel.Width / 2, GameRenderer.Panel.Height / 2 - Game.DialogPanel.Height / 2);
Game.DialogPanel.Location = Game.DialogPanel.Parent.PointToClient(GameRenderer.Panel.PointToScreen(Game.DialogPanel.Location));
Button btn = new Button();
btn.Text = "OK";
btn.Click += delegate
{
CloseMSGBox();
if (doneevent != null)
//doneevent(btn, (input == null ? "" : input.Text));
doneevent(btn, slidervalue);
};
btn.FlatStyle = FlatStyle.Flat;
btn.FlatAppearance.BorderColor = Color.Blue;
btn.FlatAppearance.BorderSize = 2;
btn.FlatAppearance.CheckedBackColor = Color.DarkBlue;
btn.FlatAppearance.MouseDownBackColor = Color.DeepSkyBlue;
btn.Location = new Point(Game.DialogPanel.Size.Width / 2 - 40, Game.DialogPanel.Size.Height - 40);
btn.Size = new Size(80, 30);
Game.DialogPanel.Controls.Add(btn);
Game.Paused = true;
pause = pauseafter;
Game.DialogPanel.Visible = true;
}
public static void CloseMSGBox()
{
Game.DialogPanel.Visible = false;
Game.DialogPanel.Controls.Clear();
Game.Paused = pause;
//Form1.Instance.Activate();
//GameRenderer.Panel.Select();
//Form1.Instance.Select();
Form1.Instance.Focus();
}
}
public enum MSGBoxType
{
Text,
SizeInput
}
}

18
SnakeGame/Network.cs Normal file
View file

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace SnakeGame
{
public static class Network
{
public static void SyncUpdate()
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://snakegame.16mb.com/");
request.Method = "POST";
}
}
}

View file

@ -53,6 +53,8 @@
<Compile Include="Game.cs" /> <Compile Include="Game.cs" />
<Compile Include="GameRenderer.cs" /> <Compile Include="GameRenderer.cs" />
<Compile Include="MenuColorTable.cs" /> <Compile Include="MenuColorTable.cs" />
<Compile Include="MSGBox.cs" />
<Compile Include="Network.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SquareCoord.cs" /> <Compile Include="SquareCoord.cs" />

View file

@ -45,7 +45,7 @@ namespace SnakeGame
{ {
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, Size size) public static Rectangle SqCoordToRect(Point coord, Size size)
{ {
int resx = size.Width / Game.GameSize.X; int resx = size.Width / Game.GameSize.X;
int resy = size.Height / Game.GameSize.Y; int resy = size.Height / Game.GameSize.Y;
@ -54,11 +54,22 @@ namespace SnakeGame
} }
public struct SqCoord public struct SqCoord
{ {
public int X { get; set; } //public int X { get; set; } - using it in two-dimensional array; replaced with Point where needed
public int Y { get; set; } //public int Y { get; set; }
/// <summary> /// <summary>
/// Used to determine snake square "expiration". -1 means wall. Game over if other than zero in next player position. /// Used to determine snake square "expiration". Game over if other than zero or there is a wall.
/// </summary> /// </summary>
public int Tick { get; set; } public int Tick { get; set; }
public SquareType Type { get; set; }
public int PlayerID { get; set; }
}
/// <summary>
/// Only consider if Tick>0
/// </summary>
public enum SquareType
{
Wall,
Point,
Player
} }
} }

11
Web/index.php Normal file
View file

@ -0,0 +1,11 @@
<?php
if(!isset($_POST['client']) || $_POST['client']=="")
{
echo "Welcome!<br>
<a>Project home</a>";
die();
}
//WebPass: vhwfsQOv9E
//fötöpö: (sznpadmin) VKiSh7i1og
?>