# Virtual dice dots not being drawn

**URL:** <https://discourse.processing.org/t/virtual-dice-dots-not-being-drawn/7508>\
**Category:** Coding Questions\
**Created:** [January 14, 2019, 3:44pm UTC](https://discourse.processing.org/t/virtual-dice-dots-not-being-drawn/7508 "2019-01-14T15:44:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harie](https://avatars.discourse-cdn.com/v4/letter/h/8e7dd6/32.png) [@Harie](https://discourse.processing.org/u/Harie)\
**Post date:** [January 14, 2019, 3:44pm UTC](https://discourse.processing.org/t/virtual-dice-dots-not-being-drawn/7508/1 "2019-01-14T15:44:15Z")

</div>

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.

```auto
//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;
   }
}

```

---

<div class="post-metadata">

**Author:** ![Sky](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sky/32/3013_2.png) [@Sky](https://discourse.processing.org/u/Sky)\
**Post date:** [January 14, 2019, 6:36pm UTC](https://discourse.processing.org/t/virtual-dice-dots-not-being-drawn/7508/2 "2019-01-14T18:36:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Harie](https://avatars.discourse-cdn.com/v4/letter/h/8e7dd6/32.png) [@Harie](https://discourse.processing.org/u/Harie)\
**Post date:** [January 15, 2019, 4:22am UTC](https://discourse.processing.org/t/virtual-dice-dots-not-being-drawn/7508/3 "2019-01-15T04:22:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [January 15, 2019, 4:36am UTC](https://discourse.processing.org/t/virtual-dice-dots-not-being-drawn/7508/4 "2019-01-15T04:36:58Z")

</div>

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

```auto
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**
>
> ```auto
> 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](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/f6daff3ec430aaef568d1e6c3bfd94354e0ec3d0.png)

---

<div class="post-metadata">

**Author:** ![Harie](https://avatars.discourse-cdn.com/v4/letter/h/8e7dd6/32.png) [@Harie](https://discourse.processing.org/u/Harie)\
**Post date:** [January 15, 2019, 2:11pm UTC](https://discourse.processing.org/t/virtual-dice-dots-not-being-drawn/7508/5 "2019-01-15T14:11:31Z")

</div>

Thank you very much.
