It's a little program that render a bouncing ball, I did what they said but when I pressed run and nothing happened, then it ended with exit code:9.
Anyone have ideas what is going on here?Did I place any function in a wrong place?
#include <iostream>
#include <math.h>
usingnamespace std;
int width = 80;
int height = 80;
int ballX = 40;
int ballY = 40;
int forceX = 1;
int forceY = 1;
int distance(int x1, int y1, int x2, int y2){
int xDelta = x1 - x2;
int yDelta = y1 - y2;
return sqrt(xDelta * xDelta + yDelta * yDelta);
}
void updatePhysics(){
ballX += forceX;
ballY += forceY;
}
void drawFrame(){
for(int y = 0; y < height; y ++){
for(int x = 0; x < width; x ++){
//Render pixels.
if(x == ballX && y == ballY){
if(distance(ballX, ballY, x, y) < 6){ //If the pixel is inside the ellipse.
cout << '.'; //Render dots.
}elseif(distance(ballX, ballY, x, y) >= 6){ //Otherwise
cout << "x"; //render X marks.
}
}
cout << endl;
//I try to get the x and y output to see what's going on here
// but I just cannot figure it out.
//cout << "X = " << x << endl;
//cout << "Y = " << y << endl;
}
}
}
int main(){
while (true) {
updatePhysics();
drawFrame();
}
return 0;
}
It is running. It will run forever. It is showing you many many blank lines, so fast, over and over, and sometimes there is something else being shown as well, but it goes by so fast you aren't seeing it.
You can prove this to yourself by replacing line 35 with:
Yes! So I should delete this line, maybe change something else...
I think you really hit some point.
Still working on it, but thanks!
Will update here when I actually solve this problem.
Finally solved!
The main error goes with my updateFrame() function where I forgot to add the if() to change the direction of the ellipse. And yes, the line 35 where I put a stupid cout << endl; that couse a massive output of the RETURN.