Need to record a collision between two objects.
in
Programming Questions
•
1 year ago
Hello, am having 2 problems with this code I am trying to write. I need to make the black ball continue to rise on its own after keyPressed. At the moment I need to hold down a key for it to continue to rise which is wrong however I dont know how to fix it.
The second problem is I have no idea how to make the code register the 2 balls colliding, and then stopping the program and making the screen go blank. If the 2 balls dont collide then the black ball can go off the screen and then the program also stops.
Thanks in advance for any help.
Here is my code so far.
- int x = 0;
- int y = 0;
- int speedX = 1;
- int speedY = 1;
- int goUp = 1;
- void setup() {
- size(200,200);
- smooth();
- }
- void draw() {
- background(255);
- stroke(0);
- line(0,61,width,61);
- move();
- bounce();
- display();
- blackEllipse();
- keyPressed();
- }
- // A function to move the red ball.
- void move() {
- // Change the x and y location by speed.
- x = x + speedX;
- y = y + speedY;
- }
- // Bounce the red ball.
- void bounce() {
- // If the ball hits an edge, reverse the speed.
- if ((x > width-40) || (x < 0)) {
- speedX = speedX * - 1;
- }
- if ((y > 21) || (y < 0)) {
- speedY = speedY * - 1;
- }
- }
- // A function to display the ball
- void display() {
- noStroke();
- fill(255,0,0);
- ellipse(20+x,20+y,40,40);
- }
- // A function to display the black ball.
- void blackEllipse() {
- fill(0);
- ellipseMode(CENTER);
- ellipse(width/2,height-20+goUp,40,40);
- }
- void keyPressed() {
- if (keyPressed) {
- goUp = goUp-1;
- }
- }
1