Share via


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

Comments

  • Anonymous
    January 29, 2009
    PingBack from http://www.clickandsolve.com/?p=1998

  • Anonymous
    January 30, 2009
    This is absolutely awesome.  I will never look at Excel the same way!

  • Anonymous
    July 02, 2012
    Hw to run this?? Hw to save ds file??

  • Anonymous
    April 09, 2014
    Prior  to planning it is every important plan because when you invite the people for the party then you will send the invitation cards or you will send email to the friends. so it si the big plan for the shower party. if there are any other  pregnant women in shower party then you can site down on comfortable place. because the pregnant woman his a lot of pain. you will take care of pregnant women. Funny games you will select the funny games for the party because some people like the funny games and they wants to entertain the different games to play in shower party. there are a lot games baby which you can play in shower party.

  • Anonymous
    April 10, 2014
    the shower party is the occasion of the enjoyments and most of the people come to party with very keen and they wants to enjoy the shower party. you also invite your friend and your family member for the shower party.  

  • Anonymous
    May 16, 2014
    how can i change the rectangle into a picture?

    • Anonymous
      April 20, 2016
      The comment has been removed
  • Anonymous
    August 31, 2016
    I cannot run this game. its a compile error. cannot define a public user- defined type within an object module.how i correct this?