collision detection using keys

edited January 2015 in Arduino

i am making a game where you have to catch items as they fall, i managed to get the collision detection to work with the mouse function, but as i am making it to work with arduino i cant get the collision detection to work, code is below, thank you for any help.

import processing.serial.*;
import cc.arduino.*;
import com.tinkerkit.*;

Arduino arduino;

//declare the button
TKButton but;
TKButton but1;

{
  arduino = new Arduino(this, Arduino.list()[2], 57600);
}

PFont font1;//font for game over
PFont font2;//font for score and timer
PImage F_icecream;//icecream image
PImage F_apple;//orange image
PImage F_cherry;//cherry image
PImage egg;//bad egg image
PImage ball;//catcher image
PImage cloud1;
PImage cloud2;
PImage cloud3;
PImage dayImg;
PImage nightImg;


float[] y_icecream = new float[2000];//totaly 2000 icecreams 
float[] y_apple = new float[2000];//totaly 2000
float[] y_cherry = new float[1800];
float[] y_egg = new float[3000];
float[] x_cloud1 = new float[10];
float[] x_cloud2 = new float[10];
float[] x_cloud3 = new float[10];

float speed = 1;
int diameter =200;
float TUx;
float TUy;
float light=0;
float fade=120;


int time1=120000;
int seconds=120000;//60 seconds;
String secondsBox=nfc(seconds);// tansfer timer number in to text box
String clock = "Game Over!";//game over string

int score=0;//score beging as "0"
String scoreBox=nfc(score);//tansfer score number in to text box

int currentTime = millis();

ball ball1;

int gameState;



{
  but = new TKButton(arduino, TK.I0);

  but1 = new TKButton(arduino, TK.I1);
}

void setup() {
  size(540, 960);
  String scoreBox=nfc(score);//display score at first


  gameState = 0;
  ball1 = new ball (400, 720, 100);


  ball = loadImage("ball.png");


  cloud1 = loadImage("cloud1.png");
  for (int i = 0; i < x_cloud1.length; i++) {
    x_cloud1[i] = random(-4500, 790);
  }
  cloud2 = loadImage("cloud2.png");
  for (int i = 0; i < x_cloud2.length; i++) {
    x_cloud2[i] = random(-4500, 790);
  }
  cloud3 = loadImage("cloud3.png");
  for (int i = 0; i < x_cloud3.length; i++) {
    x_cloud3[i] = random(-4500, 790);
  }

  //display icecreams in different places  
  F_icecream = loadImage("icecream.gif");
  for (int i = 0; i < y_icecream.length; i++) {
    y_icecream[i] = random(-45000, 790);
  }
  //dispaly oranges in different places
  F_apple = loadImage("apple.gif");
  for (int i = 0; i < y_apple.length; i++) {
    y_apple[i] = random(-60000, 790);
  }
  //display cherries in different places
  F_cherry = loadImage("cherry.gif");
  for (int i = 0; i < y_cherry.length; i++) {
    y_cherry[i] = random(-60000, 790);
  }
  //display bad eggs in different places
  egg = loadImage("egg.gif");
  for (int i = 0; i < y_egg.length; i++) {
    y_egg[i] = random(-60000, 790);
  }

  size(540,960);
  println(Arduino.list());
  dayImg = loadImage("pic_day.jpg");
  nightImg = loadImage("pic_night.jpg");
}


void draw() {
  light = arduino.analogRead(3);
  println(light);
  fade = map(light,50,950,0,255);
  tint(255, fade);
  image(dayImg, 0, 0, width, height);
  tint(255, 255-fade);
  image(nightImg, 0, 0, width, height);

  int currentTime = millis();

  //println(currentTime);

  //scoreBox
  String scoreBox=nfc(score);//update score box
  fill(0);

  text(scoreBox, 25, 40, 200, 200);//font for score box

  cloud1();
  cloud2();
  cloud3();


  if (currentTime< time1) {           //boolean for checking the timer
    String secondsBox=nfc(seconds);
    fill(0);

    text(secondsBox, 520, 40, 200, 200);


    //display every fruit
    icecream();
    apple();
    cherry();
    egg();
    fill(0);

    text(scoreBox, 25, 40, 200, 200);

    ball1.display();
    ball1.keyPressed();

  }
}




