private void Spawn(Fruit fruit) { // find a valid location to spawn the fruit int fruitXPosition = _randomNumbers.Next(WINDOW_WIDTH - CELL_WIDTH); int fruitYPosition = _randomNumbers.Next(WINDOW_HEIGHT - CELL_HEIGHT); // set fruit's new position and reverse direction fruit.Position = new Vector2(fruitXPosition, fruitYPosition); fruit.Velocity = -fruit.Velocity; }
/// <summary> /// method to determine if the mouse is on the fruit /// </summary> /// <returns></returns> private bool MouseOnFruit(Fruit fruit) { bool mouseClickedOnFruit = false; // get the current state of the mouse MouseState mouseState = Mouse.GetState(); // mouse over fruit if ((_mouseNewState.X > fruit.Position.X) && (_mouseNewState.X < (fruit.Position.X + 64)) && (_mouseNewState.Y > fruit.Position.Y) && (_mouseNewState.Y < (fruit.Position.Y + 64))) { mouseClickedOnFruit = true; } return mouseClickedOnFruit; }
protected override void Initialize() { // set the background's initial position _backgroundPosition = new Rectangle(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); // create a fruit object _fruits = new List<Fruit>(); // add fruit to the list Fruit fruit01 = new Fruit(Content, "Fruit01", 32, new Vector2(300, 200), new Vector2(3, 4)); _fruits.Add(fruit01); Fruit fruit02 = new Fruit(Content, "Fruit02", 32, new Vector2(100, 50), new Vector2(-1, -1)); _fruits.Add(fruit02); Fruit fruit03 = new Fruit(Content, "Fruit03", 32, new Vector2(400, 150), new Vector2(2, 2)); _fruits.Add(fruit03); // make the fruit active fruit01.Active = true; fruit02.Active = true; fruit03.Active = true; // make mouse visible on game this.IsMouseVisible = true; base.Initialize(); }
/// <summary> /// method to determine if the mouse is on the fruit /// </summary> /// <returns></returns> private bool MouseClickOnFruit(Fruit fruit) { bool mouseClickedOnFruit = false; // get the current state of the mouse _mouseNewState = Mouse.GetState(); // left mouse button was a click if (_mouseNewState.LeftButton == ButtonState.Pressed && _mouseOldState.LeftButton == ButtonState.Released) { // mouse over fruit if ((_mouseNewState.X > fruit.Position.X) && (_mouseNewState.X < (fruit.Position.X + 64)) && (_mouseNewState.Y > fruit.Position.Y) && (_mouseNewState.Y < (fruit.Position.Y + 64))) { mouseClickedOnFruit = true; _score++; } } // store the current state of the mouse as the old state _mouseOldState = _mouseNewState; return mouseClickedOnFruit; }
/// <summary> /// method to bounce fruit of walls /// </summary> public void BounceOffWalls(Fruit fruit) { // fruit is at the top or bottom of the window, change the Y direction if ((fruit.Position.Y > WINDOW_HEIGHT - CELL_HEIGHT) || (fruit.Position.Y < 0)) { fruit.Velocity = new Vector2(fruit.Velocity.X, -fruit.Velocity.Y); } // fruit is at the left or right of the window, change the X direction else if ((fruit.Position.X > WINDOW_WIDTH - CELL_WIDTH) || (fruit.Position.X < 0)) { fruit.Velocity = new Vector2(-fruit.Velocity.X, fruit.Velocity.Y); } }