This week's book giveaway is in the Python forum.
We're giving away four copies of High Performance Python for Data Analytics and have Tiago Rodrigues Antao on-line!
See this thread for details.
Win a copy of High Performance Python for Data Analytics this week in the Python forum!
    Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Making my ball move and bounce of the borders /making my paddle move on keyevents

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
And that is what I am missing out on. Logic
 
Master Rancher
Posts: 4107
37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

 I am missing out on. Logic


Yes.  I assume you know how to use an if statement and how to change the value in an int variable.  That is all it takes.

If you need help with writing an if statement, please post a description of what you want to do with the statement.

There is sample code in the Runner class: it changes the value of dx or dy based on the values in some boolen variables.
 
Matthias Utrata
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Alright. My paddle works now fine, but I wanna try to make the intersections now, so when the ball hits the paddle or the bricks, it should bounce off. Does anyone of you have an idea?

 
Norm Radder
Master Rancher
Posts: 4107
37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

My paddle works now fine,

Where is the value of paddleXpos changed?

Look at the Pong.java program for examples of how a paddle changes the direction of the balls movement.
Also your code changes the direction of the ball when it hits a boundary.
 
Matthias Utrata
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
It is changed in my eventHandler that is in the start method
 
Matthias Utrata
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
There is all of my current code

 
Norm Radder
Master Rancher
Posts: 4107
37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
What is the purpose of lines 60 and 68?  Adding/subtracting 0 does nothing.

What happened with the code to change the paddle's X value in the same method that the ball's position was changed so that they are moved at the same time?  The OS's response to key oresses may be slower than the frequency that the run method is called.  Is that why the paddle jumps 10 pixels each time it is moved?
 
Matthias Utrata
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
60 is just saying that if the position of the paddle reaches the left end. It does not get changed. 68 is the same just to the right. I am uncertain how I implement the KeyEvent into the run Method
 
Bartender
Posts: 4272
160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
A way without using booleans is:

have a Field called paddleDeltaX, initially set to 0.
Then, in your run method, do:


And the KeyHandler will be:

and in the released handler:
   
 
Norm Radder
Master Rancher
Posts: 4107
37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Ok, I'm done now.  I leave the thread to Piet so he can give you the code you need.
Good luck.
 
Matthias Utrata
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Norm Radder wrote:Ok, I'm done now.  I leave the thread to Piet so he can give you the code you need.
Good luck.



Thank you. Take care!
 
Matthias Utrata
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Piet Souris wrote:A way without using booleans is:

have a Field called paddleDeltaX, initially set to 0.
Then, in your run method, do:


And the KeyHandler will be:

and in the released handler:
   



Thank you so much! It works so much better now. No delay or anything.
 
Piet Souris
Bartender
Posts: 4272
160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
You're welcome.

Do have a look at the code that Norm linked to. Always a good thing to see more than one solution.
 
Marshal
Posts: 71760
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Matthias Utrata
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hello everyone!

I am once again in need of your help. I luckily got my paddle bounce now working, but I am quite confused about how I could make it hit the target bricks. I create all the target bricks with two for loops, yet I am uncertain of how to use them with if statements (detecting a hit/removing). I will paste a link to my pastebin below.

https://pastebin.com/h1NtUkQV

 
Piet Souris
Bartender
Posts: 4272
160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
But that way I did not get any notification.

@Matthias,

collision between the ball and a brick is done in the same way as a collision between the ball and the paddle, albeit that you now have to check for collision at the left and right sides of the brick as well, changing possibly the x-direction. You must do that check for all the bricks (still) present. Did you put all your bricks in a List or Set? It would also be very handy to use a Brick class instead of just Rectangles.
 
Matthias Utrata
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Piet Souris wrote:But that way I did not get any notification.

@Matthias,

collision between the ball and a brick is done in the same way as a collision between the ball and the paddle, albeit that you now have to check for collision at the left and right sides of the brick as well, changing possibly the x-direction. You must do that check for all the bricks (still) present. Did you put all your bricks in a List or Set? It would also be very handy to use a Brick class instead of just Rectangles.



I create all of them in the run method. Is there a way, that I can create them in a constructor and then use them in the run method?
 
