Beginner help with sound and interaction of objects

I am very VERY new at coding and I’m not sure how to make it so if the apple touches the basket then the score will increase by one; instead the score increases by one when it crosses the Y-axis of the basket so every time no matter what. I also need help with how to make it so every time an apple falls it plays the same sound; instead, it plays and stops it once by doesn’t repeat every time the apple falls.

The code:

//importing sound through minim
//I tried to use Soundfile but it didn’t work
import ddf.minim.*;

Minim minim;

AudioPlayer player;

//loading images
PImage apple;
PImage basket;
PImage cloud;

//variables
float appleSpeed = 3; //speed that apple is falling
float appleXPosition;
float appleYPosition = 0;
float score = 0;
float basketXPosition;
float basketYPosition = 310;
float basketpoint;
float grass;

void setup()
{
size(600, 400);
background(195, 251, 250);
smooth();
//loading images
apple = loadImage(“apple.png”);
basket = loadImage(“basket.png”);
cloud = loadImage(“cloud.png”);

appleXPosition= random(10, 590);

basketXPosition = mouseX; //basket cooresponds with mouse

//new mp3 file imported (falling noise for apples)
minim = new Minim(this);
player = minim.loadFile(“Fallingsound.mp3”);
}

void draw()
{

background(195, 251, 250);

//scoreboard
fill(0);
text(score, 550, 20);

//grass on ground
fill(139, 241, 174);
ellipse(300, 400, 600, 40);

//Clouds
image(cloud, 100, 20, 60, 60);
image(cloud, 300, 40, 80, 80);
image(cloud, 500, 25, 80, 80);

//code for apple falling
image(apple, appleXPosition, appleYPosition, 50, 50);
appleYPosition = appleYPosition + appleSpeed;

//making noise when apple falls
if (appleYPosition <= 5) {

player.play();

}
//stopping noise once apple hits the floor
if (appleYPosition>= 350) {
minim.stop();
player.close();
//How do I get it to play and stop everytime
//the apple hits the floor and appears at the top,
//not just on the first apple
}

//if apple hits floor
if (appleYPosition >= 350) {
appleXPosition = random (10, 590);
appleYPosition = 0;
}
//Getting basket to correlate with the mouse along x axis
image(basket, mouseX, basketYPosition, 70, 70);

//if the apple goes in the basket
if (appleYPosition== basketXPosition) {
appleYPosition= 0;
appleXPosition = random (10, 590);
score = score + 1; //score increases by one
//I couldn’t figure out how to make it so that when the
//apple specifically touches the basket you would gain a point
}

//if score gets to 10 show “gameover” on screen
if (score == 10) {
background (225);
fill(0);
text(“gameover”, 250, 200, 100, 200);
}
}

1 Like

Edit; Post deleted. Sorry I’m in the wrong topic

This should help you with object interaction:
(Press enter to start again after gameover)

int appley = 0;
int basketx;
float applex = random(0, 560);
int point = 0;
boolean gameover;

void setup() {
  size(600, 600);
  frameRate(200);
}

void draw() {
  background(66, 239, 245);

  //apple
  appley = appley + 2;
  rect(applex, appley, 30, 30);

  //basket
  basketx = mouseX - 45;
  rect(basketx, 500, 90, 60);

  //points (and apple reset)
  if (appley>525 && appley<527 && applex > basketx && applex < basketx + 90) {
    point++;
    appley = 0; 
    applex = random(0, 560); 
  }
  fill(0);
  textSize(25);
  text("Score: " + point, 10, 25);
  
  //gameover
  if (appley > 580 && applex < basketx || appley > 580 && applex > basketx + 60) gameover = true;
  
  //gameover
  if (gameover == true) {
    fill(66, 239, 245);
    rect(-1,-1,600,600);
    fill(0);
    textSize(50);
    text("Game Over",50,100);
    appley = 0;
    point = 0;
  }
}

void keyReleased() {
  if (key == ENTER) gameover = false;
}

Hello,

Welcome to the forum!

There is a section on interaction here:

We were all new once… it is a good place to start.

One of the best tools in a programmers tool chest is knowing the resources available to you and learning to navigate, filter and use them.

This is a very short list:

Resources < Click here to expand !

I encourage you to review the resources available here:

Some cool stuff here that will inspire:
https://natureofcode.com/book/introduction/

:)