Creating Excel Game (or something similar for fun)

Awhile back I saw really interesting article: Microsoft Excel: Revolutionary 3D Game Engine?
After that I was forced to do small test on that and that of course resulted to this post :-)

I just grabbed the idea and made small “car game” (but in reality it just vaguely reminds of car game) on top of Excel. And here is the result: Car Game example
My car (or just blue box if you wish) can be controlled with mouse. If you press left mouse button it increases the speed of the car (a.k.a. gas pedal). I was too lazy to “invent” brake so car just slowly slows down if user isn’t pressing the left mouse button. User can control the “car” by moving mouse from left to right. Right mouse button quits the game.

Click here for the video.

Since I just wanted to test basic input and drawing mechanisms I didn’t even bother to think of creating any physics. I’ll leave that to you! But here’s the source code for my example:

 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
 ' WinAPI stuff:Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As IntegerDeclare Sub GetCursorPos Lib "user32" (ByRef lpoint As POINTAPI)Declare Function ShowCursor Lib "user32" (ByVal bShow As Long) As LongType POINTAPI  x As Long  y As LongEnd Type Sub GameLoop()  ' Input related variables:  Dim mouseLocation As POINTAPI  Dim mouseLocationPrevious As POINTAPI  ' Get & Set original mouse position:  GetCursorPos mouseLocation  mouseLocationPrevious = mouseLocation  ' Initialize the system:  ActiveSheet.Range("Xdiff").Value = 0  ActiveSheet.Range("Ydiff").Value = 0  ActiveSheet.Range("SteeringWheel").Value = 90  ActiveSheet.Range("Pedal").Value = 0  ActiveSheet.Range("CarX").Value = 250  ActiveSheet.Range("CarY").Value = 250  ' Hide mouse cursor:  ShowCursor 0  ' We're going to loop until mouse click:  While GetAsyncKeyState(vbKeyRButton) = 0    ' Magic:    Sheets("GameBoard").Range("Z_SortArea").Sort _      Sheets("GameBoard").Range("NormalZ"), xlAscending    GetCursorPos mouseLocation    DrawCar "car1", mouseLocation, mouseLocationPrevious    ActiveSheet.Range("CarX").Value = _      ActiveSheet.Range("CarX").Value - ActiveSheet.Range("SpeedY").Value    ActiveSheet.Range("CarY").Value = _      ActiveSheet.Range("CarY").Value - ActiveSheet.Range("SpeedX").Value    mouseLocationPrevious = mouseLocation    ' Let’s take a nap:    Sleep(10)    If GetAsyncKeyState(vbKeyLButton) <> 0 Then      ' User is pressing the gas pedal => More speed:      ActiveSheet.Range("Pedal").Value = _        ActiveSheet.Range("Pedal").Value + ActiveSheet.Range("Accelerate").Value      If ActiveSheet.Range("Pedal").Value > ActiveSheet.Range("MaxPedal").Value Then        ActiveSheet.Range("Pedal").Value = ActiveSheet.Range("MaxPedal").Value      End If    Else      ' User isn't pressing the gas pedal => Take of some speed (if moving):      ActiveSheet.Range("Pedal").Value = _        ActiveSheet.Range("Pedal").Value - ActiveSheet.Range("Brake").Value      If ActiveSheet.Range("Pedal").Value < 0 Then        ActiveSheet.Range("Pedal").Value = 0      End If    End If  End While  ' Bring back the mouse cursor:  ShowCursor 1End SubSub DrawCar(carID As String, _  mouseLocation As POINTAPI, _  mouseLocationPrevious As POINTAPI)  Dim diffX As Double  Dim diffY As Double  Dim Points(1 To 5, 1 To 2) As Single  Dim carX As Double  Dim carY As Double  Dim carWidth As Double  Dim carHeight As Double  carX = ActiveSheet.Range("CarX").Value  carY = ActiveSheet.Range("CarY").Value  carHeight = ActiveSheet.Range("CarSize1").Value  carWidth = ActiveSheet.Range("CarSize2").Value  ' Get input and store current values to sheet:  ActiveSheet.Range("Xdiff").Value = mouseLocationPrevious.x - mouseLocation.x  ActiveSheet.Range("Ydiff").Value = mouseLocationPrevious.y - mouseLocation.y  ActiveSheet.Range("SteeringWheel").Value = _    ActiveSheet.Range("SteeringWheel").Value + ActiveSheet.Range("Xdiff2").Value  Points(1, 1) = carX - carWidth  Points(1, 2) = carY + carHeight  Points(2, 1) = carX + carWidth  Points(2, 2) = carY + carHeight  Points(3, 1) = carX + carWidth  Points(3, 2) = carY - carHeight  Points(4, 1) = carX - carWidth  Points(4, 2) = carY - carHeight  Points(5, 1) = Points(1, 1)  Points(5, 2) = Points(1, 2)  ' Draw this car:  On Error Resume Next  ActiveSheet.Shapes(carID).Delete  Err.Clear ' Deleting Shape that doesn't exist => Error  ActiveSheet.Shapes.AddPolyline(Points).Name = carID  ActiveSheet.Shapes(carID).Rotation = ActiveSheet.Range("SteeringWheel").ValueEnd Sub

I did my tests on Excel 2007 and I used .xlsm format (Macro enabled). You can grab my file from here.

So if you want to open that example it gives you this security warning:

Security warning

You need to click Options... button just below ribbon. And then check Enable this content and click OK:

Security Alert - Macro

And if you want to run the game you can just press Alt-F8 and then hit enter (or click Run):

Macro

Well I have to say that I enjoyed playing with Excel. Maybe some day I’m going to use this for some serious thing or not :-)

Anyways... Happy hacking!

J