Skip to content

Instantly share code, notes, and snippets.

@papagunit
Created July 20, 2023 05:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save papagunit/e5cd2dca0c0622151a0a6f5e3856616d to your computer and use it in GitHub Desktop.
Save papagunit/e5cd2dca0c0622151a0a6f5e3856616d to your computer and use it in GitHub Desktop.
Pseudo click VBA game
Sure, here’s an example of a simple visual game that can be run in Excel using VBA:
Open a new Excel workbook and press Alt + F11 to open the Visual Basic Editor.
In the editor, insert a new module by clicking on “Insert” in the menu bar and selecting “Module”.
Copy and paste the following code into the module:
Option Explicit
Sub PlayGame()
Dim i As Integer
Dim j As Integer
Dim score As Integer
score = 0
For i = 1 To 10
For j = 1 To 10
Cells(i, j).Value = ""
Cells(i, j).Interior.ColorIndex = 0
Next j
Next i
Do While score < 10
i = Int(Rnd() * 10) + 1
j = Int(Rnd() * 10) + 1
If Cells(i, j).Value = "" Then
Cells(i, j).Value = "X"
Cells(i, j).Interior.ColorIndex = 3
score = score + 1
End If
Application.Wait (Now + TimeValue("0:00:01"))
For i = 1 To 10
For j = 1 To 10
If Cells(i, j).Value = "X" Then
Cells(i, j).Value = ""
Cells(i, j).Interior.ColorIndex = 0
End If
Next j
Next i
Loop
MsgBox "Game over! Your score is " & score & "."
End Sub
Save the module and return to the Excel workbook.
In the worksheet, select any cell and assign the macro “PlayGame” to it by right-clicking on the cell, selecting “Assign Macro”, and choosing “PlayGame” from the list of available macros.
Click on the cell to start the game.
The game will display a 10x10 grid of cells. Every second, a random cell will be highlighted with an “X”. The player must click on the cell with the “X” before it disappears. The game ends when the player has clicked on 10 cells. A message box will display the player’s score.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment