We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › probably easy to answer mousePressed-question
Page Index Toggle Pages: 1
probably easy to answer mousePressed-question (Read 1089 times)
probably easy to answer mousePressed-question
Mar 16th, 2010, 7:45pm
 
Hi, I'm making a game that is kinda similar to pool - I have the player rotate the cuestick around a ball, and they click the mouse to make the cuestick stay in place of the direction they'd like it to be. In order to go to the next level, the player presses a button("go to the next level!") and go through the whole rotating cuestick process. I'm having trouble rotating the cuestick again in the next level... I think because you have to click the button with the mouse to go to the next level which also freezes the cuestick for the next level. So the cuestick in the next level is just frozen. If someone could tell me how to rotate it again, that would be greatly appreciated! Here is my code:

//x-axis of ball = float ballx
//y-axis of ball = float bally
//radius of ball = balld
//placement of pocket = float pocketx, float pockety
//radius of pocket = float pocketr

int mode = 0;

float ballx = 320;
float bally = 380;
float cuex = ballx;
float cuey = bally;
float balld = 15;
float ballangle = 0;
float ballspeedx = 8.0;
float ballspeedy = 8.0;
float angle;
boolean rotation = true;

float pocketx = 550;
float pockety = 100;
float pocketr = 50;

int b1x = 210;   // Button 1 x-coordinate
int b1y = 215;  // Button 1 y-coordinate
int b1w = 220;  // Button 1 width
int b1h = 50;   // Button 1 height

PFont f;
PFont f48;

void setup() {
 size(640, 480);
 smooth();
 ellipseMode(RADIUS);
 f = loadFont("ZebrawoodStd-Fill-28.vlw");
 f48 = loadFont("ZebrawoodStd-Fill-48.vlw");
 textFont(f);
 textAlign(CENTER);
}

void draw() {

 background(204);
 strokeCap(ROUND);
 strokeWeight(2);
 fill(255);
 
 if (mode == 0) {
   modeZero();
 }
 else if (mode == 1) {
   modeOne();
 }
 else if (mode == 2) {
   modeTwo();
 }
 else if (mode == 3) {
   modeThree();
 }
 else if (mode == 4) {
   modeFour();
 }
 else if (mode == 5) {
   modeFive();
 }
}

void mousePressed() {
 if (mode == 1) {
   rotation = false;
 }
 if (mode == 3) {
   if (overButton(b1x, b1y, b1w, b1h) == true) {
   mode = 4;
   }
 }
}

void keyPressed() {
   if (mode == 0) {
   mode = 1;
 }
 if (mode == 1 && rotation == false) {
   mode = 2;
   ballangle = angle - PI;
 }
}

boolean overButton(int x, int y, int w, int h) {
 if (mouseX > x && mouseX < x+w && mouseY > y && mouseY < y+h) {
   return true;
 }
 else {
   return false;
 }
}