//cloud function
void cloud1() {
  for (int i = 0; i < x_cloud1.length; i++) {
    x_cloud1[i] += 0.4;
    float y = i * 6;
    image(cloud1, x_cloud1[i], y, y*5, y*2);
  }
}

void cloud2() {
  for (int i = 0; i < x_cloud2.length; i++) {
    x_cloud2[i] += 0.3;
    float y = i * 6;
    image(cloud2, x_cloud2[i], y, i*30, i*15);
  }
}

void cloud3() {
  for (int i = 0; i < x_cloud3.length; i++) {
    x_cloud3[i] += 0.2;
    float y = i * 6;
    image(cloud3, x_cloud3[i], y, y*5, y);
  }
}




//fruits function
void icecream() {
  //create icecream arraylist
  for (int i = 0; i < y_icecream.length; i++) {
    y_icecream[i] += 3;
    float x = i * 6;
    image(F_icecream, x, y_icecream[i]);

    //if icecream be catched
    if (x-30<mouseX&&mouseX<x+30 && y_icecream[i]-30<mouseY&&mouseY<y_icecream[i]+30) {
      score= score+2;//score +2
      fill(0);
      text(scoreBox, 25, 40, 200, 200);
      y_icecream[i]=-100;//that object dispeared
    }
  }
}

void apple() {
  //create apple arraylist
  for (int i = 0; i < y_apple.length; i++) {
    y_apple[i] += 5;
    float x = i * 6;
    image(F_apple, x, y_apple[i]);
    //if orange be catched
    if (x-30<mouseX&&mouseX<x+30 && y_apple[i]-30<mouseY&&mouseY<y_apple[i]+30) {
      score= score+1;//score+1
      fill(0);

      text(scoreBox, 25, 40, 200, 200);
      y_apple[i]=-100;//that object dispeared
    }
  }
}

void cherry() {
  //create cherries arraylist
  for (int i = 0; i < y_cherry.length; i++) {
    y_cherry[i] += 4;
    float x = i * 6;
    image(F_cherry, x, y_cherry[i]);

    //if orange be catched
    if (x-30<mouseX&&mouseX<x+30 && y_cherry[i]-30<mouseY&&mouseY<y_cherry[i]+30) {
      score= score+5;
      fill(0);

      text(scoreBox, 25, 40, 200, 200);
      y_cherry[i]=-100;//that object dispeared
    }
  }
}

void egg() {
  //create bad egg arraylist
  for (int i = 0; i < y_egg.length; i++) {
    y_egg[i] += 8;//egg drop speed
    float x = i * 6;
    image(egg, x, y_egg[i]);

    //if orange be catched
    if (x-40<mouseX&&mouseX<x+40 && y_egg[i]-40<mouseY&&mouseY<y_egg[i]+40) {

      time1=0;
      y_egg[i]=-100;//that object dispeared
    }
  }
}









void display() {
  fill(255, 0, 0, 70);
  noStroke();



  fill(255);
  noStroke();
}

void reset() {
}






class ball {

  float x;
  float y;
  float speed;
  float r; //radius



  ball(float tempX, float tempY, float tempR) {
    x = tempX;
    y = tempY;
    r = tempR;
    speed = 0;
  }
  void change() {
  }

  void display() {
    noStroke();
    image(ball, x, y, r, r);
  }
  //key commands
  //ball flies off page in response to key command
  void keyPressed() {
    if (key == CODED) {
    }
    if (but.read ()== TK.HIGH) {
      x = x+5;
      if (x >= width - 25) {
        x = width - 25;
      }

      println(but.read());
    } else if (but1.read() == TK.HIGH) {
      x = x-5;
      if (x <= 25) {
        x = 25;
      }

      println(but1.read());
    }
  }

  void tick() {
    seconds-=5;
  }
}
Tagged:

