Monday, April 23, 2018

Excel Falling Letters

In a previous post we learned to move objects in the Excel worksheet. Here we’ll use the same technique to move text boxes containing letters, thus building the Excel Falling Letters game. Keystroke capture similar to that used in the Excel Tron Game through the Application.Onkey method will be used to check if the falling letter matches the user’s keyboard stroke.


Adding Letters

In order to add letters we will use the Shapes.AddTextbox method and set the coordinates and position of the text box (see the “addbox” subroutine).  We will name each box with consecutive numbers added to the prefix “mytb”. The letter will be introduced with the property .TextFrame.Characters.Text on the Textbox Shape. We convert a random numeric variable (num(tb)) into the letter or character with the VBA function Chr along the corresponding Ascii code. Find the full list of Ascii codes in this other page: ASCII Characters Code Set

Another random variable (colindex) will determine its color through the TextFrame.Characters.Font. ColorIndex property. The “cleantb” subroutine deletes all text boxes in the worksheet at the game start.


An internal counter will determine when new letters are added. The rate will depend on the level and it is specified in the “level” subroutine within the Levels module.

Timer Events

In order to achieve the movement of falling letters we need to call a “moving” procedure that will change the position of the text boxes every certain time interval. As we learned in previous posts, we need to use time intervals of milliseconds to create the sensation of a smooth continuous movement by declaring a SetTimer function (check the Excel Tron Game post for more details if this is new to you).

Capture Keystroke

Keystroke is captured with the Application.Onkey method. But we will need to define and link a subroutine for each key in scope of the game (i.e. the whole alphabet). 



The “catch” subroutine within the Keys module will check if the keystroke is matching any of the falling letters. If so, it will delete the letter in that textbox (completely deleting the textbox is not an option as it would create some conflicts with code as it is now) and add score points. Otherwise, it will red/blue flash the playing area and deduct points.

And that’s it. Enjoy the game and comment with your scores, especially if you beat me (I couldn’t get further than level 6).
 
 

No comments:

Post a Comment

Popular Posts