• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with While loops  RSS feed

 
David Smithy
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have only been learning code for 4 weeks and I have been trying to make a simple ricochet game but I'm really stuck and have nobody else to talk to about it as my Tutor just talks to me like I'm incredibly stupid!
I'm guessing I've missed something simple but I am learning haha please point it out!

Here is my code:








 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Please explain what exactly the problem is. Does your code compile? Does it run? Does it do what you expect it to do? If not, then what does it actually do, and how does this differ from what you expected?
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I have added code tags, which you should always use, to your post (and removed excess blank lines and had to shorten the long comments), and you can see how much better it looks

Do you really write bouncy ball games after only four weeks' learning?
What is the problem? You said something about while loops. I can't see anything wrong with the loop. What goes wrong?
I don't like your way to choose the colours of balls, however. There is a much easier way to do it with an array of colours.
 
David Smithy
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the welcome, I have not come across arrays yet, I did think the if's was a bit messy, sorry about that

I did not know how to style the code, so thanks very much for making it look nicer

It does compile with no syntax errors, however when it runs the ball moves to the right hand side of the window, changes color many times and then moves left a small amount and stops completely.

It should trigger this.isTouchingLeftOrRight but it does not seem to.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many balls have you got in the game?
When are they supposed to change colour?
How are you working out whether the balls touch the edges? The bit about 20px seems strange. If it means diameter, use diameter.
I think you need more classes. I think you need a Ball class, which maintains its colour, location and movement. When you touch something, you change the movement. It may be easy to work out when you touch the margins of the court, but a lot more awkward if two balls bounce off each other. I do remember having to work out bouncing when I did A‑level physics, but that was a very long time ago.

You may have the problem that you are moving the ball left when it touches the right margin, but not changing its direction to move left. So it keeps trying to go right.
 
David Smithy
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have 1 ball and I want it to change color when the ball hits the edge.

Thanks for pointing out the (+20) I have changed all instances of that to



After changing this it still heads to the right wall, changes color a lot, beeps a lot and then moves left. It's like it is not doing the if's

I have a circle class which maintains it's diameter, colour, x and y position. However the code for it to change color needs to be in the ball court class.
I'm not sure how to maintain movement, I thought the while kept moving the ball with this bit of code



Thanks for talking to me!
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rather than subtracting xInc and yInc when the isTouchingLeftOrRight() returns true, shouldn't you be changing the sign of the xInc value so the lines at the top of the while loop that are supposed to move the ball, move it in the correct direction.
The same logic applies when the ball is detected in a corner and/or at the top/bottom edge.
 
David Smithy
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That sounds like a good idea tony, I just have no idea how to implement it! I will try and do that when I try again. Having a break as I've been at it all
Day!

Thanks for your suggestion.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok lets just look at xInc and moving left and right at the moment. When you enter the while loop xInc contains a positive value and so the call to setXPos(this.ball.getXPos() + xInc) moves the ball to the right. Note: Each time the loop executes (whilst xinc is positive) the ball will move an increment to the right.
This keeps on happening until isTouchingLeftOrRight() returns true at which point we need to stop moving the ball to the right and start moving the ball to the left. So if we now change the sign of xInc what will happen for the next n iterations of the loop?
 
David Smithy
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you say sign, I'm not sure what you mean?


Does this not change the sign of xInt?

I'm sorry, I'm just new to this!

I appreciate your help
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is something not very nice about a line.of.code.with.that.many.dots.in. It makes me suspect you are doing things the complicated way. And you should avoid complicated things as much as possible. Your Ball class would have a move() method, so you don't mess around with getXPosition or anything like that. You can simply say ball.move();
I still think you need a Ball class. The Ball class has direction of movement fields (easiest is probably moveX and moveY fields). You would have changeDirection methods, but the automatic response to hitting something solid is to bounce.
No, the line you showed doesn't change the sign of xInc. It moves it left by xInc, but no more than that. You would want something on the lines of xInc = -xInc;
I notice you are moving the balls every 30ms. If you use much more than 30ms delay, the display will flicker unpleasantly. If you have much less than 30ms, there is a risk that the display will not refresh quickly enough to show the motion. I presume you have got xInc and yInc small enough that you can actually watch the balls' motion.
 
David Smithy
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,



here is my code for isTouchingLeftOrRight()


When I execute it with ricochet(1, 0, 20)
The ball moves to the right hand side edge of the window, hits it, changes color too fast to see and beeps too many times to count. Its as if its looping the if?

I tried many values for the delay and 30ms is fine. I still cannot figure out why it is not working!
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have some poor style in that predicate method. You can see what better style would look like in this style guide §10.5.2.

It looks as though you were touching the right edge and not moving away from it.
I still think you need a Ball class separate from the class representing the Court. Each time you run the program (every 30ms), you call a move method on the ball. I think you are going to have to delete lots of the current class, I am afraid.

The ball needs to “know” the size of the court. I have thought of two ways.
  • 1: You might pass a court instance to the ball instance; then the ball can tell the size of the court and whether it has reached the edge of the court.
  • 2: You call getMargin methods on the ball object, and the court can tell whether they are greater than its bounds.
  • In both cases you would call a bounceHorizontally or bounceVertically method on the ball object.

    I am not certain, but I think option 2 is more elegant. There are bound to be more such options.
    If you make the court class extend a GUI class (e.g. JPanel), it has getWidth and similar methods, which will allow for any size of court.
    There is an alternative to Thread#sleep: a Swing Timer.
     
    Tony Docherty
    Bartender
    Posts: 3271
    82
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    When I execute it with ricochet(1, 0, 20)
    The ball moves to the right hand side edge of the window, hits it, changes color too fast to see and beeps too many times to count. Its as if its looping the if?

    I suggest you comment out the code for handling corners and top/bottom edges and concentrate on getting the left and right motion working.
    The reason I suggest you comment out the code is to make sure it isn't being accidentally executed (which I suspect it is if you run the method with a yInc value of 0).
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!