private FullSnake _Snake; // Temporary snake. #endregion Fields #region Constructors /* * Constructor of NetworkCOntainer * - Initialize variables. * */ public NetworkContainer() { _Msg = "000"; _Snake = new FullSnake(); _Fruit = new Fruit(); _Insect = new Insect(); _ListWalls = new ListWalls(); _Nickname = ""; _Score = 0; _HasBeenModified = false; }
private int _Y; // Position in Y. #endregion Fields #region Constructors /* * Constructor of Wall * - Initialize variables * */ public Wall(int width, int height, FullSnake snake, Fruit fruit, Insect insect, List<Wall> listWall) { int tmpX, tmpY; _Side = width / 54 - 2; _RandomNumber = new Random(); tmpX = Generate_X(width); tmpY = Generate_Y(height); while(!CheckPositions(tmpX,tmpY,snake,fruit,insect,listWall)) { tmpX = Generate_X(width); tmpY = Generate_Y(height); } _X = tmpX; _Y = tmpY; }
/* * Check if temporary X & Y are on the snake, the fruit, the insect, or a wall * */ private Boolean CheckPositions(int x, int y, FullSnake fullSnake, Fruit fruit, Insect insect, List<Wall> listWalls) { Boolean ok = true; for (int i = 0; i < fullSnake.Get_SnakeSize(); i++) { if ((x == fullSnake.Get_Snake()[i].Get_X()) && (y == fullSnake.Get_Snake()[i].Get_Y())) { ok = false; //Console.WriteLine("Wall appeared on the snake"); } } if (x == fruit.Get_X() && y == fruit.Get_Y()) { ok = false; //Console.WriteLine("Wall appeared on the fruit"); } if (((insect.Get_X() == x) && (insect.Get_Y() == y)) || ((insect.Get_X() == (x + (_Side / 2 + 1))) && (insect.Get_Y() == y)) || ((insect.Get_X() == x) && (insect.Get_Y() == (y + (_Side / 2) + 1))) || ((insect.Get_X() == (x + (_Side / 2 + 1))) && (insect.Get_Y() == (y + (_Side / 2) + 1)))) { ok = false; //Console.WriteLine("Wall appeared on the insect"); } if(listWalls != null) for (int z = 0; z < listWalls.Count(); z++) { if ((x == listWalls[z].Get_X()) && (z == listWalls[z].Get_Y())) { ok = false; //Console.WriteLine("Wall appeared on a wall"); } } return ok; }
/* * Move fruit * - Move Position of the fruit. * - Check if new positions are on the snake, regenerate if needed. * */ public void MoveFruit(int width, int height, FullSnake fullSnake) { int tmpX; int tmpY; tmpX = Generate_X(width); tmpY = Generate_Y(height); while (!CheckPositions(tmpX, tmpY, fullSnake)) { tmpX = Generate_X(width); tmpY = Generate_Y(height); } _X = tmpX; _Y = tmpY; }
/* * Check if temporary X & Y are on the snake or a wall (multiplayers) * */ private Boolean CheckPositions(int x, int y, FullSnake fullSnake, List<Wall> listWalls) { Boolean ok = true; for (int i = 0; i < fullSnake.Get_SnakeSize(); i++) { if ((x == fullSnake.Get_Snake()[i].Get_X()) && (y == fullSnake.Get_Snake()[i].Get_Y())) { ok = false; //Console.WriteLine("Fruit appeared on the snake"); } } foreach (Wall element in listWalls) { if (element.Get_X() == x && element.Get_Y() == y) { ok = false; //Console.WriteLine("Fruit appeared on a wall"); } } return ok; }
/* * Check if temporary X & Y are on the snake * */ private Boolean CheckPositions(int x, int y, FullSnake fullSnake) { Boolean ok = true; for (int i = 0; i < fullSnake.Get_SnakeSize(); i++) { if ((x == fullSnake.Get_Snake()[i].Get_X()) && (y == fullSnake.Get_Snake()[i].Get_Y())) { ok = false; //Console.WriteLine("Fruit appeared on the snake"); } } return ok; }
/* * Check if temporary X & Y are on the snake, the fruit, or a wall (multiplayers) * */ private Boolean CheckPositions(int x, int y, FullSnake snake, Fruit fruit, List<Wall> listWalls) { Boolean ok = true; for (int i = 0; i < snake.Get_SnakeSize(); i++) { if (((snake.Get_Snake()[i].Get_X() == x) && (snake.Get_Snake()[i].Get_Y() == y)) || ((snake.Get_Snake()[i].Get_X() == (x + (_Side / 2 + 1))) && (snake.Get_Snake()[i].Get_Y() == y)) || ((snake.Get_Snake()[i].Get_X() == x) && (snake.Get_Snake()[i].Get_Y() == (y + (_Side / 2) + 1))) || ((snake.Get_Snake()[i].Get_X() == (x + (_Side / 2 + 1))) && (snake.Get_Snake()[i].Get_Y() == (y + (_Side / 2) + 1)))) { ok = false; //Console.WriteLine("Insect is on the snake"); } } if (((fruit.Get_X() == x) && (fruit.Get_Y() == y)) || ((fruit.Get_X() == (x + (_Side / 2 + 1))) && (fruit.Get_Y() == y)) || ((fruit.Get_X() == x) && (fruit.Get_Y() == (y + (_Side / 2) + 1))) || ((fruit.Get_X() == (x + (_Side / 2 + 1))) && (fruit.Get_Y() == (y + (_Side / 2) + 1)))) { ok = false; //Console.WriteLine("Insect is on the fruit"); } foreach (Wall element in listWalls) { if (((element.Get_X() == x) && (element.Get_Y() == y)) || ((element.Get_X() == (x + (_Side / 2 + 1))) && (element.Get_Y() == y)) || ((element.Get_X() == x) && (element.Get_Y() == (y + (_Side / 2) + 1))) || ((element.Get_X() == (x + (_Side / 2 + 1))) && (element.Get_Y() == (y + (_Side / 2) + 1)))) { ok = false; //Console.WriteLine("Insect is on a wall"); } } return ok; }
/* * Move insect (multiplayers) * - Move positions of the insect * - Check if new positions are on the snake/fruit/walls, regenerate if needed. * */ public void MoveInsect(int width, int height, FullSnake snake, Fruit fruit, List<Wall> listWalls) { int tmpX; int tmpY; tmpX = Generate_X(width); tmpY = Generate_Y(height); while (!CheckPositions(tmpX, tmpY, snake, fruit, listWalls)) { tmpX = Generate_X(width); tmpY = Generate_Y(height); } _X = tmpX; _Y = tmpY; }
////////////// // Set _Snake public void Set_Snake(FullSnake snake) { _Snake = snake; }
/* * Method for initializing the Game: * - Initialize object for playing. * - Initialize timers (one for the game progress, one for the insect). * - initialize the thread for graphical rendering. * */ public void InitializeGame() { _FullSnake = new FullSnake(this.gameBoardPictureBox.Width); _Fruit = new Fruit(this.gameBoardPictureBox.Width, this.gameBoardPictureBox.Height); _Insect = new Insect(this.gameBoardPictureBox.Width, this.gameBoardPictureBox.Height, -666, -666); if (_Multiplayer) _ListWalls = new ListWalls(); _Score = 0; _TimerInterval = 70; _Direction = 1; _GameOver = false; _InsectIsPresent = false; _InGame = true; _Timer = new Timer(); _Timer.Interval = _TimerInterval; _Timer.Tick += new EventHandler(TimerTick); _InsectTimer = new Timer(); _InsectTimer.Interval = 1000; _InsectTimer.Tick += new EventHandler(InsectTimerTick); _RenderThread = new System.Threading.Thread(new System.Threading.ThreadStart(Render)); _RenderThread.Name = "RenderThread"; _RenderThread.IsBackground = true; }
//////////////////////// // Display of the snake public void RenderSnake(FullSnake snake, PictureBox gameBoardPictureBox) { try { // Suppress the streak behind the snake if (snake.Get_SnakeSize() >= 1) { if (snake.Get_Snake()[snake.Get_SnakeSize() - 1].Get_Direction() == 0) // If the tail is moving up... { _MyGraphics.FillRectangle(_MyBrush2, snake.Get_Snake()[snake.Get_SnakeSize() - 1].Get_X(), snake.Get_Snake()[snake.Get_SnakeSize() - 1].Get_Y() + (gameBoardPictureBox.Width / 54), (gameBoardPictureBox.Width / 54), (gameBoardPictureBox.Width / 54)); // Recolor below the last part of snake. if (snake.Get_Snake()[snake.Get_SnakeSize() - 1].Get_Y() == gameBoardPictureBox.Height - (gameBoardPictureBox.Width / 54)) // If the snake goes to the other side of the gameboard the streak to recolor is not behind the snake. _MyGraphics.FillRectangle(_MyBrush2, snake.Get_Snake()[snake.Get_SnakeSize() - 1].Get_X(), 0, (gameBoardPictureBox.Width / 54), (gameBoardPictureBox.Width / 54)); // Recolor at the opposite side. } if (snake.Get_Snake()[snake.Get_SnakeSize() - 1].Get_Direction() == 1) // If the tail is moving right... { _MyGraphics.FillRectangle(_MyBrush2, snake.Get_Snake()[snake.Get_SnakeSize() - 1].Get_X() - (gameBoardPictureBox.Width / 54), snake.Get_Snake()[snake.Get_SnakeSize() - 1].Get_Y(), (gameBoardPictureBox.Width / 54), (gameBoardPictureBox.Width / 54)); // Recolor at the left of the last part of snake. if (snake.Get_Snake()[snake.Get_SnakeSize() - 1].Get_X() == 0) // If the snake goes to the other side of the gameboard the streak to recolor is not behind the snake. _MyGraphics.FillRectangle(_MyBrush2, gameBoardPictureBox.Width - (gameBoardPictureBox.Width / 54), snake.Get_Snake()[snake.Get_SnakeSize() - 1].Get_Y(), (gameBoardPictureBox.Width / 54), (gameBoardPictureBox.Width / 54)); // Recolor at the opposite side. } if (snake.Get_Snake()[snake.Get_SnakeSize() - 1].Get_Direction() == 2) // If the tail is moving down... { _MyGraphics.FillRectangle(_MyBrush2, snake.Get_Snake()[snake.Get_SnakeSize() - 1].Get_X(), snake.Get_Snake()[snake.Get_SnakeSize() - 1].Get_Y() - (gameBoardPictureBox.Width / 54), (gameBoardPictureBox.Width / 54), (gameBoardPictureBox.Width / 54)); // Recolor above the last part of snake. if (snake.Get_Snake()[snake.Get_SnakeSize() - 1].Get_Y() == 0) // If the snake goes to the other side of the gameboard the streak to recolor is not behind the snake. _MyGraphics.FillRectangle(_MyBrush2, snake.Get_Snake()[snake.Get_SnakeSize() - 1].Get_X(), gameBoardPictureBox.Height - (gameBoardPictureBox.Width / 54), (gameBoardPictureBox.Width / 54), (gameBoardPictureBox.Width / 54)); // Recolor at the opposite side. } if (snake.Get_Snake()[snake.Get_SnakeSize() - 1].Get_Direction() == 3) // If the tail is moving left... { _MyGraphics.FillRectangle(_MyBrush2, snake.Get_Snake()[snake.Get_SnakeSize() - 1].Get_X() + (gameBoardPictureBox.Width / 54), snake.Get_Snake()[snake.Get_SnakeSize() - 1].Get_Y(), (gameBoardPictureBox.Width / 54), (gameBoardPictureBox.Width / 54)); // Recolor at the right of the last part of snake. if (snake.Get_Snake()[snake.Get_SnakeSize() - 1].Get_X() == gameBoardPictureBox.Width - (gameBoardPictureBox.Width / 54)) // If the snake goes to the other side of the gameboard the streak to recolor is not behind the snake. _MyGraphics.FillRectangle(_MyBrush2, 0, snake.Get_Snake()[snake.Get_SnakeSize() - 1].Get_Y(), (gameBoardPictureBox.Width / 54), (gameBoardPictureBox.Width / 54)); // Recolor at the opposite side } } // Draw all parts of the snake (at the beginning of the game) if ((snake.Get_SnakeSize() == 3)) // If the size of the snake is equal to 3 (at the begin of the game)... for (int i = 0; i < snake.Get_SnakeSize(); i++) // ... Look for each part of the snake... _MyGraphics.FillEllipse(_MyBrush, new Rectangle(snake.Get_Snake()[i].Get_X(), snake.Get_Snake()[i].Get_Y(), (gameBoardPictureBox.Width / 54 - 2) - 1, (gameBoardPictureBox.Width / 54 - 2) - 1)); // ... And draw the part. // Draw the head of the snake else _MyGraphics.FillEllipse(_MyBrush, new Rectangle(snake.Get_Snake()[0].Get_X(), snake.Get_Snake()[0].Get_Y(), (gameBoardPictureBox.Width / 54 - 2) - 1, (gameBoardPictureBox.Width / 54 - 2) - 1)); } catch (Exception e) { Console.WriteLine(e); } }
///////////////////////////// // Display of the mini snake public void RenderMiniSnake(FullSnake snake, PictureBox miniGameBoardPictureBox) { for (int i = 0; i < snake.Get_SnakeSize(); i++) { if (_MiniSnakeGraphicalParts.Count < snake.Get_SnakeSize()) // If there is not enough picturebox in the pool ... { _MiniSnakeGraphicalParts.Add(new PictureBox()); // ...Add it one. _MiniSnakeGraphicalParts[_MiniSnakeGraphicalParts.Count - 1].Size = new System.Drawing.Size(snake.Get_Snake()[i].Get_SIDE() / 2, snake.Get_Snake()[i].Get_SIDE() / 2); // Definition of the picturebox size. _MiniSnakeGraphicalParts[_MiniSnakeGraphicalParts.Count - 1].BackColor = System.Drawing.Color.Black; // Definition of the picturebox color. miniGameBoardPictureBox.Controls.Add(_MiniSnakeGraphicalParts[_MiniSnakeGraphicalParts.Count - 1]); // Attach the control to the gameboard. } if(_MiniSnakeGraphicalParts[i].Parent != miniGameBoardPictureBox) // If the control is not attached to the gameboard... miniGameBoardPictureBox.Controls.Add(_MiniSnakeGraphicalParts[i]); // Attach the control to the gameboard. _MiniSnakeGraphicalParts[i].Location = new System.Drawing.Point(snake.Get_Snake()[i].Get_X()/2, snake.Get_Snake()[i].Get_Y()/2); // Definition of the panel location. } }