// Draw the updated game state each frame protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); // Clear the screen spriteBatch.Begin(); // Begin drawing // Draw grass spriteBatch.Draw(grass, new Rectangle(0, (int)(screenHeight * SKYRATIO), (int)screenWidth, (int)screenHeight), Color.White); if (gameOver) { // Draw game over texture spriteBatch.Draw(gameOverTexture, new Vector2(screenWidth / 2 - gameOverTexture.Width / 2, screenHeight / 4 - gameOverTexture.Width / 2), Color.White); String pressEnter = "Press Enter to restart!"; // Measure the size of text in the given font Vector2 pressEnterSize = stateFont.MeasureString(pressEnter); // Draw the text horizontally centered spriteBatch.DrawString(stateFont, pressEnter, new Vector2(screenWidth / 2 - pressEnterSize.X / 2, screenHeight - 200), Color.White); // If the game is over, draw the score in red spriteBatch.DrawString(scoreFont, score.ToString(), new Vector2(screenWidth - 100, 50), Color.Red); } // If the game is not over, draw it in black else { spriteBatch.DrawString(scoreFont, score.ToString(), new Vector2(screenWidth - 100, 50), Color.Black); } // Draw broccoli and dino with the SpriteClass method broccoli.Draw(spriteBatch); dino.Draw(spriteBatch); if (!gameStarted) { // Fill the screen with black before the game starts spriteBatch.Draw(startGameSplash, new Rectangle(0, 0, (int)screenWidth, (int)screenHeight), Color.White); String title = "VEGGIE JUMP"; String pressSpace = "Press Space to start"; // Measure the size of text in the given font Vector2 titleSize = stateFont.MeasureString(title); Vector2 pressSpaceSize = stateFont.MeasureString(pressSpace); // Draw the text horizontally centered spriteBatch.DrawString(stateFont, title, new Vector2(screenWidth / 2 - titleSize.X / 2, screenHeight / 3), Color.ForestGreen); spriteBatch.DrawString(stateFont, pressSpace, new Vector2(screenWidth / 2 - pressSpaceSize.X / 2, screenHeight / 2), Color.White); } spriteBatch.End(); // Stop drawing base.Draw(gameTime); }
/// <summary> /// This is called when the game should draw itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); // TODO: Add your drawing code here GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); /* Ici, nous utilisons la méthode spriteBatch.Draw pour placer la texture donnée dans les limites d’un objet Rectangle. * Un Rectangle est défini par les coordonnées x et y de ses angles supérieur gauche et inférieur droit. * À l’aide des variables screenWidth, screenHeight et SKYRATIO; nous dessinons la texture du rectangle sur le tiers inférieur de l’écran. */ spriteBatch.Draw(grass, new Rectangle(0, (int)(screenHeight * SKYRATIO), (int)screenWidth, (int)screenHeight), Color.White); if (gameOver) { // Draw game over texture spriteBatch.Draw(gameOverTexture, new Vector2(screenWidth / 2 - gameOverTexture.Width / 2, screenHeight / 4 - gameOverTexture.Width / 2), Color.White); String pressEnter = "Appuyez sur Entree pour recommencer !"; // Measure the size of text in the given font Vector2 pressEnterSize = stateFont.MeasureString(pressEnter); // Draw the text horizontally centered spriteBatch.DrawString(stateFont, pressEnter, new Vector2(screenWidth / 2 - pressEnterSize.X / 2, screenHeight - 200), Color.White); } broccoli.Draw(spriteBatch); dino.Draw(spriteBatch); // utilise la description de sprite que nous avons créée (Arial taille 36) pour tirer le score actuel du joueur vers le coin supérieur droit de l’écran. spriteBatch.DrawString(scoreFont, score.ToString(), new Vector2(screenWidth - 100, 50), Color.Black); if (!gameStarted) { // Fill the screen with black before the game starts spriteBatch.Draw(startGameSplash, new Rectangle(0, 0, (int)screenWidth, (int)screenHeight), Color.White); String title = "DinoChat deteste les brocolis "; String pressSpace = "Appuyez sur Espace pour commencer "; // Mesure la largeur et la hauteur de chaque ligne une fois imprimée, à l’aide de la méthode SpriteFont.MeasureString(String). // Cela nous donne la taille en tant qu'objet Vector2, avec la propriété X contenant sa largeur, et Y sa hauteur. Vector2 titleSize = stateFont.MeasureString(title); Vector2 pressSpaceSize = stateFont.MeasureString(pressSpace); // Draw the text horizontally centered spriteBatch.DrawString(stateFont, title, new Vector2(screenWidth / 2 - titleSize.X / 2, screenHeight / 3), Color.ForestGreen); spriteBatch.DrawString(stateFont, pressSpace, new Vector2(screenWidth / 2 - pressSpaceSize.X / 2, screenHeight / 2), Color.White); } spriteBatch.End(); base.Draw(gameTime); }