This project creates a virtual dice that can be rolled at the click of a button.
Connect the OLED Display Brick to Port 4E, Pushbutton Brick to Port 3A, and Buzzer Brick to Port 2C.
Run the following script:
var screen = new OledDisplay();
var buzzer = new Buzzer(PORT_2C);
var pb = new Pushbutton(PORT_3A);
screen.clear();
screen.drawString(15, 25, "Press button");
screen.drawString(15, 45, "to roll dice");
while(true) {
while(!pb.clicked());
var lastnum = 0;
var throws = random(10, 20);
for (var i=0; i<throws; i++) {
while((num = random(1, 6)) == lastnum);
lastnum = num;
draw_dice(num);
buzzer.playTone(50, (i+5)*5);
}
}
function draw_dice(num) {
const radius = 7;
const offx = (screen.width() - screen.height()) / 2;
screen.startBuffer();
screen.clear();
screen.drawRect(offx, 0, screen.height()-1, screen.height()-1);
switch(num) {
case 1:
screen.drawEllipse(offx+(screen.height()/2), screen.height()/2, radius, radius, true);
break;
case 2:
screen.drawEllipse(offx+screen.height()/3, screen.height()/3, radius, radius, true);
screen.drawEllipse(offx+screen.height()/3*2, screen.height()/3*2, radius, radius, true);
break;
case 3:
screen.drawEllipse(offx+screen.height()/4, screen.height()/4, radius, radius, true);
screen.drawEllipse(offx+screen.height()/2, screen.height()/2, radius, radius, true);
screen.drawEllipse(offx+screen.height()/4*3, screen.height()/4*3, radius, radius, true);
break;
case 4:
screen.drawEllipse(offx+screen.height()/4, screen.height()/4, radius, radius, true);
screen.drawEllipse(offx+screen.height()/4, screen.height()/4*3, radius, radius, true);
screen.drawEllipse(offx+screen.height()/4*3, screen.height()/4, radius, radius, true);
screen.drawEllipse(offx+screen.height()/4*3, screen.height()/4*3, radius, radius, true);
break;
case 5:
screen.drawEllipse(offx+screen.height()/2, screen.height()/2, radius, radius, true);
screen.drawEllipse(offx+screen.height()/4, screen.height()/4, radius, radius, true);
screen.drawEllipse(offx+screen.height()/4, screen.height()/4*3, radius, radius, true);
screen.drawEllipse(offx+screen.height()/4*3, screen.height()/4, radius, radius, true);
screen.drawEllipse(offx+screen.height()/4*3, screen.height()/4*3, radius, radius, true);
break;
case 6:
screen.drawEllipse(offx+screen.height()/4, screen.height()/4, radius, radius, true);
screen.drawEllipse(offx+screen.height()/4, screen.height()/4*3, radius, radius, true);
screen.drawEllipse(offx+screen.height()/4*3, screen.height()/4, radius, radius, true);
screen.drawEllipse(offx+screen.height()/4*3, screen.height()/4*3, radius, radius, true);
screen.drawEllipse(offx+screen.height()/4, screen.height()/2, radius, radius, true);
screen.drawEllipse(offx+screen.height()/4*3, screen.height()/2, radius, radius, true);
break;
}
screen.flushBuffer();
}
Here's how it looks in action: