Setting Wall Boundaries on Moving Ball
in
Programming Questions
•
1 year ago
Heya,
I'm having trouble setting boundaries for a moving red ball inside the top area of my program. Which is above the black line.
I know that I need a condition which changes the direction of the ball once the radius of the ball goes near the edges of the program, but I'm not exactly sure how to implement it.
After that I need to add a black ball, on the bottom middle which needs to be "shot" straight up after any key is pressed and if it hits the red ball then text "You Win!" is displayed, and if it misses then text "Game Over" is displayed; hence I need to code to know when there's a collision or not. For that part I have a vague idea of using the dist function to compare the sum of the two balls radiuses, and if it's less than the sum then it's a collision(display win text), otherwise not(display game over text).
But I'm trying to solve one problem at a time, so boundaries first. :)
So far I've got the red ball moving freely, and a line.
- float dia, centerX, centerY, speedX, speedY;
- PFont font;
- void setup() {
- size(200, 200);
- background(255);
- smooth();
- font = createFont("Arial",16); // to fix text delay later when text is added in
- textFont(font);
- dia = 40;
- centerX = random(dia/2, width-dia*2);
- centerY = random(dia/2, height-dia*2);
- speedX = random(1, 3);
- speedY = random(1, 3);
- // condition needs to go here
- }
- void draw() {
- background(255);
- stroke(0);
- line(0,60,200,60);
- fill(255, 0, 0);
- noStroke();
- ellipse(centerX, centerY, dia, dia);
- centerX+=speedX;
- centerY+=speedY;
- }
1