Published on

Create a Whack-a-Mole Game in Excel

Authors

Create a Whack-a-Mole Game in Excel

Let's create a fun Whack-a-Mole style game where players need to quickly click on cells that light up. This simple game tests reflexes and demonstrates Excel's event handling capabilities.

Game Overview

  • Cells randomly light up across a grid
  • Player must click the lit cell to score points
  • Cells remain lit for a short time only
  • Score increases with each successful hit
  • Game runs for 30 seconds

Step-by-Step Tutorial

1. Setting Up the Game Board

First, let's create our game interface:

  • Cells B2:F6: This will be our 5x5 game grid
  • Cell B8: "Score:"
  • Cell C8: Will show the current score
  • Cell B9: "Time Left:"
  • Cell C9: Will show the countdown timer

2. Formatting the Game Grid

  1. Format cells B2:F6:
    • Set equal row height and column width
    • Center align all cells
    • Add thin borders to create a grid
    • Set background color to light gray

3. Adding VBA Code

Public Score As Integer
Public GameActive As Boolean
Public TimeLeft As Integer
Public ActiveCell As Range

Sub StartGame()
    If GameActive Then Exit Sub
    
    ' Initialize game
    Score = 0
    TimeLeft = 30
    GameActive = True
    Range("C8").Value = Score
    Range("C9").Value = TimeLeft
    
    ' Start game loop
    GameLoop
End Sub

Sub GameLoop()
    If Not GameActive Then Exit Sub
    
    ' Clear previous active cell
    If Not ActiveCell Is Nothing Then
        ActiveCell.Interior.ColorIndex = xlNone
    End If
    
    ' Generate new position
    Dim row As Integer, col As Integer
    row = Int(Rnd * 5) + 2  ' Rows 2-6
    col = Int(Rnd * 5) + 2  ' Columns B-F
    
    ' Activate new cell
    Set ActiveCell = Cells(row, col)
    ActiveCell.Interior.ColorIndex = 6 ' Yellow
    
    ' Update timer
    TimeLeft = TimeLeft - 1
    Range("C9").Value = TimeLeft
    
    ' Check if game should continue
    If TimeLeft <= 0 Then
        GameActive = False
        MsgBox "Game Over! Final Score: " & Score
        Exit Sub
    End If
    
    ' Schedule next mole after delay
    Application.OnTime Now + TimeValue("00:00:01"), "GameLoop"
End Sub

Sub CellClick(Target As Range)
    If Not GameActive Then Exit Sub
    If Target Is Nothing Then Exit Sub
    
    ' Check if clicked cell is active
    If Not ActiveCell Is Nothing Then
        If Target.Address = ActiveCell.Address Then
            Score = Score + 1
            Range("C8").Value = Score
            ActiveCell.Interior.ColorIndex = xlNone
            Set ActiveCell = Nothing
        End If
    End If
End Sub

4. Setting Up Event Handling

  1. In the ThisWorkbook module, add:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    CellClick Target
End Sub
  1. Add a "Start Game" button:
    • Insert a button above the grid
    • Label it "Start Game"
    • Assign it to the StartGame macro

5. Final Setup

  1. Protect the worksheet:
    • Allow selection of cells in the game grid only
    • Hide the grid lines
  2. Add instructions in cell B11: "Click 'Start Game' to begin. Click the yellow cells as fast as you can!"

How to Play

  1. Click "Start Game" to begin
  2. Watch for cells that turn yellow
  3. Click the yellow cells as quickly as possible
  4. Score a point for each successful hit
  5. Try to get the highest score in 30 seconds

Understanding the Game Mechanics

This game demonstrates several Excel gaming concepts:

  1. Event Handling: Responding to cell clicks
  2. Timer System: Creating time-based gameplay
  3. Random Elements: Generating random cell positions
  4. Score Tracking: Maintaining player score

Download

You can download the game template here (Coming soon)

Next Steps

Try these modifications to enhance the game:

  1. Add different colored cells worth different points
  2. Include multiple active cells at once
  3. Add sound effects for successful hits

Stay tuned for more Excel game development tutorials!


This Whack-a-Mole style game shows how Excel can be used to create fast-paced action games. It combines timing, user interaction, and score tracking to create an engaging gaming experience.