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 › code help: shape/colour change on keypress
Page Index Toggle Pages: 1
code help: shape/colour change on keypress (Read 2781 times)
code help: shape/colour change on keypress
Mar 29th, 2010, 11:51pm
 
Just wondering whether someone could help me with some code.  I'm trying to create a drawing program where by pressing the up and down arrows on a keyboard, a user can cycle through various shapes such as a sphere, triangle, square, etc.  Also,  by pressing the left and right arrows they can also change the colour of that shape.

Any help in terms of the code I would need to make this happen would be very much appreciated.
Re: code help: shape/colour change on keypress
Reply #1 - Mar 30th, 2010, 12:15am
 
Quote:
int theShape = 0;
int theColor = 0;
color[] colors = { color(255,0,0), color(255,255,0), color(0,255,0), color(0,0,255) };
void setup(){
  size(200,200);
  noStroke();
}

void draw(){
  background(0);
  fill( colors[theColor] );
  switch( theShape ){
    case 0:
    default:
      ellipse(100,100,160,160);
      break;
    case 1:
      rect(20,20,160,160);
      break;
    case 2:
      beginShape(TRIANGLES);
      vertex(100, 20);
      vertex(20, 180);
      vertex(180, 180);
      endShape();
      break;
    case 3:
      rect(90,20,20,160);
      rect(20,90,160,20);
      break;
  }
}  
  
  void keyPressed(){
    if( key==CODED ){
     if( keyCode == UP ){ theShape=(theShape+1)%4; }
     if( keyCode == DOWN ){ theShape=(theShape+3)%4; }
     if( keyCode == LEFT ){ theColor=(theColor+1)%4; }
     if( keyCode == RIGHT ){ theColor=(theColor+3)%4; }
    }
  }

Re: code help: shape/colour change on keypress
Reply #2 - Mar 30th, 2010, 1:38am
 
Grin

TfGuy44 - You still doing people's homework for them then?
Did you look at the OP's user-name?  My guess: this was posted by a teacher wanting to check out how easy it is to cheat on this site when it's blatantly obvious that the request is for someone's homework...

Lips Sealed
Re: code help: shape/colour change on keypress
Reply #3 - Mar 30th, 2010, 2:05am
 
hehe Smiley
Re: code help: shape/colour change on keypress
Reply #4 - Mar 30th, 2010, 2:55am
 
Yep, I sure am! And yes, I did happen to notice the username.

Frankly, I don't care. If they cheat they learn nothing and lose, or get caught and fail and lose. If they don't cheat, they learn something and win, and can't fail either, which is also a win.

In short,
CHEAT --> LOSE
DON'T CHEAT --> WIN
Re: code help: shape/colour change on keypress
Reply #5 - Mar 31st, 2010, 8:30pm
 
Thanks for your help in getting me started.  In actual fact it was my teacher that suggested these forums in order for me to get help with my code, and my name is actually Aaron Cheater which explains where my username comes from.

I'd like to share what I've done so far.  I'm still having trouble getting the shapes to follow the mouse; the circle, square and cross are fine, but the triangle goes all weird as you'll see.  Is it just a matter of fiddling around with the mouseX and mouseY functions to get it to work properly, or is there something else that i'm missing?

Here is my code so far:

Quote:
int theShape = 0;
int theColor = 0;
color[] colors = {
  color(255,0,0,100), color(255,255,0,100), color(0,255,0,100), color(0,0,255,100) };

