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 & HelpSyntax Questions › Someone save me, please!
Page Index Toggle Pages: 1
Someone save me, please! (Read 894 times)
Someone save me, please!
Jan 13th, 2010, 4:48am
 
I'm horrible at this coding thing, but there's a class at uni that I have to pass. I really need your help.
I need to modify a code that consists of a shape(let's call it X) chasing another shape(Y). The original code makes it go "game over/black screen" when one monster catches another. What I want to do is make the screen go black for a second or something, and shape Y starts chasing X instead (like pacman).
So far I just removed a couple of lines to remove the game over screen thing.
Quote:
/VARIABLES  
String myName = "C.S";


color monsterColor = color(random(255),random(255),random(255)); //assign a random color to the ball



//variables controlling the position of the ball
float xPos = 100+random(200);
float yPos = 100+random(200);

//variables controllin the size of the ball
float size = 40;

//variables controlling the speed of the ball  
float speedX = random(5);  
float speedY = random(5);


//SETUP CODE-BLOCK  
void setup() {
 size(400,400);
 background(255);
 frameRate(40);
 print(myName);
 smooth();
 noCursor(); //hides the mouse cursor inside the canvas
}

//DRAW CODE-BLOCK
void draw() {




   if(frameCount % 200 == 0) { //increase the speed every 200 frames
     speedX = speedX*1.5;
     speedY = speedY*1.5;
     frameCount = 0; //reset frameCount
    }

   background(255); //draw the background



   //collision detection for the x-axis
   if(xPos+size/2 >= width || xPos-size/2 < 0) {
     speedX = (speedX)*(-1);
     monsterColor = color(random(255),random(255),random(255));
    }

   //collision detection for the y-axis
   if(yPos+20 >=height || yPos-20 < 0) {
     speedY = speedY*-1;
     monsterColor = color(random(255),random(255),random(255));
    }

   //move the monster
   xPos = xPos + speedX;
   yPos = yPos + speedY;

   //draw the monster
   fill(monsterColor);
   stroke(monsterColor);
   ellipse(xPos, yPos, size/2,size/2);
   noFill();
   ellipse(xPos, yPos, size,size);
   line(xPos,yPos,xPos-20,yPos-20);
   ellipse(xPos-20,yPos-20,5,5);
   line(xPos,yPos,xPos+20,yPos-20);
   ellipse(xPos+20,yPos-20,5,5);
   arc(xPos,yPos+10, size-5, size-frameCount%15, 0, PI); //draws the mouth of the monster. It's hight will change over time because it depends on frameCount



   rectMode(CENTER);
   fill(0);
   stroke(0);
   ellipse(mouseX,mouseY,size,size);
   fill(255);
   ellipse(mouseX-8,mouseY-5,10,10);
   ellipse(mouseX+8,mouseY-5,10,10);
   ellipse(mouseX,mouseY+10,10,18-(dist(mouseX,mouseY, xPos, yPos)/400)*18); //changes the size of the prey's eyes based on the distance to the monster


   //check if the monster caught it's prey by calculating the distance between them
   float distance = dist(mouseX,mouseY, xPos, yPos);
   if(distance <= size) {  
     rectMode(CORNER);
     fill(0);
     rect(0,0,400,400);

    }

  }


Yeah.. I'm kinda dumb.
Re: Someone save me, please!
Reply #1 - Jan 13th, 2010, 5:26am
 
Hopefully no-one is going to do your homework for you and just post a complete code solution...

A couple of suggestions:

If you want to swap which monster chases which you'll need to find a way to separate out the monster drawing code.  One option : you can create a function to which you pass position parameters:

Code:
// call the function with:
drawMonster(xValue, yValue);

// define the function
void drawMonster(float xValue, float yValue) {
 // you can then reference the xValue and yValue passed to the function:
 ellipse(xValue,yValue, 20,20);
}


You'd then need a variable to store which monster is chasing and which is being chased - when there are just two states a boolean will do the trick.  You can then check the value of the boolean using a condition:

Code:
boolean theBoolean = false;

if(theBoolean == true) {
 // do this thing
}
// i.e. when it is false
else {
 // do something else
}


And simply change the value for instance when a monster is caught...

That should hopefully be enough to get you started.  Have a go and if you get stuck post your attempt and people can let you know where you're going wrong...
Re: Someone save me, please!
Reply #2 - Jan 13th, 2010, 5:40am
 
Well, I wouldn't mind if someone DID post the entire code/hint

I just don't understand the language.

But I'll work on these codes. Thanks.
Re: Someone save me, please!
Reply #3 - Jan 13th, 2010, 2:18pm
 


Did you write this code yourself?
Page Index Toggle Pages: 1