# Beginner help with sound and interaction of objects

**URL:** https://discourse.processing.org/t/beginner-help-with-sound-and-interaction-of-objects/22743
**Category:** Libraries
**Created:** [July 20, 2020, 8:45am UTC](https://discourse.processing.org/t/beginner-help-with-sound-and-interaction-of-objects/22743 "2020-07-20T08:45:50Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ahagele](https://avatars.discourse-cdn.com/v4/letter/a/a9a28c/32.png) [@ahagele](https://discourse.processing.org/u/ahagele)
#### Post date: [July 20, 2020, 8:45am UTC](https://discourse.processing.org/t/beginner-help-with-sound-and-interaction-of-objects/22743/1 "2020-07-20T08:45:50Z")

</div>

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);  
}  
}

---

<div class="post-metadata">

### Author: ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)
#### Post date: [July 20, 2020, 9:23am UTC](https://discourse.processing.org/t/beginner-help-with-sound-and-interaction-of-objects/22743/2 "2020-07-20T09:23:25Z")

</div>

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

---

<div class="post-metadata">

### Author: ![qewer3322](https://avatars.discourse-cdn.com/v4/letter/q/e19adc/32.png) [@qewer3322](https://discourse.processing.org/u/qewer3322)
#### Post date: [July 20, 2020, 9:48am UTC](https://discourse.processing.org/t/beginner-help-with-sound-and-interaction-of-objects/22743/3 "2020-07-20T09:48:12Z")

</div>

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

```auto
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;
}

```

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [July 20, 2020, 1:18pm UTC](https://discourse.processing.org/t/beginner-help-with-sound-and-interaction-of-objects/22743/4 "2020-07-20T13:18:43Z")

</div>

Hello,

Welcome to the forum!

There is a section on interaction here:

> **[Examples](https://processing.org/examples/)**
>
> Short, prototypical programs exploring the basics of programming with Processing.

> [@ahagele](#):
>
> I am very VERY new at coding

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:
> 
> - The Processing _website_ has references, examples, tutorials, etc.  
> [https://processing.org/](https://processing.org/)
> - The Processing PDE (IDE) has lots to offer!  
> An exploration of the tabs will reveal what is available; libraries, tools and local examples there.
> - The Coding Train  
> [https://www.youtube.com/channel/UCvjgXvBlbQiydffZU7m1\_aw](https://www.youtube.com/channel/UCvjgXvBlbQiydffZU7m1_aw)  
> [https://thecodingtrain.com/](https://thecodingtrain.com/)
> - Happy Coding  
> [Processing Tutorials - Happy Coding](https://happycoding.io/tutorials/processing/)
> - The source code can give insight if you are adventurous:  
> [GitHub - processing/processing: Source code for the Processing Core and Development Environment (PDE)](https://github.com/processing/processing)  
> Many of the Processing functions are here:  
> [processing/core/src/processing/core/PApplet.java at master · processing/processing · GitHub](https://github.com/processing/processing/blob/master/core/src/processing/core/PApplet.java)
> - The Processing Foundation  
> [https://processingfoundation.org/](https://processingfoundation.org/)  
> Check out the tabs.  
> In the education tab, there is a reference to the Coding Train.
> - And the Internet. `:)`

Some cool stuff here that will inspire:  
[https://natureofcode.com/book/introduction/](https://natureofcode.com/book/introduction/)

`:)`
