Play now here
Screen.Recording.2024-10-14.140351.mp4
delay(milliseconds: number): void
: delay the execution by milliseconds.g_has(name: string): boolean
: check if a global variable namedname
exists.g_get(name: string): any
: get the value of the global variable namedname
.g_set(name: string, value: any): void
: set the value of the global variable namedname
tovalue
. Global variables persist through different executions of theloop()
function.print(...args: any[]): void
: print the provided values in the "Output" tab.read(pin: number)
: read a value from the device at the specific GPIO pin. All device pins can be found in constants defined at the top of the code.write(pin: number, value: number)
: write a value to the device at the specific GPIO pin. All device pins can be found in constants defined at the top of the code.
The game is deployed at https://stellarscript.study. Only "Sandbox" mode is implemented at the time of writing this post.
This is some sample code you can copy paste to understand how certain functions work
const ENGINE_LEFT = 0;
const ENGINE_RIGHT = 1;
const SENSOR_LEFT = 2;
const SENSOR_RIGHT = 3;
const WEAPON = 4;
const LOW = 0;
const HIGH = 1;
// This function is called every cycle forever.
function loop() {
// Read sensor values
const leftSensorValue = read(SENSOR_LEFT);
const rightSensorValue = read(SENSOR_RIGHT);
// Determine if an asteroid is detected
const isAsteroidDetected = leftSensorValue === 1 || rightSensorValue === 1;
// If an asteroid is detected, turn the appropriate engine on and shoot a bullet
if (isAsteroidDetected) {
if (leftSensorValue === 1) {
// Asteroid is on the left side, turn left engine on and shoot a bullet
write(ENGINE_LEFT, HIGH);
write(ENGINE_RIGHT, LOW);
write(WEAPON, HIGH);
} else {
// Asteroid is on the right side, turn right engine on and shoot a bullet
write(ENGINE_RIGHT, HIGH);
write(ENGINE_LEFT, LOW);
write(WEAPON, HIGH);
}
} else {
// No asteroid detected, move forward by turning both engines on
write(ENGINE_LEFT, HIGH);
write(ENGINE_RIGHT, HIGH);
}
}
- Using nodejs and npm
- Run the
npm install
to install dependencies - Run the
npm run dev
to run the development server to test out changes- Webpack will build the typescript into javascript
- Webpack dev server will host the script in a little server on http://localhost:8082/
- Run
npm run build:dev
to produce javascript bundles for debugging in thedist/
folder - Run
npm run build:prod
to produce javascript bundles for production (minified) in thedist/
folder