Matthias Utrata
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I now added a few if statements inside my for loops, where I create the targets, but the last row, does not get registered as a target. Only the second to last row does and I do not know why sadly.

@Piet
 
Matthias Utrata
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator


That is my problem code
 
Piet Souris
Bartender
Posts: 4272
160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Matthias Utrata wrote:I create all of them in the run method. Is there a way, that I can create them in a constructor and then use them in the run method?


You can define your Rectangles upfront and put then in a List<Rectangle>.
Then, in your run-method, you can have, say:

SideHit is an enum, with the values West, North, East, South and NO_HIT, and depending on the state returned you can determine what new direction the ball gets.

But as you see, you have meny variables denoting position, speed, color et cetera. As we mentioned in earlier topics, you can ease the code by having a Ball class, a Brick class and a Paddle class. More on that later, perhaps
 
Piet Souris
Bartender
Posts: 4272
160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
What is 'targets'?
 
Matthias Utrata
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Piet Souris wrote:What is 'targets'?



targets is just my variable name for a GraphicsContext object
 
Matthias Utrata
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Piet Souris wrote:

Matthias Utrata wrote:I create all of them in the run method. Is there a way, that I can create them in a constructor and then use them in the run method?


You can define your Rectangles upfront and put then in a List<Rectangle>.
Then, in your run-method, you can have, say:

SideHit is an enum, with the values West, North, East, South and NO_HIT, and depending on the state returned you can determine what new direction the ball gets.

But as you see, you have meny variables denoting position, speed, color et cetera. As we mentioned in earlier topics, you can ease the code by having a Ball class, a Brick class and a Paddle class. More on that later, perhaps



I am happy with having everything in one class. It is just better for me to understand my code.
 
Matthias Utrata
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Piet Souris wrote:

Matthias Utrata wrote:I create all of them in the run method. Is there a way, that I can create them in a constructor and then use them in the run method?


You can define your Rectangles upfront and put then in a List<Rectangle>.
Then, in your run-method, you can have, say:

SideHit is an enum, with the values West, North, East, South and NO_HIT, and depending on the state returned you can determine what new direction the ball gets.

But as you see, you have meny variables denoting position, speed, color et cetera. As we mentioned in earlier topics, you can ease the code by having a Ball class, a Brick class and a Paddle class. More on that later, perhaps



I am happy with having everything in one class. It is just better for me to understand my code. I have no idea what an enum is and no idea how that should help me with the hit detection. Could you maybe just take a look at my code in the pastebin link and give me an example with my code?
https://pastebin.com/h1NtUkQV
 
Piet Souris
Bartender
Posts: 4272
160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I've run your code from PasteBin, and it runs fine. so I will not comment it further.

You still have some work to do, like coming up with a new ball. And how to handle bricks when they have been hit.

Just one question: why do you use four equal GraphicsContexts?
 
Matthias Utrata
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
It is only just because of the names. Helps me a bit with it.

I still have the problems though with my knockback. The ball doesnt quite hit where it is supposed to. That is where I need help. And I do not know how to remove a GraphicsContext object. When you run it again, you notice how the ball ignores the last row completely. I do not know why it is like that.

@Piet
 
Piet Souris
Bartender
Posts: 4272
160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Your hit-calculations are not 100%. They seem to miss he underside of a brick. The tenth row is not missed, only a ball coming from below "goes trough" the underside as if that underside isn't there at all, and then bounces back from the upperside.

One way to see that in a clear way is to change the line:

into

and then'

et cetera. If you do that for countCol = 10, you see that it behaves exactly as all the other countCols.

So have a recheck of your hit-routines and see why these fail to recognize the undersides. What I also noticed a few times, is that when the ball, going northeast and hitting the leftupper corner of a brick, bounces off in the southwest diretion. Although that is explainable, it does make a very strange impression.

To make things a little easier to spot, give the ball an y-speed of two and an x-speed of 1.

Let me know your findings!
 
Matthias Utrata
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
There is one particular thing, that I now know is the mistake. I first did not know, that objects get drawn from the upper left corner first.

Now I am curious, if there is a way to get all points of the circle. I know now, how I wanna do the if statements and how I can make it work. I just need the information of getting every point of the circle.

