/// <summary> /// Updates the menu entry locations. /// </summary> protected void UpdateMenuEntryLocations() { // Make the menu slide into place during transitions, using a // power curve to make things look more interesting (this makes // the movement slow down as it nears the end). float transitionOffset = (float)Math.Pow(this.TransitionPosition, 2); // start at Y = 175; each X value is generated per entry Vector2 position = new Vector2(0f, 310); // update each menu entry's location in turn for (int i = 0; i < this._menuEntries.Count; i++) { AnswerEntry menuEntry = this._menuEntries[i]; // each entry is to be centered horizontally position.X = this.ScreenManager.GraphicsDevice.Viewport.Width / 2 - menuEntry.GetWidth(this) / 2; if (ScreenState == ScreenState.TransitionOn) { position.X -= transitionOffset * 256; } else { position.X += transitionOffset * 512; } // set the entry's position menuEntry.Position = position; // move down for the next entry the size of this entry position.Y += menuEntry.GetHeight(this); } }
/// <summary> /// Setups the next question. /// </summary> private void SetupNextQuestion() { // First, detach from the existing entries if (_menuEntries != null) { _menuEntries.ForEach(e => e.Selected -= entry_Selected); } // Now, load and hook up the new ones if (_questions.Count > 0) { _currentQuestion = _questions.Pop(); this._menuEntries = new List <AnswerEntry>(); foreach (Answer answer in _currentQuestion.Answers) { AnswerEntry entry = new AnswerEntry(answer); entry.Selected += entry_Selected; this._menuEntries.Add(entry); } } else { // No more questions? No more game! ExitScreen(); } }
/// <summary> /// Handles the Selected event of the entry control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="PlayerIndexEventArgs"/> instance containing the event data.</param> void entry_Selected(object sender, PlayerIndexEventArgs e) { AnswerEntry entry = (AnswerEntry)sender; if (entry.IsCorrect) { _correctSound.Play(); Player.AddToScore(_correctAnswerPoints); } else { _incorrectSound.Play(); _quizTimer.SubtractTime(_incorrectAnswerTime); } SetupNextQuestion(); }
/// <summary> /// This is called when the screen should draw itself. /// </summary> /// <param name="gameTime">Time passed since the last call to Draw.</param> public override void Draw(Microsoft.Xna.Framework.GameTime gameTime) { base.Draw(gameTime); _quizTimer.Draw(ScreenManager.SpriteBatch); // make sure our entries are in the right place before we draw them this.UpdateMenuEntryLocations(); GraphicsDevice graphics = this.ScreenManager.GraphicsDevice; SpriteBatch spriteBatch = this.ScreenManager.SpriteBatch; SpriteFont font = this.ScreenManager.Font; spriteBatch.Begin(); // Draw each menu entry in turn. for (int i = 0; i < this._menuEntries.Count; i++) { AnswerEntry menuEntry = this._menuEntries[i]; bool isSelected = this.IsActive && (i == this._selectedEntry); menuEntry.Draw(this, isSelected, gameTime); } // Make the menu slide into place during transitions, using a // power curve to make things look more interesting (this makes // the movement slow down as it nears the end). float transitionOffset = (float)Math.Pow(this.TransitionPosition, 2); // Draw the menu title centered on the screen Vector2 titlePosition = new Vector2(graphics.Viewport.Width / 2, 120); Vector2 titleOrigin = font.MeasureString(this._currentQuestion.QuestionText) / 2; Color titleColor = new Color(192, 192, 192) * this.TransitionAlpha; float titleScale = 1.25f; titlePosition.Y -= transitionOffset * 100; spriteBatch.DrawString(font, this._currentQuestion.QuestionText, titlePosition, titleColor, 0, titleOrigin, titleScale, SpriteEffects.None, 0); spriteBatch.End(); }