protected override void Initialize() { base.Initialize(); MenuScene(); gs = GameState.Menu; er = EnterReleased.Yes; }
protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) { this.Exit(); } KeyboardState ks = Keyboard.GetState(); //if quiz is in progress, update timer and its location constantly // (transition - pause between questions) if (gs == GameState.Quiz || gs == GameState.Transition) { quiz.Duration -= (float)gameTime.ElapsedGameTime.TotalSeconds; quiz.ScorePosition = new Vector2(WINDOWWIDTH - spriteFont.MeasureString(quiz.Score.ToString() + " ").X, 0); } //counts milliseconds during transition (between questions) if (gs == GameState.Transition) { quiz.Timer -= (float)gameTime.ElapsedGameTime.TotalMilliseconds; } //if quiz is in progress if (gs == GameState.Quiz) { if (ks.IsKeyDown(Keys.Left)) //if left arrow key pressed { if (choose == Choose.Left) { quiz.Score += ONESCORE; //update score (+) quiz.HiColor1 = Color.LimeGreen; //highlight correct flag quiz.ScoreColor = Color.LimeGreen; //highlight score quiz.Correct++; //update correct answer count correct.Play(); //play correct sound } else { quiz.Score -= ONESCORE; //update score (-) quiz.HiColor1 = Color.OrangeRed; //highlight incorrect flag quiz.ScoreColor = Color.OrangeRed; //highlight score quiz.Incorrect++; //update incorrect answer count incorrect.Play(); //play incorrect sound } gs = GameState.Transition; //enter transition (pause between questions) quiz.Timer = TIMER; //set transition timer } if (ks.IsKeyDown(Keys.Right)) //if right arrow key pressed { if (choose == Choose.Right) { quiz.Score += ONESCORE; //update score (+) quiz.HiColor2 = Color.LimeGreen; //highlight correct flag quiz.ScoreColor = Color.LimeGreen; //highlight score quiz.Correct++; //update correct answer count correct.Play(); //play correct sound } else { quiz.Score -= ONESCORE; //update score (-) quiz.HiColor2 = Color.OrangeRed; //highlight incorrect flag quiz.ScoreColor = Color.OrangeRed; //highlight score quiz.Incorrect++; //update incorrect answer count incorrect.Play(); //play incorrect sound } gs = GameState.Transition; //enter transition (pause between questions) quiz.Timer = TIMER; //set transition timer } } // if gamestate set to menu, show it if (gs == GameState.Menu) { ShowMenu(); } // if quiz/transition (pause between questions) in progress and time is up, // show results (score, correct, incorrect) and play sound if ((gs == GameState.Quiz || gs == GameState.Transition) && quiz.Duration <= 0) { ShowResult(); select.Play(); } //if results screen is up if (gs == GameState.Result) { //make navigation selection for play again if (result.Index == 0 && ks.IsKeyDown(Keys.Enter)) { StartQuiz(); select.Play(); } //make navigation selection for go to menu if (result.Index == 1 && ks.IsKeyDown(Keys.Enter)) { ShowMenu(); select.Play(); er = EnterReleased.No; //set er to No until Enter key is released } } // if help / how to play / about screen is up if (gs == GameState.Help || gs == GameState.HowToPlay || gs == GameState.About) { if (ks.IsKeyUp(Keys.Enter)) //as Enter key is released, set er so { er = EnterReleased.Yes; } //make navigation selection for go to menu if (er == EnterReleased.Yes && ks.IsKeyDown(Keys.Enter)) { ShowMenu(); select.Play(); er = EnterReleased.No; //set er to No until Enter key is released } } // if main menu screen is up if (gs == GameState.Menu) { if (ks.IsKeyUp(Keys.Enter)) { er = EnterReleased.Yes;//as Enter key is released, set er so } //make navigation selection for play (start quiz) if (menu.Index == 0 && er == EnterReleased.Yes && ks.IsKeyDown(Keys.Enter)) { StartQuiz(); select.Play(); } //make navigation selection for show help if (menu.Index == 1 && er == EnterReleased.Yes && ks.IsKeyDown(Keys.Enter)) { ShowHelp(); select.Play(); er = EnterReleased.No; //set er to No until Enter key is released } //make navigation selection for show how to play if (menu.Index == 2 && er == EnterReleased.Yes && ks.IsKeyDown(Keys.Enter)) { ShowHowToPlay(); select.Play(); er = EnterReleased.No; //set er to No until Enter key is released } //make navigation selection for about info if (menu.Index == 3 && er == EnterReleased.Yes && ks.IsKeyDown(Keys.Enter)) { ShowAbout(); select.Play(); er = EnterReleased.No; //set er to No until Enter key is released } //make navigation selection for application exit if (menu.Index == 4 && ks.IsKeyDown(Keys.Enter)) { this.Exit(); } } //if game is in transition state (pause between questions), or it's the first // question in quiz if ((gs == GameState.Transition && quiz.Timer <= 0) || gs == GameState.QuizFirst) { int country1 = r.Next(0, countries.Count()); //left flag randomized int country2 = country1; //right flag equals to left flag // while right flag equals to left flag, randomize right flag to it doesn't while (country2 == country1) { country2 = r.Next(0, countries.Count()); } int select = r.Next(0, 2); //make a random choice between left and right flag int chooseText; //to remember ID (int) of the random choice above //set global 'choose' in accordance with randomized choice (left/right), // as well as the ID ('chooseText') to draw the flag if (select == 0) { choose = Choose.Left; chooseText = country1; } else { choose = Choose.Right; chooseText = country2; } //locations of rectangles of left and right flags are set quiz.Rect1 = new Rectangle(countries[country1].X, countries[country1].Y, FLAGWIDTH, FLAGHEIGHT); quiz.Rect2 = new Rectangle(countries[country2].X, countries[country2].Y, FLAGWIDTH, FLAGHEIGHT); //set country name text (seen under the two flags on the quiz screen) quiz.Text = countries[chooseText].Name; Vector2 countryTextDim = spriteFont.MeasureString(quiz.Text); double countryTextPosX = graphics.PreferredBackBufferWidth / 2 - countryTextDim.X / 2; quiz.TextPosition = new Vector2(Convert.ToInt32(countryTextPosX), COUNTRYTEXTY); //highlight for left and right flags is to default; score text colour to white quiz.HiColor1 = Color.DarkGray; quiz.HiColor2 = Color.DarkGray; quiz.ScoreColor = Color.White; gs = GameState.Quiz; //enter quiz game state } base.Update(gameTime); }
protected override void Update(GameTime gameTime) { KeyboardState ks = Keyboard.GetState(); // Always snowPosition1 += snowSpeed; snowPosition2 += snowSpeed; if (snowPosition1.Y > WINDOWHEIGHT) { snowPosition1.Y = snowPosition2.Y - snowTex.Height; } if (snowPosition2.Y > WINDOWHEIGHT) { snowPosition2.Y = snowPosition1.Y - snowTex.Height; } // Running / Pause if (gs == GameState.Running || gs == GameState.Pause) { if (ks.IsKeyDown(Keys.Left)) { paddle.X -= PADDLEPSPEED; if (paddle.X <= 0) { paddle.X = 0; } } if (ks.IsKeyDown(Keys.Right)) { paddle.X += PADDLEPSPEED; if (paddle.X > WINDOWWIDTH - paddle.Width) { paddle.X = WINDOWWIDTH - paddle.Width; } } if (blocks.Count() == 0) { EndScene(); gs = GameState.Over; endScene.Score = score; endScene.LifeBonus = lifes * LIFEBONUS; return; } if (lifes == 0) { EndScene(); gs = GameState.Over; endScene.Score = score; endScene.LifeBonus = lifes; lifes = MAXLIFES; return; } } // Pause if (gs == GameState.Pause) { if (ks.IsKeyUp(Keys.Enter)) { er = EnterReleased.Yes; } ball.X = paddle.Center.X - ballTex.Width / 2; if (ks.IsKeyDown(Keys.Space) || (ks.IsKeyDown(Keys.Enter) && er == EnterReleased.Yes)) { gs = GameState.Running; ballSpeed.Y = -BALLSPEED; } } // Menu if (gs == GameState.Menu) { if (ks.IsKeyUp(Keys.Enter)) { er = EnterReleased.Yes; } if (menuScene.Index == 0 && er == EnterReleased.Yes && ks.IsKeyDown(Keys.Enter)) { StartGame(); er = EnterReleased.No; } if (menuScene.Index == 1 && er == EnterReleased.Yes && ks.IsKeyDown(Keys.Enter)) { HowToScene(); er = EnterReleased.No; } if (menuScene.Index == 2 && er == EnterReleased.Yes && ks.IsKeyDown(Keys.Enter)) { HelpScene(); er = EnterReleased.No; } if (menuScene.Index == 3 && er == EnterReleased.Yes && ks.IsKeyDown(Keys.Enter)) { AboutScene(); er = EnterReleased.No; } if (menuScene.Index == 4 && ks.IsKeyDown(Keys.Enter)) { this.Exit(); } } if (gs == GameState.Help || gs == GameState.HowTo || gs == GameState.About) { if (ks.IsKeyUp(Keys.Enter)) { er = EnterReleased.Yes; } if (er == EnterReleased.Yes && ks.IsKeyDown(Keys.Enter)) { MenuScene(); er = EnterReleased.No; } } //Game Over if (gs == GameState.Over) { if (endScene.Index == 0 && ks.IsKeyDown(Keys.Enter)) { StartGame(); er = EnterReleased.No; } if (endScene.Index == 1 && ks.IsKeyDown(Keys.Enter)) { MenuScene(); er = EnterReleased.No; } } // Running if (gs == GameState.Running) { // Ball Speed ball.X += (int)ballSpeed.X; ball.Y += (int)ballSpeed.Y; // Ball - Paddle if (ball.Intersects(paddle)) { ballSpeed.Y *= -1; ballSpeed.X = (ball.Center.X - paddle.Center.X) / 6; // To slow down } // Ball - Block foreach (var block in blocks) { if (ball.Intersects(block)) { blocks.Remove(block); score += SCOREPOINTS; ballSpeed.Y *= -1; break; } } // Ball - Walls if (ball.Y < 0) { ballSpeed.Y *= -1; } if (ball.X < 0 || ball.X > WINDOWWIDTH - ball.Width) { ballSpeed.X *= -1; } if (ball.Y > WINDOWHEIGHT) { ResetPaddle(); lifes--; return; } } base.Update(gameTime); }