Monday, March 23, 2020

Excel Geometry Dash

In this post we see how to create the game of Geometry Dash in Excel using VBA time events to generate movement of a shape object along a display full of obstacles. Excel Geometry Dash is a fun game and allows to easily create your own levels by simply filling cells and drawing spikes in a separate Excel worksheet. Try it out!


Geometry Dash

Geometry Dash is a rather modern game released in 2013 and inspired by The Impossible Game (released a few years earlier). It consists of a vehicle (a cube in its most simple form) moving along a display full of obstacles. Users cannot control the speed of the vehicle but can make it jump to avoid the obstacles.

The Cube

In order to add and move the main character in Excel Geometry Dash, the cube, we add a class module (clsCube) with the following procedures: AddCube, MoveCube, GlowCube, UnglowCube, ClearCube (see the VBA code under clsCube).

Class modules subroutines are called differently than regular modules (we have already seen that in Excel Battleship). Here’s how you refer to the class to add a new cube:

 
  Dim myCube As Object
  Set myCube = New clsCube
  myCube.AddCube
 


In AddCube we use the AddShape method and set the coordinates and position as indicated below:

 
  Dim newCube As Object
  Set newCube = Game.Shapes.AddShape(msoShapeRectangle, c, f, d, d)
 

Where c and f indicate the horizontal and vertical coordinates of the position and d indicates the length of the edge (arista). In this case, we use d twice as it is a square shape.

Then we name and fill the shape as indicated below. We will use that name to refer to the cube in other procedures of Excel Geometry Dash.

 
  With newCube
      .Name = “mycube”
           With .Fill
              .Solid
              .ForeColor.SchemeColor = 3
          End With
  End With
 


Cube Jump

This is probably the most complicated part of the Excel Geometry Dash game. There are different ways to do it, here’s just one possible solution. We create a number of Boolean variables to determine the state of the cube. 

jump = True, to start jump movement ‘f=f-20

jumpup = True, when is moving up ‘f=f-20

jumpuptop = True, when it reached the highest point in the jump ‘no change to f

jumpfallprep = True, when preparing to start descent ‘f=f+10

jumpfallcome = True, to start falling ‘f=f+10

jumpfall = True, when is moving down ‘f=f+20
 

Through each of those states, the vertical position of the shape (f) will change by subtracting 20 units to the initial value when moving up and adding 10 or 20 when falling. The cube will remain steady at the top for a while to create the gravity jump movement effect. The MoveCube procedure in the clsCube class module will update the cube’s position.

 
  Sub MoveCube()
      With Game.Shapes("mycube")
          .Left = c
          .Top = f
          .Rotation = rot
      End With
  End Sub
 


During the jump action in Excel Geometry Dash, the cube will be rotating by changing the value of the variable “rot”, which sets the inclination of the shape through Shapes(“mycube”).Rotation.
The jump is triggered with the Arrow Up keystroke using Application.OnKey "{UP}". But both jump and jumpfall will also be triggered when the cube has no objects underneath and is above the starting level (f=292).

Background Movement

The background display including all obstacles is copied from the “Level1” tab to the main Excel Geometry Dash console (“Game”). Users can create new levels by simply modifying the content of that tab and using any interior colors other than black and white. Spikes can also be manually entered anywhere using the forward and backward slashes (“/\”).

  
 
The moving effect in Excel Geometry Dash is achieved by deleting a range of cells at the beginning of the worksheet (and behind the cube’s position) every certain time in milliseconds (in this game it was set to 80 ms). That movement is triggered by time events under the “Timer” module, calling the “move” procedure. Please note the SetTimer function declaration code is working for 64bit systems and it is slightly different if you have a 32bit machine. This was already used and explained in Excel Tron Game.

Sorting Obstacles

Conditions will check the position of the cube relative to the background obstacles every time the “move” procedure is called. Conditions check for interior color and value in cells within and right in front the cube’s position.  


 
The cube will crash if cells have an interior color other than black or a value other than nothing. In that case, the CubeGlow procedure is triggered, indicating the cube has crashed, and the game will resume starting a new attempt from the beginning. 

The goal of Excel Geometry Dash game is to avoid all obstacles and reach the white finish line. There is a condition checking for white cells to complete the level.

 
 
 

No comments:

Post a Comment

Popular Posts