void setup (){
  size (400,600);
  background (255);
  smooth ();
  stroke(0,0,0,5);
  strokeWeight(5);


void draw (){
  if (mousePressed) {
  fill( colors[theColor] );
  switch( theShape ){
  case 0:
  default:
    ellipse(mouseX,mouseY,60,60);
    break;
  case 1:
    rect(mouseX-30, mouseY-30,60,60);
    break;
  case 2:
    beginShape(TRIANGLES);
    vertex(mouseX+50, mouseY+50);
    vertex(pmouseX-20, pmouseX-20);
    vertex(mouseX-20, mouseY-20);
    endShape();
    break;
  case 3:
    rect(mouseX, mouseY-20,20,60);
    rect(mouseX-20, mouseY,60,20);
    break;
  } 

}

void keyPressed () {
  if( key==CODED ){
    if( keyCode == UP ){
      theShape=(theShape+1)%4; 
    }
    if( keyCode == DOWN ){
      theShape=(theShape+3)%4; 
    }
    if( keyCode == LEFT ){
      theColor=(theColor+1)%4; 
    }
    if( keyCode == RIGHT ){
      theColor=(theColor+3)%4; 
    }
  }
    if( key == DELETE ){
      background(255);
    }
}



Any help would be much appreciated!
Re: code help: shape/colour change on keypress
Reply #6 - Mar 31st, 2010, 11:18pm
 
yes, that was actually the problem.
in this case its easier anyway to use a function like translate to move the triangle. So just draw one, yours was not correct anyway and then translate it to the mouse position.

just change this :

 case 2:
pushMatrix();
translate(mouseX,mouseY);
   beginShape(TRIANGLES);
   vertex(0,-20);
   vertex(30, 30);
   vertex(-30, 30);
   endShape();
   popMatrix();
   break;
Re: code help: shape/colour change on keypress
Reply #7 - Apr 1st, 2010, 5:58am
 
SeriouslyPunked wrote on Mar 31st, 2010, 8:30pm:
Thanks for your help in getting me started.  In actual fact it was my teacher that suggested these forums in order for me to get help with my code, and my name is actually Aaron Cheater which explains where my username comes from.

I'd like to share what I've done so far.  I'm still having trouble getting the shapes to follow the mouse; the circle, square and cross are fine, but the triangle goes all weird as you'll see.  Is it just a matter of fiddling around with the mouseX and mouseY functions to get it to work properly, or is there something else that i'm missing


;D
Sorry, that obviously just proved to be an amusing coincidence.  TfGuy has a tendency to just post code rather than guiding people towards a solution; which I personally feel panders to the cheats...

Anyway - the problem with the triangle is the offsets you're using and the fact you use pmouseX/Y for one point (this gives you the mouse position on the previous frame).

Using translate is an alternative solution but there's nothing wrong with the approach you're using.  In this situation it's useful to use graph paper: draw a point - this represents mouseX,mouseY.  Now draw your shape around the point.  For each vertex of the shape you can count the offset from the point representing the mouse position.  So a triangle might be:

Code:
beginShape(TRIANGLES);
 vertex(mouseX, mouseY+20); // point centred above the mouse
 vertex(mouseX+20, mouseY-20);  // down and right from the mouse
 vertex(mouseX-20, mouseY-20); // down and left from the mouse
endShape();


Re: code help: shape/colour change on keypress
Reply #8 - Apr 6th, 2010, 1:08am
 
Thanks guys for all your help, and just for all your interest heres my final code:

int theShape = 0;
int theColor = 0;
color[] colors = {
 color(255,0,0,100), color(255,255,0,100), color(0,255,0,100), color(0,0,255,100) };
PImage b;

void setup (){
 b = loadImage("icon.gif");
 size (400,600);
 background (255);
 smooth ();
 stroke(0,0,0,5);
 strokeWeight(0);
 cursor(b, mouseX, mouseY);
}

void draw (){
 if (mousePressed) {
 fill( colors[theColor] );
 switch( theShape ){
 case 0:
 default:
   ellipse(mouseX,mouseY,60,60);
   break;
 case 1:
   rect(mouseX-30, mouseY-30,60,60);
   break;
case 2:
pushMatrix();
translate(mouseX,mouseY);
  beginShape(TRIANGLES);
  vertex(0,-20);
  vertex(30, 30);
  vertex(-30, 30);
  endShape();
  popMatrix();
  break;
 case 3:
   rect(mouseX, mouseY-20,20,60);
   rect(mouseX-20, mouseY,60,20);
   break;
 }
}
}

void keyPressed () {
 if( key==CODED ){
   if( keyCode == UP ){
     theShape=(theShape+1)%4;
   }
   if( keyCode == DOWN ){
     theShape=(theShape+3)%4;
   }
   if( keyCode == LEFT ){
     theColor=(theColor+1)%4;
   }
   if( keyCode == RIGHT ){
     theColor=(theColor+3)%4;
   }
 }
   if( key == DELETE ){
     background(255);
   }
}

Obviously the icon image won't work but I might upload it to Open Processing eventually if you're that keen on checking it out.

Thanks again.
Page Index Toggle Pages: 1