void modeZero() {
 textFont(f48);
 fill(0);
 text("Poolio", 320, 70);
 textFont(f);
 text("1. move mouse left and right", 320, 140);
 text("to rotate cuestick", 320, 170);
 text("2. click mouse to freeze aim", 320, 230);
 text("3. press the space bar to shoot", 320, 290);
 fill(#A71111);
 text("press any key to start playing!", 320, 350);
}

void modeOne() {
 pushMatrix();
 translate(cuex, cuey);
 rotate(angle);
 line(balld/2 + 13, 0, balld/2 + 200, 0);//cuestick
 popMatrix();
 
 fill(0);
 ellipse(pocketx, pockety, pocketr, pocketr); //pocket
 
 fill(255);
 ellipse(ballx, bally, balld, balld); //cueball
 if (rotation) {
   angle = map(mouseX, 20, 620, 0, PI*3);
 }
 
 fill(0);
 text("Level 1", 70, 465);

}

void modeTwo() {
 
 fill(0);
 ellipse(pocketx, pockety, pocketr, pocketr); //pocket
 // Draw the ball
 pushMatrix();
 fill(255);
 translate(ballx, bally);
 rotate(ballangle);
 ellipse(0, 0, balld, balld);
 popMatrix();
 // Update position and bounce off walls
 ballx += cos(ballangle) * ballspeedx;
 bally += sin(ballangle) * ballspeedy;
 checkWalls();
 
 
 if (circleIntersect(ballx, bally, balld, pocketx, pockety, pocketr) == true) {
  mode = 3;
 }
 
 // Slow down
 ballspeedx *= 0.99;
 ballspeedy *= 0.99;
 
 fill(0);
 text("Level 1", 70, 465);
}

void checkWalls() {
 if (ballx > width-balld) {
   ballx = width-balld;
   ballspeedx *= -1;
 }
 else if (ballx < balld) {
   ballx = balld;
   ballspeedx *= -1;
 }
 else if (bally > height-balld) {
   bally = height-balld;
   ballspeedy *= -1;
 }
 else if (bally < balld) {
   bally = balld;
   ballspeedy *= -1;
 }
}



void modeThree() {
 fill(0);
 ellipse(pocketx, pockety, pocketr, pocketr); //pocket
  // Draw the ball
 pushMatrix();
 translate(ballx, bally);
 rotate(ballangle);
 stroke(0, 0);
 fill(0, 0);
 ellipse(0, 0, balld, balld);
 popMatrix();
 
 if (overButton(b1x, b1y, b1w, b1h) == true) {
   stroke(0);
   fill(255);
 }
 else {
   stroke(0);
   fill(153);
 }
 rect(b1x, b1y, b1w, b1h);
 textAlign(CENTER);
 fill(0);
 text("Go to level 2", b1x + b1w/2, b1y + 32);
 
 fill(0);
 text("Level 1", 70, 465);
 stroke(0, 0);
}


boolean circleIntersect(float bx, float by, float br, float px, float py, float pr) {
 if (dist(bx, by, px, py) < br-10 + pr-10) {
   return true;
 } else {
   return false;
 }
}

void modeFour() {
 
 stroke(0);
 fill(0);
 ellipse(pocketx, pockety, pocketr, pocketr); //pocket
  // Draw the ball
 
 pushMatrix();
 translate(cuex, cuey);
 rotate(angle);
 line(balld/2 + 13, 0, balld/2 + 200, 0);//cuestick
 popMatrix();
 
 fill(255);
 ellipse(ballx, bally, balld, balld); //cueball
 if (rotation) {
   angle = map(mouseX, 20, 620, 0, PI*3);
 }
}

void modeFive() {
 stroke(0);
 fill(0);
 ellipse(pocketx, pockety, pocketr, pocketr); //pocket
  // Draw the ball
 
 pushMatrix();
 translate(cuex, cuey);
 rotate(angle);
 line(balld/2 + 13, 0, balld/2 + 200, 0);//cuestick
 popMatrix();
 
 fill(255);
 ellipse(ballx, bally, balld, balld); //cueball
 if (rotation) {
   angle = map(mouseX, 20, 620, 0, PI*3);
 }
}
Re: probably easy to answer mousePressed-question
Reply #1 - Mar 17th, 2010, 12:05am
 
When mousePressed and mode 1, you set rotation to false.
You should set it back to true if clicked outside of the button, and when entering a new stage (mode?).
Re: probably easy to answer mousePressed-question
Reply #2 - Mar 17th, 2010, 12:33am
 
Okay cool, I got it to rotate in mode 3, but I'd also like the player to stop the rotation in mode 4 (because they need to choose the aim) by clicking the mouse again. So this is what I did, but the rotation stops in mode 3 again:

void mousePressed() {
 if (mode == 1) {
   rotation = false;
 }
 if (mode == 3) {
   if (overButton(b1x, b1y, b1w, b1h) == true) {
   mode = 4;
   }
   if (mousePressed) {
     rotation = true;
   }
 }
 if (mode == 4) {
   rotation = false;
 }
}
Re: probably easy to answer mousePressed-question
Reply #3 - Mar 17th, 2010, 4:10am
 
You are in mousePressed() so mousePressed variable is always true!
And use else if like you do elsewhere (but there, you should use switch instead...).
Otherwise, you set rotation to true and fall into the next test which resets it to false...
Re: probably easy to answer mousePressed-question
Reply #4 - Mar 17th, 2010, 5:38am
 
I don't really understand how to use the switch function... I looked it up in references, but I don't know how to apply it to my code
Re: probably easy to answer mousePressed-question
Reply #5 - Mar 17th, 2010, 6:37am
 
Code:
void draw() {

background(204);
strokeCap(ROUND);
strokeWeight(2);
fill(255);

switch (mode) {
case 0: modeZero(); break;
case 1: modeOne(); break;
case 2: modeTwo(); break;
case 3: modeThree(); break;
case 4: modeFour(); break;
case 5: modeFive(); break;
default: println("Something went wrong!");
}
}

void mousePressed() {
switch (mode) {
case 1: rotation = false; break;
case 3:
if (overButton(b1x, b1y, b1w, b1h)) {
mode = 4;
rotation = true;
}
break;
case 4: rotation = false; break;
}
}
(untested)
Re: probably easy to answer mousePressed-question
Reply #6 - Mar 17th, 2010, 10:00am
 
wow that was so helpful, thank you. That worked out great.

so now I'm working on the second level, and I can't seem to make the ball move. It is supposed to move when you press a key on the keyboard. Here is the section of the code where I think the problem is:

void keyPressed() {
  if (mode == 0) {
  mode = 1;
}
if (mode == 1 && rotation == false) {
  mode = 2;
  ballangle = angle - PI;
}
if (mode == 4 && rotation == false) {
  mode = 5;
  ballangle = angle - PI;
}
}

void modeZero() {
textFont(f48);
fill(0);
text("Poolio", 320, 70);
textFont(f);
text("1. move mouse left and right", 320, 140);
text("to rotate cuestick", 320, 170);
text("2. click mouse to choose aim", 320, 230);
text("3. press the space bar to shoot", 320, 290);
fill(#A71111);
text("press any key to start playing!", 320, 350);
}

void modeOne() {
pushMatrix();
translate(cuex, cuey);
rotate(angle);
line(balld/2 + 13, 0, balld/2 + 200, 0);//cuestick
popMatrix();

fill(0);
ellipse(pocketx, pockety, pocketr, pocketr); //pocket

fill(255);
ellipse(ballx, bally, balld, balld); //cueball
if (rotation) {
  angle = map(mouseX, 20, 620, 0, PI*3);
}

fill(0);
text("Level 1", 70, 465);


}

void modeTwo() {
fill(0);
ellipse(pocketx, pockety, pocketr, pocketr); //pocket
// Draw the ball
pushMatrix();
fill(255);
translate(ballx, bally);
rotate(ballangle);
ellipse(0, 0, balld, balld); //ball
popMatrix();
// Update position and bounce off walls
ballx += cos(ballangle) * ballspeedx;
bally += sin(ballangle) * ballspeedy;
checkWalls();

if (circleIntersect(ballx, bally, balld, pocketx, pockety, pocketr) == true) {
 mode = 3;
}

ballspeedx *= 0.99;
ballspeedy *= 0.99;

fill(0);
text("Level 1", 70, 465);
}

void obstacle() {
ballspeedx *= 0.99;
ballspeedy *= 0.99;

if (ballx > rectx-balld) {
 
if (ballx > rectx-balld) {
  directionX = -directionX;
}

if (bally > recty-balld) {
  directionY = -directionY;
}
}
}
 
void checkWalls() {
if (ballx > width-balld) {
  ballx = width-balld;
  ballspeedx *= -1;
} else if (ballx < balld) {
  ballx = balld;
  ballspeedx *= -1;
} else if (bally > height-balld) {
  bally = height-balld;
  ballspeedy *= -1;
} else if (bally < balld) {
  bally = balld;
  ballspeedy *= -1;
}
}

void modeThree() {
fill(0);
ellipse(pocketx, pockety, pocketr, pocketr);

pushMatrix();
translate(ballx, bally);
rotate(ballangle);
stroke(0, 0);
fill(0, 0);
ellipse(0, 0, balld, balld);
popMatrix();

if (overButton(b1x, b1y, b1w, b1h) == true) {
  stroke(0);
  fill(255);
}
else {
  stroke(0);
  fill(153);
}
rect(b1x, b1y, b1w, b1h);
textAlign(CENTER);
fill(0);
text("Go to level 2", b1x + b1w/2, b1y + 32);

fill(0);
text("Level 1", 70, 465);
stroke(0, 0);
}

void modeFour() {
float ballx = 320;
float bally = 380;
 
stroke(0);
fill(0);
ellipse(pocketx-460, pockety, pocketr-10, pocketr-10);
obstacle();

rect(rectx, recty, rectWidth, rectHeight);

pushMatrix();
translate(cuex, cuey);
rotate(angle);
line(balld/2 + 13, 0, balld/2 + 200, 0);
popMatrix();

fill(255);
ellipse(ballx, bally, balld, balld);
if (rotation) {
  angle = map(mouseX, 50, 620, 0, PI*3);
}
fill(0);
text("Level 2", 70, 465);
}

void modeFive() {
fill(0);
float pocketx = 90;
float ballx = 320;
float bally = 380;
ellipse(pocketx, pockety, pocketr-10, pocketr-10); //pocket
// Draw the ball
pushMatrix();
fill(255);
translate(ballx, bally);
rotate(ballangle);
ellipse(0, 0, balld, balld); //ball
popMatrix();
// Update position and bounce off walls
ballx += cos(ballangle) * ballspeedx;
bally += sin(ballangle) * ballspeedy;
checkWalls();
obstacle();

rect(rectx, recty, rectWidth, rectHeight);

if (circleIntersect(ballx, bally, balld, pocketx, pockety, pocketr) == true) {
 mode = 3;
}

ballspeedx *= 0.99;
ballspeedy *= 0.99;

fill(0);
text("Level 2", 70, 465);
}
Re: probably easy to answer mousePressed-question
Reply #7 - Mar 17th, 2010, 10:55am
 
somebody, anybody... wanna help me out here?
Page Index Toggle Pages: 1