Home Codebug Magic 8 ball on a Codebug example

Magic 8 ball on a Codebug example

by iainhendry

This is magic 8 ball running on a codebug

CodeBug™ is a fun, engaging, entry level introduction to simple programming and electronic concepts to anyone, at any age. It is easy to program using the online interface, which features colourful drag and drop blocks, an in-browser emulator and engaging community features. It enables you to create your own games, clothes, robots or whatever other inventions you have in mind.
  • Plugs onto Raspberry Pi GPIO as an add-on
  • Starting point for Internet of Things devices
  • Versatile I/O interface to connect lights, switches, sensors, motors and more
  • 5×5 Red LED grid with 2 user buttons
  • 6 Touch sensitive I/O pads (4 I/O, power and ground)
  • Drag and drop programming from web browser
  • Windows, Mac and Linux compatible
  • Micro USB connector for programming and power
  • Expansion port for I2C, SPI and UART
  • Online project storage and sharing

 

 

Code

Python code

# 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 StringSprite

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);


while True:
  Random_Number = random.randint(0, 9)
  # interrupts -- and therefore sleep -- not supported in Python
  if Random_Number == 0:
    codebug.scroll_sprite(StringSprite('It will happen', 'R'), 100/1000, 'R')
  elif Random_Number == 1:
    codebug.scroll_sprite(StringSprite('YOU WILL DIE', 'R'), 100/1000, 'R')
  elif Random_Number == 2:
    codebug.scroll_sprite(StringSprite('Your future is uncertain', 'R'), 100/1000, 'R')
  elif Random_Number == 3:
    codebug.scroll_sprite(StringSprite('Failure awaits', 'R'), 100/1000, 'R')
  elif Random_Number == 4:
    codebug.scroll_sprite(StringSprite('Success awaits', 'R'), 100/1000, 'R')
  elif Random_Number == 5:
    codebug.scroll_sprite(StringSprite('it is challengling', 'R'), 100/1000, 'R')
  elif Random_Number == 6:
    codebug.scroll_sprite(StringSprite('It will fail', 'R'), 100/1000, 'R')
  elif Random_Number == 7:
    codebug.scroll_sprite(StringSprite('It is doomed', 'R'), 100/1000, 'R')
  elif Random_Number == 8:
    codebug.scroll_sprite(StringSprite('Retry Fate is awaiting you', 'R'), 100/1000, 'R')
  elif Random_Number == 9:
    codebug.scroll_sprite(StringSprite('It is Certain', 'R'), 100/1000, 'R')

JavaScript Code

var Random_Number;


codebug_direction('U');
codebug_sleepafter(5);
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) {
  Random_Number = random_range(0, 9);
  sleep_until_input();
  if (Random_Number == 0) {
    fivebyfivedisplay.sprite_scroll((new StringSprite('It will happen', 'R')), 100, 'R');
  } else if (Random_Number == 1) {
    fivebyfivedisplay.sprite_scroll((new StringSprite('YOU WILL DIE', 'R')), 100, 'R');
  } else if (Random_Number == 2) {
    fivebyfivedisplay.sprite_scroll((new StringSprite('Your future is uncertain', 'R')), 100, 'R');
  } else if (Random_Number == 3) {
    fivebyfivedisplay.sprite_scroll((new StringSprite('Failure awaits', 'R')), 100, 'R');
  } else if (Random_Number == 4) {
    fivebyfivedisplay.sprite_scroll((new StringSprite('Success awaits', 'R')), 100, 'R');
  } else if (Random_Number == 5) {
    fivebyfivedisplay.sprite_scroll((new StringSprite('it is challengling', 'R')), 100, 'R');
  } else if (Random_Number == 6) {
    fivebyfivedisplay.sprite_scroll((new StringSprite('It will fail', 'R')), 100, 'R');
  } else if (Random_Number == 7) {
    fivebyfivedisplay.sprite_scroll((new StringSprite('It is doomed', 'R')), 100, 'R');
  } else if (Random_Number == 8) {
    fivebyfivedisplay.sprite_scroll((new StringSprite('Retry Fate is awaiting you', 'R')), 100, 'R');
  } else if (Random_Number == 9) {
    fivebyfivedisplay.sprite_scroll((new StringSprite('It is Certain', 'R')), 100, 'R');
  }
}

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