Hello all I would like to show you guys my first ever game in love!
Please tell me how I can improve my code because I think it looks ugly.
Project pong! (opinions on code needed!)
-
- Prole
- Posts: 46
- Joined: Fri May 31, 2013 9:42 pm
Project pong! (opinions on code needed!)
- Attachments
-
- pong.love
- v1
- (929 Bytes) Downloaded 111 times
Re: Project pong! (opinions on code needed!)
Hi,
first of all: congratulation for you first game.
Here are my suggestion for better code:
Edit: One last suggestion:
first of all: congratulation for you first game.
Here are my suggestion for better code:
- Try to avoid numbers in your code. By that, of course, I don't mean numbers in general, but those, that might change, when you change your code. For example, in your code the condition for one player to get points is
This is better:
Code: Select all
if ball.x > 799 then
because you saved the window width in the variable w. Now, if you change screen resolution, it will still work correctly. The idea is to remove redundancy.Code: Select all
if ball.x > w-1
- Use dt consistently, that means either everywhere or nowhere. Better is everywhere. For example this:
Should be
Code: Select all
if love.keyboard.isDown("s") then p1.y = p1.y + p1.speed end
And the p1.speed should be adapted (you need a larger value).Code: Select all
if love.keyboard.isDown("s") then p1.y = p1.y + p1.speed*dt end
- For the ball movement, you should use velocity. Right now you store the balls speed in one variable and the balls direction in four booleans leftDown, leftUp, rightDown, and rightUp. This is again redundant and you could construct contradictions, if two or more of the booleans are true. So better introduce two numbers to store the x- and y-velocity. The updating of the ball position is then
No if/then statement is needed then. To reflect the ball, say on the top you go
Code: Select all
ball.x = ball.x + ball.vx * dt ball.y = ball.y + ball.vy * dt
or a bit more robust:Code: Select all
ball.vy = -ball.vy
Code: Select all
ball.vy = math.abs(ball.vy)
- For the paddles, better use a rectangle
Code: Select all
love.graphics.rectangle('fill',p1.x,p1.y,p1.w,p1.h)
- And last, the collision check with the paddle
It does not check what radius the ball has (10 is hard coded). You should add a variable for the balls radius.
Code: Select all
if ball.x < p1.x + 10 and ball.y > p1.y and ball.x > p1.x + p1.w and ball.y < p1.y + p1.h then
Edit: One last suggestion:
- If you are planing to make a larger game, you should put some operations/calculations into functions. That makes the code more readable.
Check out my blog on gamedev
-
- Prole
- Posts: 46
- Joined: Fri May 31, 2013 9:42 pm
Re: Project pong! (opinions on code needed!)
Thank you for the very good tips! I will keep them in mind with my next project. Thank you!micha wrote:Hi,
first of all: congratulation for you first game.
Here are my suggestion for better code:Hope that helps.
- Try to avoid numbers in your code. By that, of course, I don't mean numbers in general, but those, that might change, when you change your code. For example, in your code the condition for one player to get points is
This is better:Code: Select all
if ball.x > 799 then
because you saved the window width in the variable w. Now, if you change screen resolution, it will still work correctly. The idea is to remove redundancy.Code: Select all
if ball.x > w-1
- Use dt consistently, that means either everywhere or nowhere. Better is everywhere. For example this:
Should beCode: Select all
if love.keyboard.isDown("s") then p1.y = p1.y + p1.speed end
And the p1.speed should be adapted (you need a larger value).Code: Select all
if love.keyboard.isDown("s") then p1.y = p1.y + p1.speed*dt end
- For the ball movement, you should use velocity. Right now you store the balls speed in one variable and the balls direction in four booleans leftDown, leftUp, rightDown, and rightUp. This is again redundant and you could construct contradictions, if two or more of the booleans are true. So better introduce two numbers to store the x- and y-velocity. The updating of the ball position is then
No if/then statement is needed then. To reflect the ball, say on the top you goCode: Select all
ball.x = ball.x + ball.vx * dt ball.y = ball.y + ball.vy * dt
or a bit more robust:Code: Select all
ball.vy = -ball.vy
Code: Select all
ball.vy = math.abs(ball.vy)
- For the paddles, better use a rectangle
Code: Select all
love.graphics.rectangle('fill',p1.x,p1.y,p1.w,p1.h)
- And last, the collision check with the paddle
It does not check what radius the ball has (10 is hard coded). You should add a variable for the balls radius.Code: Select all
if ball.x < p1.x + 10 and ball.y > p1.y and ball.x > p1.x + p1.w and ball.y < p1.y + p1.h then
Edit: One last suggestion:
- If you are planing to make a larger game, you should put some operations/calculations into functions. That makes the code more readable.
Who is online
Users browsing this forum: No registered users and 8 guests