Sunday, February 18, 2018

Excel Tron Game

In this post we code a simple game with movement in Excel. Moving things this way around the worksheets may not be quite necessary for office work Excel purposes but it is fun and can help you learn other stuff such as time events. Once you learn this, you will be able to make any other similar games.


Tron was a popular Arcade game in the 80s, you may have had the chance to play or see the movies. You drive a high speed vehicle controlled by the keyboard arrows and have to make your opponent crash before you do.

In order to build the Excel Tron Game, and very much any similar 2D game in Excel, there are three main pillars to consider: Location in the worksheet, time events, and keystroke capture.


Location in the worksheet 

The Excel worksheet object is the “playing ground” and simple movement is achieved using the cells as location coordinate parameters. But first we need to adjust the ColumnWidth and RowHeight of the playground range adequately. This and other display settings are coded in the “Layout” module. In order to better organize our code we will create and properly name the different modules (see below).

The player movement happens in PlayerA module, but it’s closely dependent on the module Keys. Depending on keystroke the player will move in one or other direction. The code will simply fill the corresponding cell with blue color – see the code below.



As per the movement of PlayerB (the computer), it follows the exact same principle, but it needs further programing depending on what you aim to accomplish. In this simple Excel Tron Game version the movement is just checking for edges, cell interior color different than black and PlayerA’s movement coordinates. You can add to this and adapt the module to create higher difficulty levels.

Time events 

In order to achieve continuous movement we need to call the “moving” procedure every certain time interval. This could be easily achieved with the Application.OnTime method. This method allows to call a procedure at a particular time and within time intervals. However, the minimum time interval possible with this method is one second, and we want a faster and more continuous move here. We will talk more about this method in other post and now we will look into another option that allows for time intervals in milliseconds. 

In order to achieve shorter time intervals and create a realistic Excel Tron Game we will declare a SetTimer function that will be calling another procedure (AdressOf “YourMovementProcedure”) every time interval in milliseconds within (“speed” variable).  In this case we are calling “TimerEvent” for error handling and then “PlayerAmove” and the speed has been set at 50 milliseconds. Please note the SetTimer function declaration code is slightly different for 32bit and 64bit Excel versions.
 
 
  Option Explicit
  #If Win64 Then
    Public Declare PtrSafe Function SetTimer Lib "User32" ( _
        ByVal hwnd As LongLong, _
        ByVal nIDEvent As LongLong, _
        ByVal uElapse As LongLong, _
        ByVal lpTimerFunc As LongLong) As LongLong
    Public Declare PtrSafe Function KillTimer Lib "User32" ( _
        ByVal hwnd As LongLong, _
        ByVal nIDEvent As LongLong) As LongLong
    Public TimerID As LongLong
  #Else
    Public Declare PtrSafe Function SetTimer Lib "User32" ( _
        ByVal hwnd As Long, _
        ByVal nIDEvent As Long, _
        ByVal uElapse As Long, _
        ByVal lpTimerFunc As Long) As Long
    Public Declare PtrSafe Function KillTimer Lib "User32" ( _
        ByVal hwnd As Long, _
        ByVal nIDEvent As Long) As Long
    Public TimerID As Long
  #End If
 
  Sub StartTimer()
      Keys.bindKeys
      If TimerID <> 0 Then
          KillTimer 0, TimerID
          TimerID = 0
      End If
      TimerID = SetTimer(0, 0, speed, AddressOf TimerEvent)
  End Sub
 
  Sub StopTimer()
      KillTimer 0, TimerID
      TimerID = 0
      Keys.freeKeys
  End Sub
 
  Sub TimerEvent()
      On Error Resume Next
      PlayerA.PlayerAmove
      Exit Sub
  End Sub
 


Keystroke capture 

As mentioned earlier, the keyboard arrows control the movement in the Excel Tron Game. In Excel VBA keys are capture with the Application.OnKey method. Every key defined with this method will call a procedure programmed to react accordingly. Each procedure will check if the turn is possible (you cannot move backwards in the same direction) and will assign the corresponding increment value (rowBinc or colBinc).

 
  Sub bindKeys()
      Application.OnKey "{LEFT}", "ml"
      Application.OnKey "{UP}", "mu"
      Application.OnKey "{DOWN}", "md"
      Application.OnKey "{RIGHT}", "mr"
      Application.OnKey "{ESC}", "stopGame"
  End Sub

  Sub ml()
      If colAinc <> 1 Then
          colAinc = -1
          rowAinc = 0
      End If
  End Sub

  Sub mu()
       If rowAinc <> 1 Then
           colAinc = 0
           rowAinc = -1
       End If
  End Sub

  Sub mr()
       If colAinc <> -1 Then
           colAinc = 1
           rowAinc = 0
       End If
  End Sub

  Sub md()
       If rowAinc <> -1 Then
           colAinc = 0
           rowAinc = 1
       End If
  End Sub

  Sub freeKeys()
      Application.OnKey "{LEFT}"
      Application.OnKey "{UP}"
      Application.OnKey "{DOWN}"
      Application.OnKey "{RIGHT}"
      Application.OnKey "{ESC}"
  End Sub


Now go ahead and download the Excel Tron Game. You can read the whole code there and play it for fun. With this same principle you can build other games such as Snake, Tetris, PacMan in Excel. 

These concepts can also be useful for your “more professional” Excel office work as you can setup certain key procedures or add timer events to execute some macros.
 
    

No comments:

Post a Comment

Popular Posts