Need help with an assignment from school

Hello everyone,

I was given the assignment to create a program that allows the user to collect coins. Randomly generate a yellow circle on the screen, the user must move a square to meet the circle. Once they get their square, they get a point for doing so. Another circle should be generated in another random location. Draw the current number of points on the top left corner of the screen.
I have no idea were to start
gg

Good project. Start with a blank sketch and add things to it in small steps.

To get a blank sketch going, you will need to know about the setup() and draw() functions. You will need a call to background() in one, and size() in the other.

Next, make the background green. Which function controls the background color?

Next, realize that you are going to need to keep track of a few things. Specifically, you will need to remember the position of the player. This means you will need a couple of float variables.

Can you draw a player rectangle at the player’s position? Use rect() and fill()!

Now where is the coin? More variables! How does the coin’s location get set? Use random()!

Try putting something together now - ANYTHING - and post the code of your attempt for more help. Tell us what works. Tell us what doesn’t. Tell us what you have tried. Tell us what you expected to happen. Show us your code.

2 Likes

I have a problem with the square collecting the circle and i also have a problem with the score text number changing as i collect the coins. I used circleX and circleY to generate random locations after the circle is gotten.

this is what i have so far

float x;
float y;
float circleX;
float circleY;

 
void setup(){
  size (400, 400);
  x = width/2;
  y = height/2;
  circleX = random(0,width);
  circleY = random(0,height);
}
 
void draw(){
  background(0,255,0);
  fill(255,0,0);
  rect(x, y, 20,20);
  fill(0);
 text("Score : ", 0, 15); 
  ellipse(circleX, circleY, 10,10);
}
 
void keyPressed() {
  ellipse(circleX, circleY, 10,10);
  if ((keyPressed == true) && (key == CODED)) {
    if (keyCode == UP){
     y --;
    } else if (keyCode == DOWN) {
      y ++;
    } else if (keyCode == LEFT){
      x --;
    } else if (keyCode == RIGHT) {
      x ++;
    }  
  } 
}
1 Like

Now that we have seen the code you have already, we have a much better idea of where you are in your project! You don;t need help with the basics - your question is much more specific now!

The next thing you should add is a function that determines in the player is touching the coin. It should look like this:

boolean is_player_on_coin(){
  /// Returns true if the player is on the coin, or false otherwise.
  // TODO: Make this function return true sometimes!
  return( false );
}

You will need to write the body of this function yourself. What can you say about the position of the coin in relation to the position and size of the player when they are touching?

Here’s an image that might help you visualize it:

DoesCollide

Try writing this function yourself now. Use an if statement!

Once you have written this function, can you use it? How often do you need to check for the player collecting the coin? What happens if the player collects the coin? You will need another if statement!

4 Likes

I tried something at school for the square to collide with the circle. Please check it and tell me what i could do better.

int x;
int y;
float circleX;
float circleY;
int points = 0;

 
void setup(){
  size (400, 400);
  x = width/2;
  y = height/2;
  circleX = random(0,400);
  circleY = random(0,400);
}
 
void draw(){
  background(0,255,0);
 fill(255,0,0);
  rect(x, y, 20,20);
  fill(0); 
  ellipse(circleX, circleY, 6,6);
fill(0);
text("Score :" + points, 0,20);
}
void keyPressed() {
   if ((keyPressed == true) && (key == CODED)) {
    if (keyCode == UP){
     y --;
    } else if (keyCode == DOWN) {
      y ++;
    } else if (keyCode == LEFT){
      x --;
    } else if (keyCode == RIGHT) {
      x ++;
    }  
    
      
    }
    
//collision with the coin
if ((x >= circleX) && (y >= circleY)){
  ellipse(circleX, circleY, 6,6);
  points++;
  }
}

you need to know the distance between circle and the rectangle to do circle collision detection
use dist() to get distance between two point. and then think of what @TfGuy44 has explained

1 Like

I still have no ideas on how to do the collision between the square and coin.

2 Likes
int x;
int y;
float circleX;
float circleY;
int points = 0;

 
void setup(){
  size (400, 400);
  x = width/2;
  y = height/2;
  circleX = random(0,400);
  circleY = random(0,400);
}
 
void draw(){
  background(0,255,0);
 fill(255,0,0);
  rect(x, y, 20,20);
  fill(0); 
  ellipse(circleX, circleY, 6,6);
fill(0);
text("Score :" + points, 0,20);
}
void keyPressed() {
   if ((keyPressed == true) && (key == CODED)) {
    if (keyCode == UP){
     y --;
    } else if (keyCode == DOWN) {
      y ++;
    } else if (keyCode == LEFT){
      x --;
    } else if (keyCode == RIGHT) {
      x ++;
    }  
    
      
    }
//collision 
if((dist(x,y,circleX,circleY) > 400)){
  boolean is_player_on_coin(){
    return( false );
  ellipse(circleX, circleY, 6,6);
  points++;

}
}

please check the collision aspect. please tell me what im doing wrong

thats not the right way you call a function or define a function. see Reference / Processing.org

1 Like