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.
Page Index Toggle Pages: 1
color game (Read 651 times)
color game
Mar 16th, 2010, 11:22pm
 
so, i'm trying to fix a game where the goal is to click a colored circle that is bouncing around the screen. there is a text prompt at the top saying what color ball to click (the circle's fill is constantly changing). And once you click the right color the background changes to that color, and the circle's fill continues to change. But right now when I click the circle everything changes okay except the text prompt is overlaying the previous one since the background isn't refreshing. Here is the code
Code:

//order: yellow, blue, red, lime

PFont andale;
String tyellow = "YELLOW";
String tred = "RED";

//circ1
float radius = 30.0;
float x = 50.0;
float y = 50.0;
float speedX = 0.6;
float speedY = 1.0;
int directionX = 1;
int directionY = -1;
color c1color = color(255);


color backgroundColor = color(255);



//mouse easing
float mx;
float my;
float targetX, targetY;
float easing = 0.05;

//yellow
float vred = 60;
boolean foundColor = false;
int leniency = 15;

//red
float vredd =




void setup(){
 size(480, 320);
 smooth();
 noStroke();
 andale = loadFont("AndaleMono-14.vlw");
 textFont(andale);
 ellipseMode(RADIUS);
 colorMode(HSB, 360, 100, 100);

}

void draw(){
 background(backgroundColor);
 noCursor();
 
//text yellow
//  fill(0);
//  text(tyellow, 400, 20, 70, 70);
 
 //color circ1
 if (frameCount % 90 == 0){
   float r = random(0, 360);
   c1color = color(r, 100, 100);
 }


 //1st circ
 fill(c1color);
 noStroke();
 ellipse(x, y, radius, radius);
 x += speedX * directionX;
 
 if ((x > width-radius) || (x < radius)) {
   directionX = -directionX;
 }
 y += speedY * directionY;
 if ((y > height-radius) || (y < radius)){
   directionY = -directionY;
 }

 //mouse easing
 targetX = mouseX;
 mouseX = constrain(mouseX, 0, width); //mouse or target
 float dmx = targetX - mx;
 if(abs(dmx) > 1) {
   mx += dmx * easing;
 }

 targetY = mouseY;
 mouseY = constrain(mouseY, 0, height); //constrain either mouse or target
 float dmy = targetY - my;
 if(abs(dmy) > 1) {
   my += dmy * easing;
 }

 stroke(0);
 strokeWeight(2);
 strokeCap(SQUARE);  
 line(mx-20, my, mx+20, my);
 line(mx, my-20, mx, my+20);
 
 if (foundColor == true) {
   //println("Hey, I found it.");
  fill(0);
  text(tred, 430, 20, 70, 70);
 }else if (foundColor == false){
   fill(0);
   text(tyellow, 400, 20, 70, 70);
 }

}

//background change
void mousePressed(){
 if(dist(x, y, mouseX, mouseY) < radius) {
   backgroundColor = c1color;

   if ( vred > hue(c1color) - leniency && vred < hue(c1color) + leniency) {
     foundColor = true;
   }
 
 }
}


Re: color game
Reply #1 - Mar 17th, 2010, 12:22am
 
I don't see the issue you report, since draw() starts correctly with a background() call.
Once I eliminated the variable vredd, the sketch is working as you describe, so I don't know what is your problem.
Re: color game
Reply #2 - Mar 17th, 2010, 12:36am
 
well that code i have only works for 2 different colors. And I wanted to make it so it continues on for say 5 different color prompts. Do you know what could be done about that? Sorry for the confusion.
Re: color game
Reply #3 - Mar 17th, 2010, 4:52am
 
I would make two arrays (or an array of a class, but that's another story).
One would hold the names of the colors, another would hold the color values.
Thus you can manage as many colors as you want with generic code (ie. no need to change the logic when you add one color).
Page Index Toggle Pages: 1