This project provides basic API for creating simple board games.
- Create a new directory inside the main directory.
- Copy there
game.jsandindex.htmlfiles fromemptydirectory. - Edit
game.jsyou may find there two important functions that your file must have:initialize()This function will be called before your program runs. It should contain any code which is necessary to initialize the program, e.g. setting board size, setting speed of refreshing board, initializing variables, etc. If you don't need to set anything specific before running the program, this may be left empty (but not removed!).nextStep()This function should contain code that will be executed with each step of the program.
Below you can see what already defined functions and constants you may utilize in your game.js file.
Board is always square and contains NxN cells.
Returns board's size.
If board is 6x6, it will return 6.
Sets board size to newSize. newSize must be from range [5, 512].
Makes board empty.
Fills a cell at (x, y) with color (if provided, otherwise with default color).
Draws an image at (x, y) with specified with and height in cells if provided.
Clears cell at (x, y).
Returns false if cell at (x, y) is empty, true if filled.
You can utilize up, down, left, right arrow keyboard keys using the following functions and constants.
You can use the following global constants, which reflect arrow keyboard keys:
- KEY_RIGHT
- KEY_LEFT
- KEY_UP
- KEY_DOWN
- KEY_EMPTY - when none of the above keys was pressed
Returns a constant (described above) that represents key which was pressed last time (even a few steps before).
Clears recent key value.
At the beginning getRecentKey() will return KEY_EMPTY.
Program runs, in step 7 user presses UP key and in step 15 presses DOWN.
Result:
In steps 0-6, getRecentKey() will return KEY_EMPTY.
In steps 7-14, getRecentKey() will return KEY_UP.
In steps 15+, getRecentKey() will return KEY_DOWN.
Returns a constant (described above) that represents key which was pressed exactly during the step in which we are currently. Otherwise returns KEY_EMPTY.
Program runs, in step 7 user presses UP key and in step 15 presses DOWN.
Result:
In steps 0-6, getLastStepKey() will return KEY_EMPTY.
In step 7, getLastStepKey() will return KEY_UP.
In steps 8-14, getLastStepKey() will return KEY_EMPTY.
In step 15, getLastStepKey() will return KEY_DOWN.
In steps 16+, getLastStepKey() will return KEY_EMPTY.
Sets speed of making next steps.