1.5k
A simple dice game on a codebug
Go to http://www.codebug.org.uk/create/ and paste the code in, the good thing is this also generates a Python and blockly example
Code
[codesyntax lang=”javascript”]
var delay; var pressed; var count; codebug_direction('U'); codebug_sleepafter(1); io_configure(0, IO_DIGITAL_INPUT); io_configure(1, IO_DIGITAL_INPUT); io_configure(2, IO_DIGITAL_INPUT); io_configure(3, IO_DIGITAL_INPUT); io_configure(4, IO_DIGITAL_INPUT); io_configure(5, IO_DIGITAL_INPUT); io_configure(6, IO_DIGITAL_INPUT); io_configure(7, IO_DIGITAL_INPUT); io_configure_pullup(0, 0); io_configure_pullup(2, 0); io_configure_pullup(3, 0); io_configure_pullup(4, 0); io_configure_pullup(5, 0); delay = 60; pressed = 0; count = 1; while (true) { if (io_get_input('A') == 1) { pressed = 1; fivebyfivedisplay.clear(); fivebyfivedisplay.update(); while (io_get_input('A') == 1) { count = count + 1; } count = count % 6; } if (1 == pressed) { fivebyfivedisplay.sprite_render(0, 0, sprite_build([[1, 1, 1, 1, 1], [1, 0, 0, 0, 1], [1, 0, 0, 0, 1], [1, 0, 0, 0, 1], [1, 1, 1, 1, 1]])); fivebyfivedisplay.update(); sleep(delay); fivebyfivedisplay.sprite_render(0, 0, sprite_build([[1, 1, 1, 1, 1], [1, 0, 0, 0, 1], [1, 0, 0, 0, 1], [0, 0, 0, 0, 1], [0, 0, 1, 1, 1]])); fivebyfivedisplay.update(); sleep(delay); fivebyfivedisplay.sprite_render(0, 0, sprite_build([[1, 1, 1, 1, 1], [0, 0, 0, 0, 1], [0, 0, 0, 0, 1], [0, 0, 0, 0, 1], [0, 0, 0, 0, 1]])); fivebyfivedisplay.update(); sleep(delay); fivebyfivedisplay.sprite_render(0, 0, sprite_build([[0, 0, 0, 1, 0], [0, 1, 0, 0, 1], [1, 0, 0, 0, 1], [0, 0, 0, 0, 1], [0, 0, 0, 0, 0]])); fivebyfivedisplay.update(); sleep(delay); fivebyfivedisplay.sprite_render(0, 0, sprite_build([[0, 0, 1, 0, 0], [0, 1, 0, 1, 0], [1, 0, 0, 0, 1], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]])); fivebyfivedisplay.update(); sleep(delay); fivebyfivedisplay.sprite_render(0, 0, sprite_build([[0, 0, 1, 0, 0], [0, 1, 0, 1, 0], [1, 0, 0, 0, 1], [0, 1, 0, 1, 0], [0, 0, 0, 0, 0]])); fivebyfivedisplay.update(); sleep(delay); fivebyfivedisplay.sprite_render(0, 0, sprite_build([[0, 0, 1, 0, 0], [0, 1, 0, 1, 0], [1, 0, 0, 0, 1], [0, 1, 0, 1, 0], [0, 0, 1, 0, 0]])); fivebyfivedisplay.update(); sleep(delay); fivebyfivedisplay.sprite_render(0, 0, sprite_build([[0, 0, 0, 0, 0], [0, 0, 1, 0, 0], [0, 1, 0, 1, 0], [0, 0, 1, 0, 0], [0, 0, 0, 0, 0]])); fivebyfivedisplay.update(); sleep(delay); if (0 == count) { count = 6; } if (1 == count) { fivebyfivedisplay.sprite_render(0, 0, sprite_build([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]])); fivebyfivedisplay.update(); } else if (2 == count) { fivebyfivedisplay.sprite_render(0, 0, sprite_build([[0, 0, 0, 0, 1], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [1, 0, 0, 0, 0]])); fivebyfivedisplay.update(); } else if (3 == count) { fivebyfivedisplay.sprite_render(0, 0, sprite_build([[1, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 1]])); fivebyfivedisplay.update(); } else if (4 == count) { fivebyfivedisplay.sprite_render(0, 0, sprite_build([[1, 0, 0, 0, 1], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [1, 0, 0, 0, 1]])); fivebyfivedisplay.update(); } else if (5 == count) { fivebyfivedisplay.sprite_render(0, 0, sprite_build([[1, 0, 0, 0, 1], [0, 0, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 0, 0], [1, 0, 0, 0, 1]])); fivebyfivedisplay.update(); } else if (6 == count) { fivebyfivedisplay.sprite_render(0, 0, sprite_build([[1, 0, 0, 0, 1], [0, 0, 0, 0, 0], [1, 0, 0, 0, 1], [0, 0, 0, 0, 0], [1, 0, 0, 0, 1]])); fivebyfivedisplay.update(); } pressed = 0; } }
[/codesyntax]
To test this out , simply upload the example – following the instructions on the website. Press the A button to roll the dice