# Virtual dice roll around many times then stop

**URL:** https://discourse.processing.org/t/virtual-dice-roll-around-many-times-then-stop/7608
**Category:** Coding Questions
**Created:** [January 17, 2019, 11:25pm UTC](https://discourse.processing.org/t/virtual-dice-roll-around-many-times-then-stop/7608 "2019-01-17T23:25:42Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![Captainsparkles](https://avatars.discourse-cdn.com/v4/letter/c/67e7ee/32.png) [@Captainsparkles](https://discourse.processing.org/u/Captainsparkles)
#### Post date: [January 17, 2019, 11:25pm UTC](https://discourse.processing.org/t/virtual-dice-roll-around-many-times-then-stop/7608/1 "2019-01-17T23:25:42Z")

</div>

I can’t figure out the problem as when you press the button it draws 6 circles, but the main point of the program is to make the dice roll around like 7 times slower each time, and then stopping on any side like an actual die.

```auto
float r = 6;
int rr = 6;
int d = 300;
float f = 10;
int a = 1;

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

void draw() {
fill(0, 0, 255);
ellipse(250, 35, 30, 30);
if(f == 20) {
  delay(d);
  d = d + 300;
  a = a + 1;
  r = random(6);
  rr = round(rr);
  println(rr);
  if (rr == 0); {
    side1();
  }
  if (rr == 1); {
    side2();
  }
  if (rr == 2); {
    side3();
  }
  if (rr == 3); {
    side4();
  }
  if (rr == 4); {
    side5();
  }
  if (rr == 5); {
    side6();
  }
  if (a == 6); {
    noLoop();
  //}
 }
}

void side1() {
  fill(255, 255, 0);
  rect(10, 10, 480, 480, 10/5);
  fill(255, 0, 0);
  ellipse(250, 250, 50, 50);
}

void side2() {
  fill(255, 255, 0);
  rect(10, 10, 480, 480, 10/5);
  fill(255, 0, 0);
  ellipse(400, 100, 50, 50);
  ellipse(100, 400, 50, 50);
}

void side3() {
  fill(255, 255, 0);
  rect(10, 10, 480, 480, 10/5);
  fill(255, 0, 0);
  ellipse(100, 100, 50, 50);
  ellipse(250, 250, 50, 50);
  ellipse(400, 400, 50, 10);
}

void side4() {
  fill(255, 255, 0);
  rect(10, 10, 480, 480, 10/5);
  fill(255, 0, 0);
  ellipse(100, 100, 50, 50);
  ellipse(100, 400, 50, 50);
  ellipse(400, 100, 50, 10);
  ellipse(400, 400, 50, 50);
}

  void side5() {
  fill(255, 255, 0);
  rect(10, 10, 480, 480, 10/5);
  fill(255, 0, 0);
  ellipse(100, 100, 50, 50);
  ellipse(100, 400, 50, 50);
  ellipse(400, 100, 50, 10);
  ellipse(400, 400, 50, 50);
  ellipse(200, 250, 50, 50);
}

  void side6() {
  fill(255, 255, 0);
  rect(10, 10, 480, 480, 10/5);
  fill(255, 0, 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);
}

void mousePressed() {
float distance = dist(mouseX, mouseY, 250, 35);
if (distance < 15) {
  f = 20;
 } else{
  f = 10;
 }
}

```

---

<div class="post-metadata">

### Author: ![Abel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/abel/32/3289_2.png) [@Abel](https://discourse.processing.org/u/Abel)
#### Post date: [January 18, 2019, 1:28am UTC](https://discourse.processing.org/t/virtual-dice-roll-around-many-times-then-stop/7608/2 "2019-01-18T01:28:29Z")

</div>

Hey **Captainsparkles!** Hope all is well! 😄

There seem to be a few things that aren’t quite right with the code you provided. I’ll comment on the general concept of the program later when I understand it better but for now here’s a thing or two.

[1] Using **background ()** outside of the draw () function doesn’t allow for frame update. This in your case means, even if your die were to appear to be rolling, it would simply seem that many other dice appearing as the previous paint of the die has not be updated/removed before the new one is drawn.

[2] Using **if () ; {}** like this simply means **if ();** which basically is doing nothing in acting as a condition for the statement in **{}** after checking the condition. I presume you are going for if() {} _[Notice the absence of the semicolon]._

[3] In general it is better to NOT use **delay ()** in your program and especially when you are hoping to let you draw animations. You can use other ways such as [this example](https://t.me/ProcessingCommunity/51).

I presume you are a beginner with Processing and perhaps you may find [this community](https://t.me/ProcessingCommunity) on Telegram Useful as there already are topics answered circling around the issues just mentioned and many others.

**As a question: Please elaborate a bit more on what you want to make as it is a bit vague now.**

Cheers! 😊  
4: 6: 31 WY

---

<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 18, 2019, 2:01am UTC](https://discourse.processing.org/t/virtual-dice-roll-around-many-times-then-stop/7608/3 "2019-01-18T02:01:58Z")

</div>

pls. repair your above post and reformat your code first!  
( asked you already with your first thread )

in processing IDE use  
[ctrl][t]

and copy paste to here into

> \</\>

not “”

even the problems are very different many parts of the code i see already here

> [@Virtual dice dots not being drawn](https://discourse.processing.org/t/virtual-dice-help/7508):
>
> 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…

---

<div class="post-metadata">

### Author: ![Captainsparkles](https://avatars.discourse-cdn.com/v4/letter/c/67e7ee/32.png) [@Captainsparkles](https://discourse.processing.org/u/Captainsparkles)
#### Post date: [January 18, 2019, 2:39am UTC](https://discourse.processing.org/t/virtual-dice-roll-around-many-times-then-stop/7608/4 "2019-01-18T02:39:10Z")

</div>

What I’m trying to do is I’m trying to make a virtual dice in pde that makes the dice roll at least 7 times when you press the button in the beginning with each time being slower, and then eventually stops at one of the faces of the die. My problem is that when you press the button, the only value it gives you is 6 without even it being rolled

---

<div class="post-metadata">

### Author: ![Captainsparkles](https://avatars.discourse-cdn.com/v4/letter/c/67e7ee/32.png) [@Captainsparkles](https://discourse.processing.org/u/Captainsparkles)
#### Post date: [January 18, 2019, 3:28pm UTC](https://discourse.processing.org/t/virtual-dice-roll-around-many-times-then-stop/7608/5 "2019-01-18T15:28:16Z")

</div>

I hope somebody replies, I would really appreciate it  
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 18, 2019, 3:44pm UTC](https://discourse.processing.org/t/virtual-dice-roll-around-many-times-then-stop/7608/6 "2019-01-18T15:44:04Z")

</div>

possibly if you format your code?  
you got more response?

also if you work more on your own understanding  
( like with more diagnostic printing )  
of that variables

> f,a,r,rr

and what you want them to do??

and when you get good tips, like from @Abel

- not use delay inside draw

you should work on that ( or minimal reply to it )

---

<div class="post-metadata">

### Author: ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)
#### Post date: [January 18, 2019, 6:39pm UTC](https://discourse.processing.org/t/virtual-dice-roll-around-many-times-then-stop/7608/7 "2019-01-18T18:39:18Z")

</div>

its never a good idea, if u dont want 2FPS. I figured i could use

```auto
int ProgramTimeNext = 0;
PTN = millis() + <delay im ms>
if(millis() > PTN) {
  do stuff
}

```

---

<div class="post-metadata">

### Author: ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)
#### Post date: [January 18, 2019, 6:56pm UTC](https://discourse.processing.org/t/virtual-dice-roll-around-many-times-then-stop/7608/8 "2019-01-18T18:56:58Z")

</div>

ive created a version of it, a bit diffrent. I hope it helps. 2 things:

- its ugly af
- i know it could be much simpler

```auto
int PTN = 0;
int s = 20;
int delay = 600;

void setup() {
  size(600, 600);
  stroke(255);
  noFill();
}

void draw() {
  //logic
  if (millis() > PTN) {
    //dice(5);
    dice(floor(random(6)));
    PTN = millis() + delay;
    delay -= 100;
    if(delay == 0) {
      noLoop();
    }
  }
}

void dice(int a) {
  //all values are shifted for 1
  println((a+1));
  background(0);
  rect(width/6,height/6,width/6*4,height/6*4);
  //just drawing points
  if (a == 0) {
    ellipse(width/2, height/2, s, s);
  }
  if (a == 1) {
    ellipse(width/4*3, height/4, s, s);
    ellipse(width/4, height/4*3, s, s);
  }
  if (a == 2) {
    ellipse(width/4*3, height/4, s, s);
    ellipse(width/2, height/2, s, s);
    ellipse(width/4, height/4*3, s, s);
  }
  if (a == 3) {
    ellipse(width/4, height/4, s, s);
    ellipse(width/4*3, height/4, s, s);
    ellipse(width/4, height/4*3, s, s);
    ellipse(width/4*3, height/4*3, s, s);
  }
  if(a == 4) {
    ellipse(width/4, height/4, s, s);
    ellipse(width/4*3, height/4, s, s);
    ellipse(width/2, height/2, s, s);
    ellipse(width/4, height/4*3, s, s);
    ellipse(width/4*3, height/4*3, s, s);
  }
  if(a == 5) {
    ellipse(width/4, height/4, s, s);
    ellipse(width/4*3, height/4, s, s);
    ellipse(width/4, height/2, s, s);
    ellipse(width/4*3, height/2, s, s);
    ellipse(width/4, height/4*3, s, s);
    ellipse(width/4*3, height/4*3, s, s);
  }
}

```

---

<div class="post-metadata">

### Author: ![Captainsparkles](https://avatars.discourse-cdn.com/v4/letter/c/67e7ee/32.png) [@Captainsparkles](https://discourse.processing.org/u/Captainsparkles)
#### Post date: [January 18, 2019, 9:31pm UTC](https://discourse.processing.org/t/virtual-dice-roll-around-many-times-then-stop/7608/9 "2019-01-18T21:31:34Z")

</div>

If I were to implement the button into the program that you did, how would I do that?

---

<div class="post-metadata">

### Author: ![Captainsparkles](https://avatars.discourse-cdn.com/v4/letter/c/67e7ee/32.png) [@Captainsparkles](https://discourse.processing.org/u/Captainsparkles)
#### Post date: [January 18, 2019, 10:52pm UTC](https://discourse.processing.org/t/virtual-dice-roll-around-many-times-then-stop/7608/10 "2019-01-18T22:52:48Z")

</div>

Please again if somebody could reply, I’d really appreciate it.  
Thanks

---

<div class="post-metadata">

### Author: ![Captainsparkles](https://avatars.discourse-cdn.com/v4/letter/c/67e7ee/32.png) [@Captainsparkles](https://discourse.processing.org/u/Captainsparkles)
#### Post date: [January 18, 2019, 11:57pm UTC](https://discourse.processing.org/t/virtual-dice-roll-around-many-times-then-stop/7608/11 "2019-01-18T23:57:00Z")

</div>

I meant the program codemasterx made

---

<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 19, 2019, 2:45am UTC](https://discourse.processing.org/t/virtual-dice-roll-around-many-times-then-stop/7608/12 "2019-01-19T02:45:56Z")

</div>

there you not need much of a button, but like  
again mousePressed

- reset delay
- enable loop

```auto
void mousePressed() {
  delay = 600;
  loop();
}

```

---

<div class="post-metadata">

### Author: ![Captainsparkles](https://avatars.discourse-cdn.com/v4/letter/c/67e7ee/32.png) [@Captainsparkles](https://discourse.processing.org/u/Captainsparkles)
#### Post date: [January 19, 2019, 2:51am UTC](https://discourse.processing.org/t/virtual-dice-roll-around-many-times-then-stop/7608/13 "2019-01-19T02:51:39Z")

</div>

I know, but I would like to add one

---

<div class="post-metadata">

### Author: ![Captainsparkles](https://avatars.discourse-cdn.com/v4/letter/c/67e7ee/32.png) [@Captainsparkles](https://discourse.processing.org/u/Captainsparkles)
#### Post date: [January 19, 2019, 2:52am UTC](https://discourse.processing.org/t/virtual-dice-roll-around-many-times-then-stop/7608/14 "2019-01-19T02:52:30Z")

</div>

How exactly would you implement it

---

<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 19, 2019, 3:03am UTC](https://discourse.processing.org/t/virtual-dice-roll-around-many-times-then-stop/7608/15 "2019-01-19T03:03:56Z")

</div>

make a rect  
fill it with color and text  
write a “check if mouse over rect” function  
and make that a condition in above mousePressed function.

more “exactly” only after you format your code!

---

<div class="post-metadata">

### Author: ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)
#### Post date: [January 19, 2019, 10:29am UTC](https://discourse.processing.org/t/virtual-dice-roll-around-many-times-then-stop/7608/16 "2019-01-19T10:29:26Z")

</div>

I added button. I hope that is what u wanted. It took me 5 mins, so no prob.

I added:  
void mousePressed() { } //when mouse is pressed, if its inside button, it throws the dice  
void reset() {} //just starts looping again  
void drawButton() {} //draws button

```auto
int PTN = 0;
int s = 20;
int delay = 600;

void setup() {
  size(600, 600);
  stroke(255);
  noFill();
}

void draw() {
  //logic
  if (millis() > PTN) {
    //dice(5);
    dice(floor(random(6)));
    PTN = millis() + delay;
    delay -= 100;
    if(delay == 0) {
      noLoop();
    }
  }
  drawButton();
}

void dice(int a) {
  //all values are shifted for 1
  println((a+1));
  background(0);
  rect(width/6,height/6,width/6*4,height/6*4);
  //just drawing points
  if (a == 0) {
    ellipse(width/2, height/2, s, s);
  }
  if (a == 1) {
    ellipse(width/4*3, height/4, s, s);
    ellipse(width/4, height/4*3, s, s);
  }
  if (a == 2) {
    ellipse(width/4*3, height/4, s, s);
    ellipse(width/2, height/2, s, s);
    ellipse(width/4, height/4*3, s, s);
  }
  if (a == 3) {
    ellipse(width/4, height/4, s, s);
    ellipse(width/4*3, height/4, s, s);
    ellipse(width/4, height/4*3, s, s);
    ellipse(width/4*3, height/4*3, s, s);
  }
  if(a == 4) {
    ellipse(width/4, height/4, s, s);
    ellipse(width/4*3, height/4, s, s);
    ellipse(width/2, height/2, s, s);
    ellipse(width/4, height/4*3, s, s);
    ellipse(width/4*3, height/4*3, s, s);
  }
  if(a == 5) {
    ellipse(width/4, height/4, s, s);
    ellipse(width/4*3, height/4, s, s);
    ellipse(width/4, height/2, s, s);
    ellipse(width/4*3, height/2, s, s);
    ellipse(width/4, height/4*3, s, s);
    ellipse(width/4*3, height/4*3, s, s);
  }
}

void reset() {
  loop();
  delay = 600;
}

void drawButton() {
  ellipse(width/8,height/8,50,50);
  textAlign(CENTER,CENTER);
  text("Throw",width/8,height/8);
}

void mousePressed() {
  if(dist(width/8,height/8,mouseX,mouseY) < /* buttom size/2 (radius) */ 25) {
    reset();
  }
}

```

---

<div class="post-metadata">

### Author: ![Captainsparkles](https://avatars.discourse-cdn.com/v4/letter/c/67e7ee/32.png) [@Captainsparkles](https://discourse.processing.org/u/Captainsparkles)
#### Post date: [January 19, 2019, 7:06pm UTC](https://discourse.processing.org/t/virtual-dice-roll-around-many-times-then-stop/7608/17 "2019-01-19T19:06:35Z")

</div>

Hi,  
The code with te button works, but my only problem is that when you run the code, the dice automatically rolls without the button being pressed, but every other time, the dice only rolls when the button is pressed.

---

<div class="post-metadata">

### Author: ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)
#### Post date: [January 20, 2019, 4:55pm UTC](https://discourse.processing.org/t/virtual-dice-roll-around-many-times-then-stop/7608/18 "2019-01-20T16:55:54Z")

</div>

Some modifications (like 3 additional lines)

```auto
int s = 20;
int delay = 600;
int PTN = delay;

void setup() {
  size(600, 600);
  stroke(255);
  noFill();
  reset();
  noLoop();
}

void draw() {
  //logic
  if (millis() > PTN) {
    //dice(5);
    dice(floor(random(6)));
    PTN = millis() + delay;
    delay -= 100;
    if(delay == 0) {
      noLoop();
    }
  }
  drawButton();
}

void dice(int a) {
  //all values are shifted for 1
  println((a+1));
  background(0);
  rect(width/6,height/6,width/6*4,height/6*4);
  //just drawing points
  if (a == 0) {
    ellipse(width/2, height/2, s, s);
  }
  if (a == 1) {
    ellipse(width/4*3, height/4, s, s);
    ellipse(width/4, height/4*3, s, s);
  }
  if (a == 2) {
    ellipse(width/4*3, height/4, s, s);
    ellipse(width/2, height/2, s, s);
    ellipse(width/4, height/4*3, s, s);
  }
  if (a == 3) {
    ellipse(width/4, height/4, s, s);
    ellipse(width/4*3, height/4, s, s);
    ellipse(width/4, height/4*3, s, s);
    ellipse(width/4*3, height/4*3, s, s);
  }
  if(a == 4) {
    ellipse(width/4, height/4, s, s);
    ellipse(width/4*3, height/4, s, s);
    ellipse(width/2, height/2, s, s);
    ellipse(width/4, height/4*3, s, s);
    ellipse(width/4*3, height/4*3, s, s);
  }
  if(a == 5) {
    ellipse(width/4, height/4, s, s);
    ellipse(width/4*3, height/4, s, s);
    ellipse(width/4, height/2, s, s);
    ellipse(width/4*3, height/2, s, s);
    ellipse(width/4, height/4*3, s, s);
    ellipse(width/4*3, height/4*3, s, s);
  }
}

void reset() {
  loop();
  delay = 600;
  background(0);
}

void drawButton() {
  ellipse(width/8,height/8,50,50);
  textAlign(CENTER,CENTER);
  text("Throw",width/8,height/8);
}

void mousePressed() {
  if(dist(width/8,height/8,mouseX,mouseY) < /* buttom size/2 (radius) */ 25) {
    reset();
  }
}

```

---

<div class="post-metadata">

### Author: ![Abel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/abel/32/3289_2.png) [@Abel](https://discourse.processing.org/u/Abel)
#### Post date: [January 20, 2019, 5:34pm UTC](https://discourse.processing.org/t/virtual-dice-roll-around-many-times-then-stop/7608/19 "2019-01-20T17:34:08Z")

</div>

Hey! Consider using more general methods in your code. Try this one for **rectangle hovering** for example.

```auto
boolean rectHovered(floatx, float y, float w, float h, float mode){
  boolean status = false;
    if(mode== CORNER && 
        mouseX >= x && mouseX <= x + w && mouseY >= y && mouseY <= y + h || 
mode == CENTER && mouseX >= x - w/2 && mouseX <= x + w/2 && mouseY >= y - h/2 && mouseY <= y + h/2)
      status = true;
  return status;
}

```

And this for a **circular hovering.**

```auto
boolean circleHovered (float x, float y, float rad, int mode) {
  return mode == CORNER && dist(mouseX, mouseY, x + rad/2, y + rad/2) <= rad/2 ||
         mode == CENTER && dist(mouseX, mouseY, x, y) <= rad/2 ? true : false;
}

```
