java - How do I add more objects to the program? -
i have started taking java classes new , got task "bouncing balls". have make user input how many balls he/she wants see in screen. have tried doing loops sure have done wrong because keep seeing 1 ball. give me pointers/hints/ or indicate problem? thank :)
program:
public class bouncingball { public static void main(string[] args) { int n = stdin.readint(); for(int i=0;i<n;i++){ // set scale of coordinate system stddraw.setxscale(-1.0, 1.0); stddraw.setyscale(-1.0, 1.0); // initial values double rx = math.random(); double ry = math.random(); // position double vx = 0.015, vy = 0.023; // velocity double radius = 0.05; // radius // main animation loop while (true) { // bounce off wall according law of elastic collision // (int = 0; < n; i++) { if (math.abs(rx + vx) > 1.0 - radius) vx = -vx; if (math.abs(ry + vy) > 1.0 - radius) vy = -vy; // update position // (int = 0; < n; i++) { rx = rx + vx; ry = ry + vy; // clear background stddraw.clear(stddraw.gray); // draw ball on screen // (int = 0; < n; i++) { stddraw.setpencolor(stddraw.black); stddraw.filledcircle(rx, ry, radius); // display , pause 20 ms // (int = 0; < n; i++) { stddraw.show(20); } } } }
i assuming want n
number of balls here. problem loop in line 3 spanning entire program, including animation loop. so, instead of spawning n balls, you're running game n-times each after other.
Comments
Post a Comment