Could I get some help with this programming?
Author |
Message |
omfgkevin
|
Posted: Mon May 28, 2012 10:11 am Post subject: 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.
Quote: 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;
while (true)
{
for (; x <= 635 ; x += 36)
c.drawOval (0 + x, 0 + x, 50, 50);
for (; x >= 0 ; y += 96)
c.drawOval (0, 0 + x, 50, 50);
}
}
}
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) |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
jr5000pwp
|
Posted: Mon May 28, 2012 1:53 pm Post subject: 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
|
|
|
|
|
|
![](images/spacer.gif) |
mirhagk
|
Posted: Tue May 29, 2012 7:25 am Post subject: 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. |
|
|
|
|
![](images/spacer.gif) |
omfgkevin
|
Posted: Tue May 29, 2012 10:27 am Post subject: 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 [ expected instead of this token
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, xVel, yVel;
[b] while (true)[/b]
{
for (; x <= 635 ; x += 36)
c.drawOval (0 + x, 0 + x, 50, 50);
for (; x >= 0 ; y += 96)
c.drawOval (0, 0 + x, 50, 50);
If x < 0 [b]or [/b]x > screenWidth [b]then[/b]
x *= -1
[b]If [/b]y < 0 [b]or [/b]y > screenHeight [b]then[/b]
y *= -1
}
}
[b]}[/b] |
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 <= 1000000 ; i++)
c.setColor (Color.white);
c.fillOval (x, y, 50, 50);
x += x2;
y += y2;
if (y > 450)
y2 = -1;
else if (y == 0)
y2 = 1;
if (x > 635)
x2 = -1;
else if (x == 00)
x2 = 1;
}
}
} |
|
|
|
|
|
![](images/spacer.gif) |
jr5000pwp
|
Posted: Tue May 29, 2012 11:10 am Post subject: 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 <= 0)
The x2=-1 is equivalent to what I was saying for inverting the x and y, except with mine, you only have to declare the velocities once. With yours you have to replace a bunch of 1s if you wanted to change it to 2.
The code that I posted is psuedocode(Although close to turing code). It is essentially a universally understandable code that can be easily translated into any coding language. That way even if you don't know a language, you can still communicate the code to someone who knows the language. |
|
|
|
|
![](images/spacer.gif) |
mirhagk
|
Posted: Tue May 29, 2012 11:36 am Post subject: RE:Could I get some help with this programming? |
|
|
I believe the equivalent java code is:
Java: |
if (x < 0 || x > screenWidth)
x2 *= -1;
if (y < 0 || y > screenHeight)
y2 *= -1 ;
|
Although I could be slightly off (I never really got in depth into Java) |
|
|
|
|
![](images/spacer.gif) |
omfgkevin
|
Posted: Wed May 30, 2012 10:11 am Post subject: RE:Could I get some help with this programming? |
|
|
I see. Thanks for the help! |
|
|
|
|
![](images/spacer.gif) |
|
|