Virtual dice dots not being drawn

I can’t figure out why the dots aren’t being drawn after the random variable is decided. Please help. The point is to make the die “roll around” 7 times slower each time, and then stopping.

//LED Dice Virtual Simulation By Harie, Xiang, Michael, Muhammed

float r = 6;
int rr = 6;
int d = 300;

void setup() {
  size(500, 500);
  background(0, 255, 255);
}

void draw() { 
  fill(255, 0, 0);
  ellipse(250, 35, 30, 30);
}

void mousePressed() {
  float distance = dist(mouseX, mouseY, 250, 35);
  if (distance < 15) {
   for (int i = 0; i < 7; i++){
   d = d + 200;
   delay(d);
   r = random(5);
   rr = round(r);
   println(rr);
   draw();
    if(rr == 0) {
    //draw();
    fill(255);
    rect(10, 10, 480, 480, 10/5);
    fill(0);
    ellipse(250, 250, 50, 50);
    delay(d);
    } else if (rr == 1) {
    //draw();
    fill(255);
    rect(10, 10, 480, 480, 10/5);
    fill(0);
    ellipse(400, 100, 50, 50);
    ellipse(100, 400, 50, 50);
    } else if (rr == 2) {
    //draw();
    fill(255);
    rect(10, 10, 480, 480, 10/5);
    fill(0);
    ellipse(100, 100, 50, 50);
    ellipse(250, 250, 50, 50);
    ellipse(400, 400, 50, 50);
    } else if (rr == 3) {
    //draw();
    fill(255);
    rect(10, 10, 480, 480, 10/5);
    fill(0);
    ellipse(100, 100, 50, 50);
    ellipse(100, 400, 50, 50);
    ellipse(400, 100, 50, 50);
    ellipse(400, 400, 50, 50);
    } else if (rr == 4) {
    //draw();
    fill(255);
    rect(10, 10, 480, 480, 10/5);
    fill(0);
    ellipse(100, 100, 50, 50);
    ellipse(100, 400, 50, 50);
    ellipse(400, 100, 50, 50);
    ellipse(400, 400, 50, 50);
    ellipse(250, 250, 50, 50);
    } else if (rr == 5) {
    //draw();
    fill(255);
    rect(10, 10, 480, 480, 10/5);
    fill(0);
    ellipse(100, 100, 50, 50);
    ellipse(100, 400, 50, 50);
    ellipse(400, 100, 50, 50);
    ellipse(400, 400, 50, 50);
    ellipse(100, 250, 50, 50);
    ellipse(400, 250, 50, 50);
    } else {
    fill(255);
    rect(10, 10, 480, 480, 10/5);
    fill(255, 0, 0);
    ellipse(250, 100, 30, 30);
   }
   }
   } else{
   r = 6;
   }
}
1 Like

you dont call mousePressed in draw, and only one draw can be called

1 Like

How do I get mouse input without mousePressed then? Also, it still doesn’t work without all the draws. Thanks.

i think what @Sky
wanted to point out is you need a different program structure:

use void mousePressed()

to change a setting, not call it from draw

use draw to draw something

not try to draw from mousePressed()

start from something like

float rr = 200;

void setup() {
  size(200,200);
  println("mouse click makes the magic");
  fill(0,200,0);
  stroke(200,0,0);
}
void draw() {
  background(200,200,0);
  translate(width/2,height/2);
  ellipse(0,0,rr,rr);
}
void mousePressed() {
  rr = random(5,width);
}

now put your dice circles in draw

  • but better by a extra function with FOR loop…
    there is actually a easy trick for a 3 * 3 grid of points

now somewhere you need to
generate the dice number 1 …6 ( i do mouseReleased() )
and translate it to the ON/OFF pattern 9 points ( 0 … 8 ) grid
for this need a array, but not for position of points…
and depending on pattern[dice] change color of points.

example
int x=50, y=x, w=50, h=w, grid=3, many=grid*grid, diam=30;
int dice = 0;
int[][] points = {
  {0, 0, 0, 0, 0, 0, 0, 0, 0}, // none
  {0, 0, 0, 0, 1, 0, 0, 0, 0}, //1
  {0, 0, 1, 0, 0, 0, 1, 0, 0}, //2
  {0, 0, 1, 0, 1, 0, 1, 0, 0}, //3
  {1, 0, 1, 0, 0, 0, 1, 0, 1}, //4
  {1, 0, 1, 0, 1, 0, 1, 0, 1}, //5
  {1, 0, 1, 1, 0, 1, 1, 0, 1}, //6
  {1, 1, 1, 1, 1, 1, 1, 1, 1}    //all
};
PShape squircle;
float ang = 0;           // animation: dice rolling

void setup() {
  size(200, 200);
  println("mouse click makes the magic");
  noStroke();                                      
  my_squircle(9, 9, 9);
}
void draw() {
  background(200, 200, 0);
  draw_dice();
}

void mousePressed() {
  dice = 7;
}

void mouseReleased() {
  dice = int(random(1, 7));
  println("new dice: "+dice);
}

void draw_dice() {
  if ( dice == 7 ) {                                          // while mouse pressed // dice rolling
    translate(width/2, height/2);
    ang += 0.15;
    rotate(ang);
    translate(-width/2, -height/2);
  }
  shape(squircle, width/2, height/2);
  for (int i = 0; i < many; i++) {
    fill(180, 180, 180);                                     // same dice squircle border, 190 would be invisible
    if ( points[dice][i] == 1 ) fill(200, 0, 0);
    ellipse(x+(i%grid)*w, y+(floor(i/grid))*h, diam, diam);
  }
}

void my_squircle(float r, float wx, float wy) {
  float x1, y1;
  squircle = createShape();
  squircle.beginShape();
  squircle.stroke(180, 180, 180);
  squircle.strokeWeight(5);
  squircle.fill(190, 190, 190);
  for (float t = 0; t < TWO_PI; t += TWO_PI/160) {
    x1 = pow(abs(cos(t)), 0.5) * r*wx * sign(cos(t));
    y1 = pow(abs(sin(t)), 0.5) * r*wy * sign(sin(t));
    squircle.vertex(x1, y1);
  }
  squircle.endShape(CLOSE);
}

float sign(float input) {
  if (input < 0)  return -1.0;
  if (input > 0)  return  1.0;
  return 0.0;
}


a rolling dice:
2019-01-15_21-15-23_snap

1 Like

Thank you very much.