Engine Overview

Adding a Timer to Minesweeper

How can I implement a timer for the Minesweeper game?

Abstract art representing computer programming

Implementing a timer for your Minesweeper game can add an extra layer of challenge and excitement. We can use SDL's time-related functions to create a simple but effective timer. Here's how you can implement it:

Step 1: Add timer-related variables

First, let's add some variables to keep track of the time in our MinesweeperUI class:

class MinesweeperUI {
private:
  Uint32 StartTime{0};
  Uint32 CurrentTime{0};
  bool TimerRunning{false};

  // ... (other members)
};

Step 2: Start the timer

We'll start the timer when the first cell is revealed. In your HandleEvent method:

void MinesweeperUI::HandleEvent(
  const SDL_Event &E) {
  if (E.type == SDL_MOUSEBUTTONDOWN) {
    // ... (existing code)
    if (FirstCellRevealed && !TimerRunning) {
      StartTime = SDL_GetTicks();
      TimerRunning = true;
    }
  }
}

Step 3: Update the timer

We need to update the timer in each frame. Add this to your Update method:

void MinesweeperUI::Update() {
  if (TimerRunning) {
    CurrentTime = SDL_GetTicks() - StartTime;
  }
  // ... (other update logic)
}

Step 4: Display the timer

Now, let's display the timer. We'll create a method to format the time:

std::string MinesweeperUI::FormatTime(
  Uint32 Milliseconds) {
  int Seconds = Milliseconds / 1000;
  int Minutes = Seconds / 60;
  Seconds %= 60;

  std::stringstream SS;
  SS << std::setfill('0') << std::setw(2)
     << Minutes << ":" << std::setfill('0')
     << std::setw(2) << Seconds;
  return SS.str();
}

And use it in our Render method:

void MinesweeperUI::Render(
  SDL_Surface *Surface) {
  // ... (existing rendering code)

  std::string TimeString =
    FormatTime(CurrentTime);
  Engine::Text TimeText{10, 10, 100, 30,
                        TimeString};
  TimeText.Render(Surface);
}

Step 5: Stop the timer

We should stop the timer when the game ends (either win or lose):

void MinesweeperUI::EndGame(bool Win) {
  TimerRunning = false;
  // ... (other end game logic)
}

Here's a complete example putting it all together:

#include <SDL.h>
#include <iomanip>
#include <sstream>
#include "Engine/Text.h"

class MinesweeperUI {
public:
  void HandleEvent(const SDL_Event &E) {
    if (E.type == SDL_MOUSEBUTTONDOWN) {
      // ... (existing code)
      if (FirstCellRevealed && !TimerRunning) {
        StartTime = SDL_GetTicks();
        TimerRunning = true;
      }
    }
  }

  void Update() {
    if (TimerRunning) {
      CurrentTime = SDL_GetTicks() - StartTime;
    }
    // ... (other update logic)
  }

  void Render(SDL_Surface *Surface) {
    // ... (existing rendering code)
    std::string TimeString =
      FormatTime(CurrentTime);
    Engine::Text TimeText{10, 10, 100, 30,
                          TimeString};
    TimeText.Render(Surface);
  }

  void EndGame(bool Win) {
    TimerRunning = false;
    // ... (other end game logic)
  }

private:
  std::string FormatTime(Uint32 Milliseconds) {
    int Seconds = Milliseconds / 1000;
    int Minutes = Seconds / 60;
    Seconds %= 60;

    std::stringstream SS;
    SS << std::setfill('0') << std::setw(2)
       << Minutes << ":" << std::setfill('0')
       << std::setw(2) << Seconds;
    return SS.str();
  }

  Uint32 StartTime{0};
  Uint32 CurrentTime{0};
  bool TimerRunning{false};

  // ... (other members)
};

This implementation provides a basic timer that starts when the first cell is revealed, updates every frame, and stops when the game ends. The time is displayed in the format "MM:SS" in the top-left corner of the game window. You can adjust the position and formatting as needed to fit your game's UI design.

This Question is from the Lesson:

Engine Overview

An introduction to the generic engine classes we'll use to create the game

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Engine Overview

An introduction to the generic engine classes we'll use to create the game

sdl2-promo.jpg
Part of the course:

Game Dev with SDL2

Learn C++ and SDL development by creating hands on, practical projects inspired by classic retro games

Free, unlimited access

This course includes:

  • 62 Lessons
  • 100+ Code Samples
  • 91% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved