Breakout Help (bouncing ball)

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
SeducingOrange
Prole
Posts: 30
Joined: Tue Feb 12, 2013 7:45 pm

Breakout Help (bouncing ball)

Post by SeducingOrange »

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 :awesome:)

EDIT:
Oops. Meant to upload a love file :oops: excuse the sloppy coding.
Attachments
crappy.love
(20.5 KiB) Downloaded 273 times
Last edited by SeducingOrange on Wed Feb 13, 2013 12:27 am, edited 1 time in total.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Breakout Help (bouncing ball)

Post by micha »

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).
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Breakout Help (bouncing ball)

Post by micha »

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

Code: Select all

ball = {x = 50, y = 50, radius = 50, segments = 500, speed = level * 200, speed = math.random (360)}
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

Code: Select all

ball = {x = 50, y = 50, vx = 10, vy = 5, radius = 50}
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

Code: Select all

ball.x = ball.x + ball.vx*dt
ball.y = ball.y + ball.vy*dt
And don't forget to draw the ball in the draw-function

Code: Select all

love.graphics.circle( "fill", ball.x, ball.y, ball.radius, 20)
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.
SeducingOrange
Prole
Posts: 30
Joined: Tue Feb 12, 2013 7:45 pm

Re: Breakout Help (bouncing ball)

Post by SeducingOrange »

Thanks for the hint. I think I got it now! I really appreciate the help you gave me! I can finally finish this project. :awesome: :awesome: :awesome:
SeducingOrange
Prole
Posts: 30
Joined: Tue Feb 12, 2013 7:45 pm

Re: Breakout Help (bouncing ball)

Post by SeducingOrange »

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
Attachments
game.love
(2.37 KiB) Downloaded 215 times
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Breakout Help (bouncing ball)

Post by davisdude »

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
SeducingOrange
Prole
Posts: 30
Joined: Tue Feb 12, 2013 7:45 pm

Re: Breakout Help (bouncing ball)

Post by SeducingOrange »

Oh I see. Do you have any good material for writing classes in lua? I thought it didn't support classes.
User 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)

Post by substitute541 »

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.
Currently designing themes for WordPress.

Sometimes lurks around the forum.
SeducingOrange
Prole
Posts: 30
Joined: Tue Feb 12, 2013 7:45 pm

Re: Breakout Help (bouncing ball)

Post by SeducingOrange »

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?
User 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)

Post by substitute541 »

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?
It's also a standard here.
Currently designing themes for WordPress.

Sometimes lurks around the forum.
Post Reply

Who is online

Users browsing this forum: No registered users and 84 guests