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; }
/* * 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; }
// Function: Choose Fruit // Selects Fruit Type and loads position public void chooseFruit() { // Reload and randomize fruit positions fruitAssets(); // Load initial fruit and helpful variables int initialFruit = 1; int itX, itY; // Select Fruit: // 0 = apple // 1 = cherry // 2 = orange initialFruit = rand.Next(3); // Translate sprite/screen position to grid position to be handled // for purposes of game logic switch (initialFruit) { case (0): currentFruit = apple; itX = ((int)currentFruit.position.X) / 16; itY = ((int)currentFruit.position.Y) / 16; gameArray[itX, itY] = 1; break; case (1): currentFruit = cherry; itX = ((int)currentFruit.position.X) / 16; itY = ((int)currentFruit.position.Y) / 16; gameArray[itX, itY] = 2; break; case (2): currentFruit = orange; itX = ((int)currentFruit.position.X) / 16; itY = ((int)currentFruit.position.Y) / 16; gameArray[itX, itY] = 3; break; } }
// Load fruit assets public void fruitAssets() { apple = new Fruit(Game.Content.Load<Texture2D>(@"images\apple"), Fruit.placeFruit(), new Point(16, 16), new Vector2(4f, 4f), new Vector2(1, 1), Color.CornflowerBlue, new Vector2(0, 0), 0); cherry = new Fruit(Game.Content.Load<Texture2D>(@"images\cherry"), Fruit.placeFruit(), new Point(16, 16), new Vector2(4f, 4f), new Vector2(1, 1), Color.CornflowerBlue, new Vector2(0, 0), 1); orange = new Fruit(Game.Content.Load<Texture2D>(@"images\orange"), Fruit.placeFruit(), new Point(16, 16), new Vector2(4f, 4f), new Vector2(1, 1), Color.CornflowerBlue, new Vector2(0, 0), 2); }
////////////// // Set _Fruit public void Set_Fruit(Fruit fruit) { _Fruit = fruit; }
/* * 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 mini fruit public void RenderMiniFruit(Fruit fruit, PictureBox miniGameBoardPictureBox) { if (_MiniFruit.Parent != miniGameBoardPictureBox) miniGameBoardPictureBox.Controls.Add(_MiniFruit); _MiniFruit.Location = new System.Drawing.Point(fruit.Get_X()/2, fruit.Get_Y()/2); }
//////////////////////// // Display of the fruit public void RenderFruit(Fruit fruit, PictureBox gameBoardPictureBox) { if (_Fruit.Parent != gameBoardPictureBox) gameBoardPictureBox.Controls.Add(_Fruit); _Fruit.Location = new System.Drawing.Point(fruit.Get_X(), fruit.Get_Y()); }