Virtual dice roll around many times then stop

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.

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;
 }
}
1 Like

Hey Captainsparkles! Hope all is well! :smile:

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.

I presume you are a beginner with Processing and perhaps you may find this community 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! :blush:
4: 6: 31 WY

1 Like

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

1 Like

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

I hope somebody replies, I would really appreciate it
Thanks

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 )

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

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

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

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

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

I meant the program codemasterx made

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

  • reset delay
  • enable loop
void mousePressed() {
  delay = 600;
  loop();
}

I know, but I would like to add one

How exactly would you implement it

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!

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

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

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.

Some modifications (like 3 additional lines)

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

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

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.

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;
}
2 Likes