/* * 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; }
/* * 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; }
//////////////////////// // 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. } }