why my code “Bouncing ball” doesn't work?

后端 未结 3 833
南方客
南方客 2020-12-11 14:15

I\'m trying to write a code a bout a bouncing ball, but i\'m stuck in how to make the ball bounced. The code seems correct, no wrong messages from eclipse and yet the ball d

3条回答
  •  Happy的楠姐
    2020-12-11 15:00

    you never declared that the g.fillOval was the ball here do this...

    g.drawOval((int)applex, (int)appley, (int)appleradius, appleradius);
      public double applex = ArandomX; //replace the random with values..
        public double appley = ArandomY; //same here
        public int appleradius = 10;
    

    this makes it move...

    a.px += a.vx;
            // check for boundaries
            if (a.px >= this.getWidth() || a.px <= 0) {
                // reverse vel and apply
                a.vx = -a.vx;
                a.px += -a.vx; // to prevent sticking
            }
    
            //a.vx += -gravity; // add this constant to accelerate things down
            a.py += a.vy;
            // check for boundaries
            if (a.py >= this.getHeight() || a.py <= 0) {
                // reverse vel and apply
                a.vy = -a.vy;
                a.py += a.vy; // to prevent sticking
            }`
    

    replace a.vx with your x or dx idk because your variables are unclear and yeah just replace a.vy with either y or dy and do the same for my px... it will work

提交回复
热议问题