
-----------------------------------
omfgkevin
Mon May 28, 2012 10:11 am

Could I get some help with this programming?
-----------------------------------
I am using ReadyToProgram.


Well, I've been running through assignments but I'm not sure how to do this one?

5.2 Write a program which uses Console graphics and loops to
have a ball which moves diagonally on the screen. When it
hits the edge of the screen, it bounces in the opposite
direction.

I do not quite understand how to make the ball *go around* the place by bouncing off the walls, and also, how would I delay it so it looks like it is moving? (Currently when I use the code it shows the all the circles leading to the bottom.)
Here is my code so far.
import java.awt.*;
import hsa.Console;


public class Program_5_2
{

    static Console c = new Console ();
    public static void main (String args

It creates a circle from the top left corner all the way to the bottom right, but stops there. Please and thanks if I could get some help! (I do not quite understand this concept)

-----------------------------------
jr5000pwp
Mon May 28, 2012 1:53 pm

Re: Could I get some help with this programming?
-----------------------------------
Your two for loops don't appear to be doing what you want them to do. Once the x is at 635 it won't go back to the left, and y(not being drawn with) will continually increase.

Velocity is what you are probably going to want to use here. With velocities you can update an objects position every frame, and you can invert it(multiply by -1) to reverse its direction.

Updating an x and y by velocity should be easy to figure out, just have 4 variables x, y, xVel and yVel, and update your x and y by xVel and yVel every frame.

The second part is to make it bounce around, this will involve a little thing called collision detection. In this case it is very simple, but if you go on to making games, it will become more complex. In this case it simply is:

[code]
If x < 0 or x > screenWidth then
     x*=-1
If y < 0 or y > screenHeight then
     y*=-1
[/code]

-----------------------------------
mirhagk
Tue May 29, 2012 7:25 am

RE:Could I get some help with this programming?
-----------------------------------
As jr5000wp mentions whenever you have a moving object you should have variables for the position as well as the velocity.

With updating the position and velocity, and the simple collision code jr5000wp has provided (which just checks if it's about to go offscreen, and if so bounces it back in the opposite direction) you will actually get a pretty nice looking bouncing ball that you can place anywhere you want, and with whatever velocity you want and it will bounce around on the screen.

-----------------------------------
omfgkevin
Tue May 29, 2012 10:27 am

Re: Could I get some help with this programming?
-----------------------------------
I'm sorry, but I still dont understand completely. (Velocities?) and also the code does not seem to work with my program while placed (asking for this)

BOLDED ARE ERRORS 
It says 
        while (true)       
 {


            for (; x = 0 ; y += 96)
                c.drawOval (0, 0 + x, 50, 50);

            If x < 0 or x > screenWidth then
                x *= -1
                If y < 0 or y > screenHeight then
                y *= -1



        }
    }
}[/code]

and if it changes anything, Im a grade 10 ICS student.  I'm really confused right now, sorry.


EDIT:

Okay right now my code works fine, but I used a different method. It goes off the screen sometimes a little bit, but thats okay.

[code]import java.awt.*;
import hsa.Console;


public class Program_5_2
{

    static Console c = new Console ();
    public static void main (String args[])
    {


        int x = 0, y = 0;
        int x2 = 1;
        int y2 = 1;
        while (true)
        {
            c.setColor (Color.red);
            c.fillOval (x, y, 50, 50);
            for (int i = 0 ; i  450)
                y2 = -1;

            else if (y == 0)
                y2 = 1;

            if (x > 635)
                x2 = -1;
            else if (x == 00)
                x2 = 1;




        }
    }
}[/code]

-----------------------------------
jr5000pwp
Tue May 29, 2012 11:10 am

Re: Could I get some help with this programming?
-----------------------------------
Your x2 and y2 are currently your velocities, that represents how many pixels your character moves per frame depending. If you decided to change your x and y velocities(x2, y2), you would run into problems with the left and top sides of the screen because you aren't checking if it is past the top or left, just if it is on the top or left. (Replace == 0 with 