Home Codebug Random dice example for a codebug

Random dice example for a codebug

by iainhendry

A random dice example for a codebug

 

Code

Python

# To run this code on CodeBug, visit:
#
#     http://www.codebug.org.uk/learn/activity/66/tethering-codebug-with-python/#step585
#
import codebug_tether
from codebug_tether import (IO_DIGITAL_OUTPUT,
                            IO_DIGITAL_INPUT,
                            IO_PWM_OUTPUT,
                            IO_ANALOG_INPUT)
import random
from codebug_tether.sprites import Sprite

random_number = None

codebug = codebug_tether.CodeBug()
codebug.set_leg_io(0, IO_DIGITAL_INPUT);
codebug.set_leg_io(1, IO_DIGITAL_INPUT);
codebug.set_leg_io(2, IO_DIGITAL_INPUT);
codebug.set_leg_io(3, IO_DIGITAL_INPUT);
codebug.set_leg_io(4, IO_DIGITAL_INPUT);
codebug.set_leg_io(5, IO_DIGITAL_INPUT);
codebug.set_leg_io(6, IO_DIGITAL_INPUT);
codebug.set_leg_io(7, IO_DIGITAL_INPUT);

def build_sprite(rows):
  s = Sprite(5, 5)
  for i in range(5):
    s.set_row(i, rows[i])
  return s


while True:
  if codebug.get_input('A') == 1:
    random_number = random.randint(1, 6)
    if random_number == 1:
      codebug.draw_sprite(0, 0, build_sprite([0b00000,0b00000,0b00100,0b00000,0b00000]));
    elif random_number == 2:
      codebug.draw_sprite(0, 0, build_sprite([0b00001,0b00000,0b00000,0b00000,0b10000]));
    elif random_number == 3:
      codebug.draw_sprite(0, 0, build_sprite([0b00001,0b00000,0b00100,0b00000,0b10000]));
    elif random_number == 4:
      codebug.draw_sprite(0, 0, build_sprite([0b10001,0b00000,0b00000,0b00000,0b10001]));
    elif random_number == 5:
      codebug.draw_sprite(0, 0, build_sprite([0b10001,0b00000,0b00100,0b00000,0b10001]));
    elif random_number == 6:
      codebug.draw_sprite(0, 0, build_sprite([0b10001,0b00000,0b10001,0b00000,0b10001]));

Javascript

var random_number;


codebug_direction('U');
codebug_sleepafter(3);
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);


while (true) {
  if (io_get_input('A') == 1) {
    random_number = random_range(1, 6);
    if (random_number == 1) {
      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 (random_number == 2) {
      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 (random_number == 3) {
      fivebyfivedisplay.sprite_render(0, 0, sprite_build([[0, 0, 0, 0, 1], [0, 0, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 0, 0], [1, 0, 0, 0, 0]]));
      fivebyfivedisplay.update();
    } else if (random_number == 4) {
      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 (random_number == 5) {
      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 (random_number == 6) {
      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();
    }
  }
}

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More