Answers

  • edited January 2015

    where's your mouse collision detection? Which lines?

    What have you tried? in which lines?

    Since you have only one ball, I assume you want to catch with the ball either an egg or a cherry etc.; is that correct? You don't say.

    Anyway, for collision detection you have to loop over all cherrys, eggs and so on and test against ball position:

    //if icecream be catched
    if (x-30<ball.x && ball.x+ball.r < x+30 && 
        y_icecream[i]-30<ball.y && ball.y+ball.r<y_icecream[i]+30) {
            score= score+2;//score +2
    

    Remark:

    Later you want to have an item class for all egg and icecream etc... since at the moment, the x-pos is always the same for them...

    The item class would look like the ball class just with an PImage in it.

    You can have an ArrayList of that class then.

    ;-)

  • thank you for your help really appreciate it :) however i am no getting an error saying:

    ball.x cannot be resolved or is not a field.

  • You have both a ball class and a ball variable (which is not an instance of the class!). You should stick to the convention of naming classes with an initial uppercase letter: class Ball, variable ball, etc.

  • so i have done as suggested and i am getting an error the function display() does not exist. sorry to be a pain i am still learning.

  • do you have ball1.display();

    ?

    Pls show your code

  • edited January 2015

    remark 1

    • you got one class ball (which you should name Ball or BallClass)

    • you got one image ball (which you should name ballImage or so)

    • you got one object ball1 (which name is ok, you could say ballObject)

    remark 2

    • you got one display() outside the function that you never use; delete it. Because you have one display in the class that you use.

    pls note, you can't say ball.display, because ball is the name of the class (or the image); you must use ball1.display() since ball1 (with 1) is your object.

    there is a tutorial on OOP (classes & objects) - pls read

    we can't run your code since we don't have the images and the arduino with the buttons

    this is your stripped down code - keys 4 and 5

    ;-)

    //import processing.serial.*;
    //import cc.arduino.*;
    //import com.tinkerkit.*;
    
    //Arduino arduino;
    
    //declare the button
    //TKButton but;
    //TKButton but1;
    
    
    //arduino = new Arduino(this, Arduino.list()[2], 57600);
    
    
    PFont font1;//font for game over
    PFont font2;//font for score and timer
    
    PImage F_icecream;//icecream image
    PImage F_apple;//orange image
    PImage F_cherry;//cherry image
    PImage egg;//bad egg image
    PImage ball;//catcher image
    PImage cloud1;
    PImage cloud2;
    PImage cloud3;
    PImage dayImg;
    PImage nightImg;
    
    
    float[] y_icecream = new float[2000];//totaly 2000 icecreams 
    float[] y_apple = new float[2000];//totaly 2000
    float[] y_cherry = new float[1800];
    float[] y_egg = new float[3000];
    float[] x_cloud1 = new float[10];
    float[] x_cloud2 = new float[10];
    float[] x_cloud3 = new float[10];
    
    float speed = 1;
    int diameter =200;
    float TUx;
    float TUy;
    float light=0;
    float fade=120;
    
    
    int time1=120000;
    int seconds=120000;//60 seconds;
    String secondsBox=nfc(seconds);// tansfer timer number in to text box
    String clock = "Game Over!";//game over string
    
    int score=0;//score beging as "0"
    String scoreBox=nfc(score);//tansfer score number in to text box
    
    int currentTime = millis();
    
    Ball ball1;
    
    int gameState;
    
    
    void setup() {
      size(540, 960);
      String scoreBox=nfc(score);//display score at first
    
    
      gameState = 0;
      ball1 = new Ball (400, 720, 100);
    
    
      ball = loadImage("ball.png");
    
    
      cloud1 = loadImage("cloud1.png");
      for (int i = 0; i < x_cloud1.length; i++) {
        x_cloud1[i] = random(-4500, 790);
      }
      cloud2 = loadImage("cloud2.png");
      for (int i = 0; i < x_cloud2.length; i++) {
        x_cloud2[i] = random(-4500, 790);
      }
      cloud3 = loadImage("cloud3.png");
      for (int i = 0; i < x_cloud3.length; i++) {
        x_cloud3[i] = random(-4500, 790);
      }
    
      //display icecreams in different places  
      F_icecream = loadImage("icecream.gif");
      for (int i = 0; i < y_icecream.length; i++) {
        y_icecream[i] = random(-45000, 790);
      }
      //dispaly oranges in different places
      F_apple = loadImage("apple.gif");
      for (int i = 0; i < y_apple.length; i++) {
        y_apple[i] = random(-60000, 790);
      }
      //display cherries in different places
      F_cherry = loadImage("cherry.gif");
      for (int i = 0; i < y_cherry.length; i++) {
        y_cherry[i] = random(-60000, 790);
      }
      //display bad eggs in different places
      egg = loadImage("egg.gif");
      for (int i = 0; i < y_egg.length; i++) {
        y_egg[i] = random(-60000, 790);
      }
    
      size(540, 960);
      //println(Arduino.list());
      dayImg = loadImage("pic_day.jpg");
      nightImg = loadImage("pic_night.jpg");
    
      background(111);
    }
    
    void draw() {
      background(111); 
      light = 3;  // ""arduino.analogRead(3);
      println(light);
      fade = map(light, 50, 950, 0, 255);
      //  tint(255, fade);
      //image(dayImg, 0, 0, width, height);
      //  tint(255, 255-fade);
      //image(nightImg, 0, 0, width, height);
    
      int currentTime = millis();
    
      //println(currentTime);
    
      //scoreBox
      String scoreBox=nfc(score);//update score box
      fill(0);
      text(scoreBox, 25, 40, 200, 200);//font for score box
    
      cloud1();
      cloud2();
      cloud3();
    
    
      if (currentTime< time1) {           //boolean for checking the timer
        String secondsBox=nfc(seconds);
        fill(0);
    
        text(secondsBox, 520, 40, 200, 200);
    
    
        //display every fruit
        icecream();
        apple();
        cherry();
        egg();
        fill(0);
    
        text(scoreBox, 25, 40, 200, 200);
    
        ball1.display();
        ball1.keyPressed();
      }
      else 
        println ("time is up");
    }
    
    
    
    
    //cloud function
    void cloud1() {
      for (int i = 0; i < x_cloud1.length; i++) {
        x_cloud1[i] += 0.4;
        float y = i * 6;
        //  image(cloud1, x_cloud1[i], y, y*5, y*2);
      }
    }
    
    void cloud2() {
      for (int i = 0; i < x_cloud2.length; i++) {
        x_cloud2[i] += 0.3;
        float y = i * 6;
        //   image(cloud2, x_cloud2[i], y, i*30, i*15);
      }
    }
    
    void cloud3() {
      for (int i = 0; i < x_cloud3.length; i++) {
        x_cloud3[i] += 0.2;
        float y = i * 6;
        //  image(cloud3, x_cloud3[i], y, y*5, y);
      }
    }
    
    
    
    
    //fruits function
    void icecream() {
      //create icecream arraylist
      for (int i = 0; i < y_icecream.length; i++) {
        y_icecream[i] += 3;
        float x = i * 6;
        // image(F_icecream, x, y_icecream[i]);
    
        //if icecream be catched
        if (x-30<mouseX&&mouseX<x+30 && y_icecream[i]-30<mouseY&&mouseY<y_icecream[i]+30) {
          score= score+2;//score +2
          fill(0);
          text(scoreBox, 25, 40, 200, 200);
          y_icecream[i]=-100;//that object dispeared
        }
      }
    }
    
    void apple() {
      //create apple arraylist
      for (int i = 0; i < y_apple.length; i++) {
        y_apple[i] += 5;
        float x = i * 6;
        // image(F_apple, x, y_apple[i]);
        //if orange be catched
        if (x-30<mouseX&&mouseX<x+30 && y_apple[i]-30<mouseY&&mouseY<y_apple[i]+30) {
          score= score+1;//score+1
          fill(0);
    
          text(scoreBox, 25, 40, 200, 200);
          y_apple[i]=-100;//that object dispeared
        }
      }
    }
    
    void cherry() {
      //create cherries arraylist
      for (int i = 0; i < y_cherry.length; i++) {
        y_cherry[i] += 4;
        float x = i * 6;
        // image(F_cherry, x, y_cherry[i]);
    
        //if orange be catched
        if (x-30<mouseX&&mouseX<x+30 && y_cherry[i]-30<mouseY&&mouseY<y_cherry[i]+30) {
          score= score+5;
          fill(0);
    
          text(scoreBox, 25, 40, 200, 200);
          y_cherry[i]=-100;//that object dispeared
        }
      }
    }
    
    void egg() {
      //create bad egg arraylist
      for (int i = 0; i < y_egg.length; i++) {
        y_egg[i] += 8;//egg drop speed
        float x = i * 6;
        // image(egg, x, y_egg[i]);
    
        //if orange be catched
        if (x-40<mouseX&&mouseX<x+40 && y_egg[i]-40<mouseY&&mouseY<y_egg[i]+40) {
    
          time1=0;
          y_egg[i]=-100;//that object dispeared
        }
      }
    }
    
    //void display() {
    //  fill(255, 0, 0, 70);
    //  noStroke();
    //
    //
    //
    //  fill(255);
    //  noStroke();
    //}
    //
    //void reset() {
    //}
    
    
    
    // ======================================
    
    
    class Ball {
    
      float x;
      float y;
      float speed;
      float r; //radius
    
      Ball(float tempX, float tempY, float tempR) {
        x = tempX;
        y = tempY;
        r = tempR;
        speed = 0;
      }
      void change() {
      }
    
      void display() {
        noStroke();
        // image(ball, x, y, r, r);
        fill(0);
        ellipse (x, y, 11, 11);
      }
      //key commands
      //ball flies off page in response to key command
      void keyPressed() {
        if (key == CODED) {
        }
        if (key == '5') {
          x = x+5;
          if (x >= width - 25) {
            x = width - 25;
          }
    
          //println(but.read());
        } 
        else if (key == '4') {
          x = x-5;
          if (x <= 25) {
            x = 25;
          }
    
          //println(but1.read());
        }
      }
    
      void tick() {
        seconds-=5;
      }
    }
    
  • i am still having no luck with it :| i have tried everything suggested above and get new errors is there any way of posting the actual file to show?

  • post your code here or in dropbox (public)

    maybe ask in the arduino forum http://arduino.cc/en

  • https://www.dropbox.com/s/27m0gixd8f5wa43/sketch_150102990oop2.zip?dl=0 i have changed it so you don't need the arduino as suggested. :)

  • edited January 2015 Answer ✓

    hmm...

    the thing is, your images are much bigger than they seem to be...

    they have a kind of transparent background around them

    so their x,y position is in fact far left and above each image.

    I countered that by saying ´imageMode(CENTER);´ which makes it so that x,y is in the middle / center of each image.

    You can also cut the frame that is around each image in a separate graphics program like irfanview.

    it works now for the eggs. I had to take out ´time = 0;´ though; that froze everything.

    instead of the collision approach with > and && etc. you might want to try rectangle collision (but actually I think the former approach is good enough).

    Best, Chrisir ;-)

    import processing.serial.*;
    //import cc.arduino.*;
    //import com.tinkerkit.*;
    
    //Arduino arduino;
    
    //declare the button
    //TKButton but;
    //TKButton but1;
    //
    //{
    //  arduino = new Arduino(this, Arduino.list()[2], 57600);
    //}
    
    PFont font1;//font for game over
    PFont font2;//font for score and timer
    PImage F_icecream;//icecream image
    PImage F_apple;//orange image
    PImage F_cherry;//cherry image
    PImage egg;//bad egg image
    PImage ballImage;//catcher image
    PImage cloud1;
    PImage cloud2;
    PImage cloud3;
    PImage dayImg;
    PImage nightImg;
    
    
    float[] y_icecream = new float[2000];//totaly 2000 icecreams 
    float[] y_apple = new float[2000];//totaly 2000
    float[] y_cherry = new float[1800];
    float[] y_egg = new float[3000];
    float[] x_cloud1 = new float[10];
    float[] x_cloud2 = new float[10];
    float[] x_cloud3 = new float[10];
    
    float speed = 1;
    int diameter =200;
    float TUx;
    float TUy;
    float light=0;
    float fade=120;
    
    
    int time1=120000;
    int seconds=120000;//60 seconds;
    String secondsBox=nfc(seconds);// tansfer timer number in to text box
    String clock = "Game Over!";//game over string
    
    int score=0;//score beging as "0"
    String scoreBox=nfc(score);//tansfer score number in to text box
    
    int currentTime = millis();
    
    Ball ball1;
    
    int gameState;
    
    
    
    //{
    //  but = new TKButton(arduino, TK.I0);
    //
    //  but1 = new TKButton(arduino, TK.I1);
    //}
    
    // -----------------------------------------------
    
    void setup() {
      size(540, 960);
      //  String scoreBox=nfc(score);//display score at first
    
    
      imageMode(CENTER); // !!!!!!!!!!!!!!!!!!!
    
    
      gameState = 0;
      ball1 = new Ball (400, 720, 100);
    
      ballImage = loadImage("ball.png");
    
    
      cloud1 = loadImage("cloud1.png");
      for (int i = 0; i < x_cloud1.length; i++) {
        x_cloud1[i] = random(-4500, 790);
      }
      cloud2 = loadImage("cloud2.png");
      for (int i = 0; i < x_cloud2.length; i++) {
        x_cloud2[i] = random(-4500, 790);
      }
      cloud3 = loadImage("cloud3.png");
      for (int i = 0; i < x_cloud3.length; i++) {
        x_cloud3[i] = random(-4500, 790);
      }
    
      //display icecreams in different places  
      F_icecream = loadImage("icecream.gif");
      for (int i = 0; i < y_icecream.length; i++) {
        y_icecream[i] = random(-45000, 790);
      }
      //dispaly oranges in different places
      F_apple = loadImage("apple.png");
      for (int i = 0; i < y_apple.length; i++) {
        y_apple[i] = random(-60000, 790);
      }
      //display cherries in different places
      F_cherry = loadImage("cherry.gif");
      for (int i = 0; i < y_cherry.length; i++) {
        y_cherry[i] = random(-60000, 790);
      }
      //display bad eggs in different places
      egg = loadImage("egg.gif");
      for (int i = 0; i < y_egg.length; i++) {
        y_egg[i] = random(-60000, 790);
      }
    
      //println(Arduino.list());
      dayImg = loadImage("pic_day.jpg");
      nightImg = loadImage("pic_night.jpg");
    }
    
    
    void draw() {
    
      background(nightImg); 
    
      //light = arduino.analogRead(3);
      //println(light);
      fade = map(light, 50, 950, 0, 255);
      tint(255, fade);
      image(dayImg, 0, 0, width, height);
      tint(255, 255-fade);
      image(nightImg, 0, 0, width, height);
    
      int currentTime = millis();
      println(currentTime);
    
      //scoreBox
      String scoreBox=nfc(score);//update score box
      fill(0);
      text(scoreBox, 25, 40, 200, 200);//font for score box
    
      cloud1();
      cloud2();
      cloud3();
    
      if (currentTime< time1) {           //boolean for checking the timer
        String secondsBox=nfc(seconds);
        fill(0);
        text(secondsBox, 520, 40, 200, 200);
    
        //display every fruit
        icecream();
        apple();
        cherry();
        egg();
        fill(0);
    
        text(scoreBox, 25, 40, 200, 200);
    
        ball1.display();
        // ball1.keyPressed();
      }
    }// func draw() 
    
    // ---------------------------------
    
    
    //cloud function
    void cloud1() {
      for (int i = 0; i < x_cloud1.length; i++) {
        x_cloud1[i] += 0.4;
        float y = i * 6;
        image(cloud1, x_cloud1[i], y, y*5, y*2);
      }
    }
    
    void cloud2() {
      for (int i = 0; i < x_cloud2.length; i++) {
        x_cloud2[i] += 0.3;
        float y = i * 6;
        image(cloud2, x_cloud2[i], y, i*30, i*15);
      }
    }
    
    void cloud3() {
      for (int i = 0; i < x_cloud3.length; i++) {
        x_cloud3[i] += 0.2;
        float y = i * 6;
        image(cloud3, x_cloud3[i], y, y*5, y);
      }
    }
    
    
    //fruits functions ----------------------------
    
    void icecream() {
      //create icecream arraylist
      for (int i = 0; i < y_icecream.length; i++) {
        y_icecream[i] += 3;
        float x = i * 6;
        image(F_icecream, x, y_icecream[i]);
    
        //if icecream be catched
        if (x-30<ball1.x && ball1.x+ball1.r < x+30 && 
          y_icecream[i]-30<ball1.y && ball1.y+ball1.r<y_icecream[i]+30) {
          score= score+2;//score +2
          fill(0);
          text(scoreBox, 25, 40, 200, 200);
          y_icecream[i]=-100;//that object dispeared
        }
      }
    }
    
    void apple() {
      //create apple arraylist
      for (int i = 0; i < y_apple.length; i++) {
        y_apple[i] += 5;
        float x = i * 6;
        image(F_apple, x, y_apple[i]);
        //if apple be catched
        if (x-30<ball1.x && ball1.x+ball1.r < x+30 && 
          y_apple[i]-30<ball1.y && ball1.y+ball1.r<y_apple[i]+30) {
          score= score+1;//score +1
          fill(0);
    
          text(scoreBox, 25, 40, 200, 200);
          y_apple[i]=-100;//that object dispeared
        }
      }
    }
    
    void cherry() {
      //create cherries arraylist
      for (int i = 0; i < y_cherry.length; i++) {
        y_cherry[i] += 4;
        float x = i * 6;
        image(F_cherry, x, y_cherry[i]);
        //if cherry be catched
        if (x-30<ball1.x && ball1.x+ball1.r < x+30 && 
          y_cherry[i]-30<ball1.y && ball1.y+ball1.r<y_cherry[i]+30) {
          score= score+5;//score +5
    
            text(scoreBox, 25, 40, 200, 200);
          y_cherry[i]=-100;//that object dispeared
        }
      }
    }
    
    void egg() {
      //create bad egg arraylist
      for (int i = 0; i < y_egg.length; i++) {
        y_egg[i] += 8;//egg drop speed
        float x = i * 6;
        image(egg, x, y_egg[i]);
    
        //if egg be catched
        if (x-40 < ball1.x && ball1.x < x+40 && 
          y_egg[i]-40<ball1.y && ball1.y < y_egg[i]+40) {
          //   time1=0;
          y_egg[i]=-300;//that object dispeared
        }
      }
    }
    
    void display() {
      fill(255, 0, 0, 70);
      noStroke();
    
      fill(255);
      noStroke();
    }
    
    void reset() {
    }
    
    void keyPressed() {
      ball1.keyPressed();
    }
    
    
    // =============================
    
    
    
    class Ball {
    
      float x;
      float y;
      float speed;
      float r; //radius  // that's diameter !!!
    
    
    
      Ball(float tempX, float tempY, float tempR) {
        x = tempX;
        y = tempY;
        r = tempR;
        speed = 0;
      }
    
    
      void display() {
        noStroke();
        image(ballImage, x, y, r, r);
    
        // test 
        fill(255, 2, 2);
        rect (x, y, 5, 5);
        fill(0);
      }
      //key commands
      //ball flies off page in response to key command
    
      void keyPressed() {
        if (key == CODED) {
        }
    
    
        if (key == '5') {
          x = x+5;
          if (x > width - 25) {
            x = width - 25;
          }
    
          //println(but.read());
        } 
        else if (key == '4') {
          x = x-5;
          if (x < -6) {
            x = -6;
          }
    
          //println(but1.read());
        }
      } // method
    
    
      void tick() {
        seconds-=5;
      }
    }// class
    
    //
    
  • wow thank you so much for all your time and effort that works perfect! really appreciate it all chrisir :)

Sign In or Register to comment.