Smart Mobile Studio game by Josh Blake: Y12 Age ~17
The code is packed with advanced features and has many constants that you can experiment with to suit your game-playing ability.
Play the game full screen and view the code on subsequent pages. If SpaceInvaders does not run in your current browser, please try another (such as Chrome).
The code has suitably named identifiers and should be straightforward for a professional programmer to follow, but we think that the following paragraph might help the majority of you to understand how Josh handles key presses. (His handling of key-up events is similar).
Tracing the code for a key press, the asm block in procedure ApplicationStarting calls procedure KeyDownEvent (which is in the Input unit). If DEBUG (in the Settings unit) is True, procedure KeyDownEvent logs the press and also outputs to the console the position of each callback procedure (of type procedure(mCode: Integer)) that have been added to the array of procedures. Whether or not in debug mode, procedure KeyDownEvent calls each procedure in the array of key-down procedures. Procedures are added to the array of key-down procedures by AddKeyDown procedure in the Input unit. This is called in procedure ApplicationStarting, which adds KeyDown from the SpaceInvaders unit to handle the space bar. The constructor for the Player adds its own KeyDown procedure to the array to handle the arrow keys.
The code benefits from:
- object-oriented code;
- good use of inheritance with virtual procedures and overrides;
- procedure type and arrays of that type;
- divided into appropriate units (with drawing procedures with Canvas as parameter in separate unit);
- debug mode with logging to the console e.g.
- enumerated types;
- good use of own functions and the inbuilt routines IntToStr, FloatToStr, Low, High, Add, Random, Now, Floor, FillRectF and FillText;
- use of KeyUp and KeyDown procedures in such a way that key input still works after code packing and code obfuscation.