|
Author |
Topic: keys do different things (Read 807 times) |
|
pokemon#1
|
keys do different things
« on: Jun 15th, 2003, 1:37am » |
|
Me and my friend have been working on an applet in which every key does something different. We haven't finished yet but here is the code so far. //Key Applet //by Taylor Sutton //and David //Copyright 2003 Taylor Sutton and David float ran = random(0,255); float rand = random(255); float rando = random(255); float xMouse; void mousePressed(){ if(mousePressed){ xMouse=random(255,width-mouseX); } } void setup(){ size(500,500); background(0xff6699FF); fill(0,255,0); stroke(255,0,0); } void refresh(){ background(255); noStroke(); noFill(); rect(-1,-1,width+1,height+1); } void loop(){ if(key=={ //Sorry about the face. //It actualy is 8 followed by ). refresh(); } if(key=='a'||key=='A'){ background(0xff6699FF); stroke(0); rect(mouseX-10,mouseY-25,20,20); } else if (key=='b'||key=='B'){ background(0xff6699FF); fill(0,255,0); ellipse(mouseX-10,mouseY+25,20,20); } else if (key=='c'||key=='C'){ background(0xff6699FF); stroke(0); triangle(mouseX+25,mouseY,mouseX+15,mouseY+20,mouseX+35,mouseY+20); } else if (key=='d'||key=='D'){ background(0xff6699FF); stroke(0); beginShape(POLYGON); vertex(mouseX-25,mouseY+10); vertex(mouseX-15,mouseY); vertex(mouseX-5,mouseY+10); vertex(mouseX-15,mouseY+20); endShape(); } if(key=='e'||key=='E'){ background(0xff6699FF); rect(mouseX-25,mouseY-25,60,60);} if(key=='f'||key=='F'){ background(0xff6699FF); translate(mouseX,mouseY); x=x-0.06; fill(100,100,255); rotate(x); stroke(0); noLights(); rect(0,0,30,30); } else if(mousePressed){ background(mouseX,pmouseY,150); } if(key=='g'||key=='G'){ background(0xff6699FF); translate(mouseX+2,mouseY+2); x-=0.08; fill(100,100,255); rotate(x); stroke(0); noLights(); ellipse(0,0,30,30); } if(key=='h'||key=='H'){ background(0xff6699FF); translate(mouseX,mouseY); x-=0.06; y=+0.06; stroke(0); fill(134); rotateY(x); rotateZ(x); rotateX(y); noLights(); box(30); } if(key=='i'||key=='I'){ background(0xff6699FF); translate(mouseX,mouseY); x+=0.1; fill(152,152,12); rotate(x); lights(); noStroke(); smooth(); ellipse(0,0,50,50); rect(0,0,30,30); } if(key=='j'||key=='J'){ noBackground(); stroke(mouseX,mouseY,100); translate(mouseX,mouseY); x+=0.1; fill(xMouse,rand,rando); rand--; rando++; if(rand==0){ rand=random(255); } if(rando==255){ rando=random(255); } rotate(x); lights(); noStroke(); smooth(); ellipse(0,0,50,50); rect(0,0,30,30); } if(key=='k'){ noBackground(); stroke(0); line(pmouseX,pmouseY,mouseX,mouseY); } if(key==32){ noBackground(); } } float z = 0; float a = 0; float y = 0; float x = 0;
|
« Last Edit: Jun 17th, 2003, 6:15pm by pokemon#1 » |
|
|
|
|
benelek
|
Re: keys do different things
« Reply #1 on: Jun 17th, 2003, 2:35am » |
|
you and your friend might be interesting in http://acg.media.mit.edu/projects/dakadaka/dakadaka.html by GroupC. dakadaka mystified me when i first had a look at it btw, you shouldn't need to reset the background before every loop - Processing does that for you if a background() is used in setup(). also, though it makes little difference in reality, using switch() instead of so many if() clauses makes for neater code. when do you plan on posting the finished sketch? -jacob
|
|
|
|
edwardgeorge
|
Re: keys do different things
« Reply #2 on: Jun 17th, 2003, 3:08pm » |
|
on Jun 17th, 2003, 2:35am, benelek wrote: DakaDaka appears to be one of those applets that never seems to work with any of my computers with sdk installed (which is all of them!). hmmm. grrrrrr.
|
Ed, Suppose, Nottingham, UK http://www.suppose.co.uk http://ed.suppose.co.uk
|
|
|
pokemon#1
|
Re: keys do different things
« Reply #3 on: Jun 17th, 2003, 3:55pm » |
|
I'm not sure when I'll post the final sketch. I'm planning on putting it on the web soon. What's the difference between if() and switch()?
|
|
|
|
proce55ing23
|
Re: keys do different things
« Reply #4 on: Jun 17th, 2003, 6:02pm » |
|
I am pokemon#1's friend, the one wo he did it with. In case anybody is wondering more of our applets are on coolthingsmaster.tk and they will be added to coolwebdesigner.tk. Also, in case anybody is wondering, our ages are both 10.
|
|
|
|
benelek
|
Re: keys do different things
« Reply #5 on: Jun 18th, 2003, 1:38am » |
|
it's always great to know that there are 10 year olds playing around with this stuff! re: if/switch... if() will check for a certain condition each time it is called. so if you have one if() for each letter of the alphabet, that is how many times a seperate question will be asked. it's not very efficient, because only one of them is going to be right at any one time. a better way to do it would be to use if/else structure, as follows: Code: if(theCondition=="a") { stuff... } else if(theCondition=="b") { otherStuff... } else if(theCondition=="c") { moreStuff... } |
| basically, it means that the "else" question is only asked if the previous question is not answered as true. this way, if the letter pressed is "a", Processing will not also ask whether b,c,d,... are also pressed. switch works in a different way, in that it points to a variable. then you can provide things for Processing to do in any number of cases. the following example was modified from Sun's java tutorial: Code: int theCondition = 5; switch (theCondition) { case 1: println("a"); break; case 2: println("b"); break; case 3: println("c"); break; case 4: println("d"); break; case 5: println("e"); break; } |
| using Switch() structure also means you don't have to type out if() and else each time Note that you have to put a break; at the end of each case. Sun's Java tutorial has a good page on it here: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/switch.html if you need more explanation, just ask. -jacob
|
|
|
|
|