void InitialMysterySpawn() { // spawn mysteryboxes in the spawn areas at beginning of game for (int i = 0; i < mysterySpawns.Length; i++) { // retrieve the spawn PictureBox spawn = mysterySpawns[i]; MysteryBox box = new MysteryBox(spawn.Location.X, spawn.Location.Y, this); // spawn a mysterbox in the location engine.mysteryBoxes.Add(box); // register it with the engine // add it to the list of fixed mysterboxes fixedBoxes[i] = box; } }
// check if collided with snake or food public bool Collided(MysteryBox box, List <ContinuousSnake> snakes, List <MysteryBox> boxes) { foreach (ContinuousSnake snake in snakes) // check all snakes { if (snake.Collided(box.picBox) != 0) // if not zero, collided with food { return(true); } } foreach (MysteryBox mbox in boxes) // check all food items { if (mbox.picBox.Bounds.IntersectsWith(box.picBox.Bounds)) { return(true); } } return(false); // if function reached here, no collision happened }
public void Spawn(List <ContinuousSnake> snakes, List <MysteryBox> boxes) // spawn food in the area { int x = engine.GetRandom(Bounds.X, Bounds.X + Bounds.Width - BodyPart.SIZE); // subtract bodypart size because apple could be out of bounds int y = engine.GetRandom(Bounds.Y, Bounds.Height + Bounds.Y - BodyPart.SIZE); // make new apple MysteryBox box = new MysteryBox(x, y, engine.mainForm); // make sure not collided with snakes or other food items if (Collided(box, snakes, boxes)) { // delete the food item box.finalize(); // run function again to try to get another position Spawn(snakes, boxes); } else { engine.mysteryBoxes.Add(box); // register it } }
// perform extrawork during frame update public override void ExtraWork(object o, EventArgs e) { base.ExtraWork(o, e); for (int i = 0; i < fixedBoxes.Length; i++) // check all mysteryboxes { // retrieve the box MysteryBox box = fixedBoxes[i]; if (box.Finalized) // if its finalized and no snake is in the location, make a new mystery box in the location { // check if a snake is the location of the box bool snakePresent = false; foreach (ContinuousSnake snake in engine.snakes) { if (snake.Collided(mysterySpawns[i]) != 0) // if its not zero, a snake collided with the box { snakePresent = true; break; } } // if a snake is not present, spawn a mysterybox there if (!snakePresent) { // retrieve the location of this mysterybox spawn Point spawn = mysterySpawns[i].Location; // make a new mysterbox in the location of the spawn point fixedBoxes[i] = new MysteryBox(spawn.X, spawn.Y, this); engine.mysteryBoxes.Add(fixedBoxes[i]);// register it with the engine } } if (snake.Collided(Gate) != 0) { snake.snakeHead.picBox.SendToBack(); } } // show the size of the snake txtSize.Text = string.Format("{0:0.#}", (double)snake.Length / (double)BodyPart.SIZE); }