About Procesing Programming

Asked 2 years ago, Updated 2 years ago, 81 views

Let me ask you a question

I am currently making a program to roll a simple dice in the processing.
The goal is to have a program that randomly displays the appearance until it is clicked, and then stops displaying the appearance when clicked

floated=random(6);

void setup() {
  size (250,250);
  rect(50,50,150,150);
  fill(255,0,0);
  else(125, 125, 30, 30);
}

void draw() {
  if(d==0){
  fill(255,0,0);
  else(125, 125, 30, 30);
  } else if(d==1){
  fill(0,0,0);
  else (85, 85, 30, 30);
  fill(0,0,0);
  else (165, 165, 30, 30);
  } else if(d==2){
  fill(0,0,0);
  else(125, 125, 30, 30);
  fill(0,0,0);
  else (85, 85, 30, 30);
  fill(0,0,0);
  else (165, 165, 30, 30);
  } else if(d==3){
  fill(0,0,0);
  else (85, 85, 30, 30);
  fill(0,0,0);
  else (165, 85, 30, 30);
  fill(0,0,0);
  else (85, 165, 30, 30);
  fill(0,0,0);
  else (165, 165, 30, 30);
  } else if(d==4){
  fill(0,0,0);
  else(125, 125, 30, 30);
  fill(0,0,0);
  else (85, 85, 30, 30);
  fill(0,0,0);
  else (165, 85, 30, 30);
  fill(0,0,0);
  else (85, 165, 30, 30);
  fill(0,0,0);
  else (165, 165, 30, 30);
  } else if(d==5){
  fill(0,0,0);
  else (85, 85, 30, 30);
  fill(0,0,0);
  else (165, 85, 30, 30);
  fill(0,0,0);
  else (85, 165, 30, 30);
  fill(0,0,0);
  else (165, 165, 30, 30);
  fill(0,0,0);
  else (165, 125, 30, 30);
  fill(0,0,0);
  else (85, 125, 30, 30);
    } 
  }

I've tried a lot of things from here, but it didn't work.
I would appreciate your reply and advice.

processing

2022-09-29 20:28

2 Answers

random() The return value of the function is float, or decimal, which seems to be the cause of the bug.Even though the return value is decimal, comparing == to an integer value does not work as expected.In this case, it is recommended to round it to an integer value such as int(random(6) (document also says so).

The following is a high-level example of the overall combination.Please refer to it if it gets stuck.

1. Create one variable to express the state of "dice is spinning" and "dice is stopped."
2. When the mouse clicks, check and switch this state variable.You can use mouthClicked().
3. In draw(), check the state variable, and if the dice are spinning, switch the dice value d.If necessary, you can use a timer to slow down the speed at which the dice are switched.
4. You will need to reset the screen using the background() function before drawing.


2022-09-29 20:28

The random function outputs random numbers in the range of float type (decimal), so round (round down) to an integer with the int function.Also, I think you can do +1 for d for clarity.

// Current State
float d=random(6); // This returns from the range of 0.0 to 5.999..

// improvement plan
int d = int(random(6))) +1; // This will be returned from the range 1 to 6
int d = int(random(1,7)); // This is the same result

To think about this, you need to think about the state of the program (=state).
In this case, there are two possible states:

When one state changes to another, it is called "state transition."
In this case, only one of the following can be considered:

  • Click to transition from "random appearance (state 1)" to "stopped appearance (state 2)"

If a state transition occurs, a dedicated variable is provided because the current state must be clarified.This variable is called a "state variable."
In the case of Processing, you should be able to achieve your goal by setting state variables such as the d variable, and by state transition within the draw method and within the mousePressed method.
The advice is to re-draw the dice every time within the draw method

.

Below is an example of the code, but ideally you can make it on your own.
If you really don't know, please refer to it.

 int state = 0; // 0 = state 1, 1 = state 2
int d = int(random(6)))+1;

void setup() {
    size (250,250);
    frameRate(10); // Number of times draw is called per second
}

void draw() {
    // change the appearance of the dice randomly in Condition 1
    if(state==0){...}

    // reset the screen every time
    background();

    // redraw the background of the dice
    fill(255); rect(50,50,150,150);
    fill(255,0,0);ellipse..

    // the appearance of a dice
    if(d==1) {...} else if...
}

void mousePressed() {
    // Clicked → State transition here!
}


2022-09-29 20:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.