How to upload image for the ball as well as adding a sound to ball everytime it's clicked (URGENT)

edited November 2016 in Library Questions

Hey Guys! So can someone please help!! I have 3 day's left to hand in this project and I need 2 things.

  1. I need code so that i'm able to upload a PNG/JPG image for the ball that's moving around the screen.

  2. I need code so that a sound can be played every time the ball is clicked using the minim library.

Feel free to modify the code and hand it back, any help is appreciated and is well needed.

import ddf.minim.*;
int rad = 60;                                   // Width of the shape
float xpos, ypos;                               // Starting position of shape    
float xspeed = 2.0;                             // Speed of the shape
float yspeed = 2.0;                             // Speed of the shape    
int xdirection = 1;                             // Left or Right
int ydirection = 1;                             // Top to Bottom
int score=0;                                     //Inital score
int lives=5;                                     //Number of lives you start with
boolean lost=false;                              //Have you lost yet?                  
int speed=1;   
PImage bg;
int y;
AudioPlayer player;
Minim minim;

void setup() 
  size(1297, 736);
  minim = new Minim(this);
  ellipse(xpos, height/10,100,100);
  xpos = width/2;                               // Set the starting position of the shape
  ypos = height/2;
  bg = loadImage("stem.jpg");                   //Load Image (jpg,png,gif)
  player = minim.loadFile("Song.wav");          //Load Audio File (mp3,wav)


void draw() 

  xpos = xpos + ( xspeed * xdirection );         // Update the position of the shape
  ypos = ypos + ( yspeed * ydirection );

  if (xpos > width-rad || xpos < rad) {          // Test to see if the shape exceeds the boundaries of the screen
                                                 // If it does, reverse its direction by multiplying by -1
    xdirection *= -1; 
  if (ypos > height-rad || ypos < rad) {
    ydirection *= -1;

  ellipse(xpos, ypos, rad, rad);                    //Draw the shape
  text("score = "+score,10,10);                     //Print the score on the screen
  text("lives = "+lives,width-80,10);               //Print remaining lives
  if (lives<=0)                                     //Check to see if you lost
    text("Click to Restart", 450,200);
    noLoop();                                       //Stop looping at the end of the draw function
void mousePressed()                                 //Runs whenever the mouse is pressed
  if (dist(mouseX, mouseY, xpos, ypos)<=rad)        //Did we hit the target?
    score=score+speed;                              //Increase the speed
    speed=speed+1;                                  //Increase the Score
  else                                              //We missed
    if (speed<1)                                    //If speed is greater than 1 decrease the speed
    lives=lives-1;                                  //Take away one life
  if (lost==true)                                   //If we lost the game, reset now and start over 
    speed=1;                                        //Reset all variables to initial conditions
    loop();                                         //Begin looping draw function again


  • upload a PNG/JPG image for the ball

    Do you mean that instead of drawing an ellipse() you want to draw an image()? Have you looked at the image reference page and example? Try that -- it should be really easy, share your code and your question if you get stuck.

  • Okay, so I changedellipsetoloadImage("ball icon.png")and then started and nothing had changed

  • edited November 2016 Answer ✓

    ellipse() draws an ellipse.

    loadImage() loads an image.

    To draw an image, you should use the image() function.

    What you really need is a global variable, of type PImage, that is your image:

    PImage my_image;

    Then, in setup() because you only need and want to load the image once, you call loadImage():

    my_image = loadImage("somefilename.jpg");

    Now that your image is loaded, it will stay loaded, and you can draw it in draw():


    Notice where the image is drawn - at (10,20). To draw it somewhere else, change those parameters to you image() call. If you want to draw it where your ellipse was, use the ellipse's coordinates.

    Also remove the ellise function, as you o not want to draw it any more.

    As for help with minim, look at the minim examples.

Sign In or Register to comment.