@Piet
 
Piet Souris
Bartender
Posts: 4272
160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Matthias Utrata wrote:There is one particular thing, that I now know is the mistake. I first did not know, that objects get drawn from the upper left corner first.


That is incorrect: the order of drawing is the order that you specify. If the ball and a brick overlap, then if the ball is drawn first, the brick will be above the ball, and vice versa.

Matthias Utrata wrote:Now I am curious, if there is a way to get all points of the circle. I know now, how I wanna do the if statements and how I can make it work. I just need the information of getting every point of the circle


Why do you want to know that? Is that to prevent some overlap?
 
Matthias Utrata
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I meant that, when you draw anything. A rectangle or an oval. The shape starts to be drawn from the upper left.
 
Piet Souris
Bartender
Posts: 4272
160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Why is that important?
 
Matthias Utrata
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
So I can determine when the hit happens.
 
Matthias Utrata
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
So I wanna activate the run() method in addition after I click the mouse button, while the setGameStarted() Method is going. I think I am missing out on something though. The run method is just for the movement of the ball and the paddle. The setGameStarted() Method should be when the major thing happens. Like rectangles getting cleared after a hit with .clearRect(). I tried that before in my run method, but it always gets redrawn so I gotta adapt my code to make it work. I am gonna change my code in pastebin so you can take a look and hopefully help me out
 
Matthias Utrata
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
@Piet

https://pastebin.com/h1NtUkQV
 
Piet Souris
Bartender
Posts: 4272
160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Can you have  look at the code in PasteBin? The startup-screen doesn't go away after a mouseclick.
 
Piet Souris
Bartender
Posts: 4272
160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Matthias Utrata wrote:So I wanna activate the run() method in addition after I click the mouse button, while the setGameStarted() Method is going. I think I am missing out on something though. The run method is just for the movement of the ball and the paddle. The setGameStarted() Method should be when the major thing happens. Like rectangles getting cleared after a hit with .clearRect(). I tried that before in my run method, but it always gets redrawn so I gotta adapt my code to make it work. I am gonna change my code in pastebin so you can take a look and hopefully help me out


First of all: the names 'run' and 'setGameStarted' are unfortunate names, not clearly expressing what they do, and therefore being a bit confusing. Can you think of better names?

I am not sure whether I agree. You can update the ball and the paddle position in a run-method, but in what method do you do the collision check? If you do that in the 'run' method, and you detect a hit with Brick B, then in following display do you show brick B or not? So, my first thoughts are doing it all in the run-method, if that is fast enough (sofar it looks like it is fast nough).

Now for something completely different: the Brick class.
We have mentioned it before, but now is the time to implement it. What should a Brick-class contain?

an x and an y, a width and a height (or use a Rectangle instance for this)
a colour
an int 'hitsTaken'
an int hitsToDestroy
a boolean isDestroyed


and methods 'public boolean isHitByBall(Ball ball)' , 'private void processHit()' and 'draw(GraphicsContext g)'. If hitsTaken == hitsToDestroy set isDestroyed to true.

Put all the bicks you need in a List<Brick>. In the run-method you can draw all the bricks in your list, unless the isDestroyed is true.

You can create more than  one such List, for instance, to have an easy way to work with more than one 'level'. The creation of those lists can be done in a separate progam. But that is for later..

For now, concentrate on the Brick class. One optimalization: a ball can only hit one brick at the time. So if it hits the paddle or a brick, then there is no need to do these checks for other bricks as well.
 
Piet Souris
Bartender
Posts: 4272
160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hmm... second thoughts:

doing the updates of ball and paddle in an 'update' method seems very fine indeed. Then do the repainting, and next do a collision check, setting a brick that has been hit to isDestroyed and adaption of the ball direction if necessary. The advantage is that a hit brick will vanish only at the next display. So, you were right!
 
Matthias Utrata
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I have no idea how Lists work and no idea what I should do with a list
 
Matthias Utrata
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Would you be so kind and add some of the changes in here so I can kinda understand it? Neither do I quite understand why I now need to do a brick class.
 
CAUTION! Do not touch the blades on your neck propeller while they are active. Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic