We are about to switch to a new forum software. Until then we have removed the registration on this forum.
i am working on a lab for class and got far but now i am hitting a major bump in the road. i already spoke with my professor but her time is limited. the objective of the sketch is to make a game that will have 6 click able die that correspond with each side of the die. then I have to use a loop to limit the amount of guesses. the game was working fin till i tried using the loop. i could click each box and it would tell me i won or chose again but would not display you lose because i didn't have a loop to track guesses. once i added the loop it now just shows a blank screen. below is the code with the loop and also i then commented the loop out to show the game working without a game over.
code not working with loop
int guess = 0;
//die
int die = 1;
//guesses
//bolleans for each die
boolean D1 = false;
boolean D2 = false;
boolean D3 = false;
boolean D4 = false;
boolean D5 = false;
boolean D6 = false;
boolean picked = false;
// dice variables
int diceX = 10;
int diceY = 10;
int diceW = 50;
int diceH = 50;
//dot variables
int dotX = 80;
int dotY = 20;
int dotW = 5;
int dotH = 5;
void setup()
{
//screen size
size(370,160);
//rolling die
//die = (int) random(1,7);
println("die landed on " + die);
//loop to make boxes for dice
while(diceX < width)
{
rect(diceX,diceY,diceW,diceH);
diceX = diceX + 60;
}
//dots on the dice
while(dotX < width)
{
fill(0);
ellipse(dotX,dotY,dotW,dotH);
ellipse(dotX + 30,dotY + 30, dotW,dotH);
ellipse(dotX + 150,dotY,dotW,dotH);
ellipse(dotX + 120, dotY + 30, dotW,dotH);
ellipse(35,35,5,5);
ellipse(155,35,5,5);
ellipse(275,35,5,5);
ellipse(320,35,5,5);
ellipse(350,35,5,5);
dotX = dotX + 60;
}
}
void draw()
{
while (guess < 3 && (picked == false))
{
// if statment for when the die is 1
println(D1);
if (D1 && die == 1)
{
textBox("you win");
picked = true;
}
//if statment for when the die is not 1
else if (D1 && die != 1)
{
textBox("sorry chose again");
}
//DEBUG
//if statment for when the die is not 2
else if (D2 && die != 2)
{
textBox("sorry chose again");
}
//if statment for when the die is 2
else if (D2 && die == 2)
{
textBox("you win");
picked = true;
}
//if statment for when the die is not 3
else if (D3 && die != 3)
{
textBox("sorry chose again");
}
// if statment for when the die is 3
else if (D3 && die == 3)
{
textBox("you win");
picked = true;
}
//if statment for when the die is not 4
else if (D4 && die != 4 && guess < 3)
{
textBox("sorry chose again");
}
// if statment for when the die is 4
else if (D4 && die == 4 && guess < 3)
{
textBox("you win");
picked = true;
}
//if statment for when the die is not 5
else if (D5 && die != 5 && guess < 3)
{
textBox("sorry chose again");
}
// if statment for when the die is 5
else if (D5 && die == 5 && guess < 3)
{
textBox("you win");
picked = true;
}
//if statment for when the die is not 6
else if (D6 && die != 6 && guess < 3)
{
textBox("sorry chose again");
}
// if statment for when the die is 6
else if (D6 && die == 6 && guess < 3)
{
textBox("you win");
picked = true;
}
//if statment for game over
else if (guess > 3)
{
textBox("you lose");
}
else
{
textBox("Please select a number");
}
println(guess);
}
noLoop();
}
void textBox(String words)
{
stroke(255);
fill(0);
rect(100,110,160,40);
fill(255);
text(words,110,130);
}
void mousePressed()
{
println("mouseX " +mouseX +" mouseY " + mouseY);
guess = guess+1;
println("inside " + guess);
if (mouseX > 10 && mouseX < 60 && mouseY > 10 && mouseY < 50)
{
D1 = true;
}
if (mouseX > 70 && mouseX < 120 && mouseY > 10 && mouseY < 50)
{
D2 =!D2;
}
if (mouseX > 130 && mouseX < 180 && mouseY > 10 && mouseY < 50)
{
D3 =!D3;
}
if (mouseX > 190 && mouseX < 240 && mouseY > 10 && mouseY < 50)
{
D4 =!D4;
}
if (mouseX > 250 && mouseX < 300 && mouseY > 10 && mouseY < 50)
{
D5 =!D5;
}
if (mouseX > 310 && mouseX < 360 && mouseY > 10 && mouseY < 50)
{
D6 =!D6;
}
}
This is the earlier version of the sketch that didn't have a loop.
void textBox()
{
stroke(255);
fill(0);
rect(100,110,160,40);
fill(255);
}
//die
int die = 0;
//guesses
int guess = 0;
//bolleans for each die
boolean D1 = false;
boolean D2 = false;
boolean D3 = false;
boolean D4 = false;
boolean D5 = false;
boolean D6 = false;
// dice variables
int diceX = 10;
int diceY = 10;
int diceW = 50;
int diceH = 50;
int dotX = 80;
int dotY = 20;
int dotW = 5;
int dotH = 5;
void setup()
{
//screen size
size(370,160);
//rolling die
die = (int) random(1,7);
println("die landed on " + die);
//loop to make boxes for dice
while(diceX < width)
{
rect(diceX,diceY,diceW,diceH);
diceX = diceX + 60;
}
//dots on the dice
while(dotX < width)
{
fill(0);
ellipse(dotX,dotY,dotW,dotH);
ellipse(dotX + 30,dotY + 30, dotW,dotH);
ellipse(dotX + 150,dotY,dotW,dotH);
ellipse(dotX + 120, dotY + 30, dotW,dotH);
ellipse(35,35,5,5);
ellipse(155,35,5,5);
ellipse(275,35,5,5);
ellipse(320,35,5,5);
ellipse(350,35,5,5);
dotX = dotX + 60;
}
}
void draw()
{
//if statment for when the die is not 1
if (D1 && die != 1 && guess < 3)
{
textBox();
text("sorry chose again",110,130);
}
// if statment for when the die is 1
else if (D1 && die == 1 && guess < 3)
{
textBox();
text("you win",110,130);
}
//if statment for when the die is not 2
else if (D2 && die != 2 && guess < 3 )
{
textBox();
text("sorry chose again",110,130);
}
//if statment for when the die is 2
else if (D2 && die == 2 && guess < 3)
{
textBox();
text("you win",110,130);
}
//if statment for when the die is not 3
else if (D3 && die != 3 && guess < 3)
{
textBox();
text("sorry chose again",110,130);
}
// if statment for when the die is 3
else if (D3 && die == 3 && guess < 3)
{
textBox();
text("you win",110,130);
}
//if statment for when the die is not 4
else if (D4 && die != 4 && guess < 3)
{
textBox();
text("sorry chose again",110,130);
}
// if statment for when the die is 4
else if (D4 && die == 4 && guess < 3)
{
textBox();
text("you win",110,130);
}
//if statment for when the die is not 5
else if (D5 && die != 5 && guess < 3)
{
textBox();
text("sorry chose again",110,130);
}
// if statment for when the die is 5
else if (D5 && die == 5 && guess < 3)
{
textBox();
text("you win",110,130);
}
//if statment for when the die is not 6
else if (D6 && die != 6 && guess < 3)
{
textBox();
text("sorry chose again",110,130);
}
// if statment for when the die is 6
else if (D6 && die == 6 && guess < 3)
{
textBox();
text("you win",110,130);
}
//if statment for game over
else if (guess > 3)
{
textBox();
text("you lose",110,130);
}
else
{
textBox();
text("Please select a number",110,130);
}
}
void mousePressed()
{
if (mouseX > 10 && mouseX < 60 && mouseY > 10 && mouseY < 50)
{
D1 = !D1;
}
if (mouseX > 70 && mouseX < 120 && mouseY > 10 && mouseY < 50)
{
D2 =!D2;
}
if (mouseX > 130 && mouseX < 180 && mouseY > 10 && mouseY < 50)
{
D3 =!D3;
}
if (mouseX > 190 && mouseX < 240 && mouseY > 10 && mouseY < 50)
{
D4 =!D4;
}
if (mouseX > 250 && mouseX < 300 && mouseY > 10 && mouseY < 50)
{
D5 =!D5;
}
if (mouseX > 310 && mouseX < 360 && mouseY > 10 && mouseY < 50)
{
D6 =!D6;
}
}
i know this is a lot to look at but im at the end of my options at the moment.
Answers
It'd help a lot if you re-post it, highlight it and hit CTRL+K to format it! 8-X
to Dareon thank you for your help. i cant hand this in as my own work now but it alt east gives me an example of how to rework it. also thank you for showing how to condense the if statements. to GoToLoop thanks for the tip. i knew it looked wrong when i posted it but i dint know what to do.
Your method of keeping track of the guesses is the better in my opinion but I am still tinkering around with how to keep track of it with a loop. Loops seam to not like this sketch. Even with cleaning things up and changing a bit around it wont run if the loop is in the draw. You helped me to get it to work so i can get credit for the attempting the work at least.