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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
' WinAPI stuff:
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Declare Sub GetCursorPos Lib "user32" (ByRef lpoint As POINTAPI)
Declare Function ShowCursor Lib "user32" (ByVal bShow As Long) As Long

Type POINTAPI
  x As Long
  y As Long
End 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 1
End Sub

Sub 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").Value
End 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 (8)

  1. Nick says:

    This is absolutely awesome.  I will never look at Excel the same way!

  2. Rama Sastry says:

    Hw to run this?? Hw to save ds file??

  3. baby games says:

    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.

  4. baby gmes says:

    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.  

  5. VBnewbie says:

    how can i change the rectangle into a picture?

    1. Starling says:

      Change this line:

      ActiveSheet.Shapes.AddPolyline(Points).Name = carID

      for this:

      ActiveSheet.Shapes.AddPicture(“[your image route]\[your image].[png, bmp, ….]”, True, True, carX, carY, [your image width], [your image weigth]).Name = carID

  6. lino says:

    I cannot run this game. its a compile error. cannot define a public user- defined type within an object module.
    how i correct this?

Skip to main content