Breakout Help (bouncing ball)
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 30
- Joined: Tue Feb 12, 2013 7:45 pm
Breakout Help (bouncing ball)
Hello everyone. I am trying to write a traditional breakout clone. I have setup the bricks and the paddle, but can not figure out how to implement the ball. The ball has to bounce off the paddle and hit the brick, when that happens the brick has to disappear. Since I am new to love, I would really like some help from experienced lovers. Thanks a lot!
-SeducingOrange (I even made a lewd name in spirit of LOVE )
EDIT:
Oops. Meant to upload a love file excuse the sloppy coding.
-SeducingOrange (I even made a lewd name in spirit of LOVE )
EDIT:
Oops. Meant to upload a love file excuse the sloppy coding.
- Attachments
-
- crappy.love
- (20.5 KiB) Downloaded 309 times
Last edited by SeducingOrange on Wed Feb 13, 2013 12:27 am, edited 1 time in total.
Re: Breakout Help (bouncing ball)
It is easiest for us to help you, if you upload a .love file of what you got already.
Here are a few steps I suggest:
1) Implement the ball, without any collision on the blocks or paddle, only on the boundary of the playing field. By that you will learn how to use velocity.
2) Next you can implement the collision with the paddle. This is similar to the boundary, only with an extra if-condition. If you want to do it super-cool you will also make the angle of reflection depending on the position of the ball on the paddle. In many breakout games you can control the ball with this mechanism. But this is not important in the beginning.
3) Implement collision with the blocks. For that I suggest you look into some tutorials on collision detection (e.g. in platformer games).
Here are a few steps I suggest:
1) Implement the ball, without any collision on the blocks or paddle, only on the boundary of the playing field. By that you will learn how to use velocity.
2) Next you can implement the collision with the paddle. This is similar to the boundary, only with an extra if-condition. If you want to do it super-cool you will also make the angle of reflection depending on the position of the ball on the paddle. In many breakout games you can control the ball with this mechanism. But this is not important in the beginning.
3) Implement collision with the blocks. For that I suggest you look into some tutorials on collision detection (e.g. in platformer games).
Check out my blog on gamedev
Re: Breakout Help (bouncing ball)
Alright. I had a look into your code.
I give you a hint, how to get started. Maybe you can work out the details on your own:
You initialize the ball with
Instead of having one "speed"-variable, you need two variables, namely for the to coordinate directions. Lets call them vx and vy (for velocity x and velocity y)
Also, for now, lets say that the initial speed is set in the beginning (I picked arbitrary numbers here). We'd get
Also for now, I leave away the segments property, as it is only a matter of drawing. It would only make sense if you have multiple balls and each one had a different number of segments.
Now in the update-function, you have to move the ball according to velocity
And don't forget to draw the ball in the draw-function
Now you should get a ball moving across the playing field. However without any collision or interaction.
Try this next. What do you have to add, to make the ball bounce of the walls?
Here is an unrelated suggestion: Your code contains a lot of repitions. This is usually a sign that you can make the code shorter by programming in a more clever way. For example, you have a lot of rectangle-drawing function calls. Have a look into what tables are in lua and try to implement this drawing with a for loop.
I give you a hint, how to get started. Maybe you can work out the details on your own:
You initialize the ball with
Code: Select all
ball = {x = 50, y = 50, radius = 50, segments = 500, speed = level * 200, speed = math.random (360)}
Also, for now, lets say that the initial speed is set in the beginning (I picked arbitrary numbers here). We'd get
Code: Select all
ball = {x = 50, y = 50, vx = 10, vy = 5, radius = 50}
Now in the update-function, you have to move the ball according to velocity
Code: Select all
ball.x = ball.x + ball.vx*dt
ball.y = ball.y + ball.vy*dt
Code: Select all
love.graphics.circle( "fill", ball.x, ball.y, ball.radius, 20)
Try this next. What do you have to add, to make the ball bounce of the walls?
Here is an unrelated suggestion: Your code contains a lot of repitions. This is usually a sign that you can make the code shorter by programming in a more clever way. For example, you have a lot of rectangle-drawing function calls. Have a look into what tables are in lua and try to implement this drawing with a for loop.
Check out my blog on gamedev
-
- Prole
- Posts: 30
- Joined: Tue Feb 12, 2013 7:45 pm
Re: Breakout Help (bouncing ball)
Thanks for the hint. I think I got it now! I really appreciate the help you gave me! I can finally finish this project.
-
- Prole
- Posts: 30
- Joined: Tue Feb 12, 2013 7:45 pm
Re: Breakout Help (bouncing ball)
I decided to come back to this after a long time of not working on it (I started a bunch of different projects and learned C++). I got collision with the paddle and walls working bot not with the bricks. Can you help me out with that? I attached a love file for ya.
Thanks for all the help by the way
Thanks for all the help by the way
- Attachments
-
- game.love
- (2.37 KiB) Downloaded 237 times
Re: Breakout Help (bouncing ball)
I hate to have to tell you this, but you need to make classes. In other words, rewrite most of your code. Then it's exactly the same as having a bunch of paddles that you can't control.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
-
- Prole
- Posts: 30
- Joined: Tue Feb 12, 2013 7:45 pm
Re: Breakout Help (bouncing ball)
Oh I see. Do you have any good material for writing classes in lua? I thought it didn't support classes.
- substitute541
- Party member
- Posts: 484
- Joined: Fri Aug 24, 2012 9:04 am
- Location: Southern Leyte, Visayas, Philippines
- Contact:
Re: Breakout Help (bouncing ball)
Since you are a beginner, you can just use the class libraries out there. Here's one that I mainly use.
Note: Sexualpunsarethestandard here.
Edit: If you want to learn more, Programming in Lua has a chapter for simulating OOP.
Note: Sexualpunsarethestandard here.
Edit: If you want to learn more, Programming in Lua has a chapter for simulating OOP.
Currently designing themes for WordPress.
Sometimes lurks around the forum.
Sometimes lurks around the forum.
-
- Prole
- Posts: 30
- Joined: Tue Feb 12, 2013 7:45 pm
Re: Breakout Help (bouncing ball)
Lol. I figured out the sexual puns when I downloaded the Live Coding Kit (LICK). That's not as bad as the cock one though. Thanks for references! And what's with the obey in everyone's avatar?
- substitute541
- Party member
- Posts: 484
- Joined: Fri Aug 24, 2012 9:04 am
- Location: Southern Leyte, Visayas, Philippines
- Contact:
Re: Breakout Help (bouncing ball)
It's also a standard here.SeducingOrange wrote:Lol. I figured out the sexual puns when I downloaded the Live Coding Kit (LICK). That's not as bad as the cock one though. Thanks for references! And what's with the obey in everyone's avatar?
Currently designing themes for WordPress.
Sometimes lurks around the forum.
Sometimes lurks around the forum.
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 2 guests