Hi,
Here is my first game, a Pong-like !
I wanted to learn and try OOP with lua (and try löve btw)
I need feedback on the code, please. I would like to make a tutorial (both a getting started with löve2d and OOP with lua) out of this game.
_____
TODO:
- Add an AI
- Add comments to the code
- Add a GUI
- Textures (graphics) + some effects
- Add more gameplay features
oopong [WIP]
oopong [WIP]
- Attachments
-
- oopong.love
- oopong love file
- (2.44 KiB) Downloaded 185 times
-
- src.zip
- oopong src
- (2.55 KiB) Downloaded 153 times
Re: oopong [WIP]
I can offer a little code review if you want. Please don't interpret this as me stamping on yourself, I'm intending it to be helpful and constructive criticism. I'll try to explain each piece of advice.
1. The Ball class:
You don't need to put all of the functions of the ball within the objects created by Ball.new. This method of OOP is the Closure method, which I had a lovely conversation about it on reddit with another helpful soul that convinced me (via profiling and benchmarks) that the memory requirements of closures in OOP are staggering. Since there's only one ball in pong, it doesn't matter, but as this is a learning exercise, the better way to do it is to use lua's Metatables feature.
The chapter on Classes from Programming In Lua will show you how to do it the metatable way: http://www.lua.org/pil/16.1.html
The same goes for the Racquet class. Your classes would probably look more like so after changing:
2. Use of global variables
My only gripe here is the underscore in the name. Since they're global, they're also public, and underscored variables kinda indicates that the variable's intent is to be private. If you use underscored variables as members of tables (like self._x and self._y), that's kosher.
Personally, I use the global table only for classes, major functions, and namespaces/modules, but it's perfectly fine stuffing it full of variables on smaller games. An alternative to using the global table is to use the local keyword at the file scope (meaning: outside any function, and at the top of the file).
3. Pythonic Indenting of function calls
It's a bit unreadable when you write things like this, and think even the python people shun it:
This way is usually more legible:
Also, as a matter of convention, width goes before height, in the same way that X goes before Y.
Okay, that's all I have right now. I hope this has been friendly and helpful. When you have a full working game, I'll give it another look. Good luck!
1. The Ball class:
You don't need to put all of the functions of the ball within the objects created by Ball.new. This method of OOP is the Closure method, which I had a lovely conversation about it on reddit with another helpful soul that convinced me (via profiling and benchmarks) that the memory requirements of closures in OOP are staggering. Since there's only one ball in pong, it doesn't matter, but as this is a learning exercise, the better way to do it is to use lua's Metatables feature.
The chapter on Classes from Programming In Lua will show you how to do it the metatable way: http://www.lua.org/pil/16.1.html
The same goes for the Racquet class. Your classes would probably look more like so after changing:
Code: Select all
Ball = {}
Ball.__index = Ball
function Ball.new( parameters )
local self = {}
self._x = 0
self._y = 0
self._radius = 10
setmetatable(self, Ball)
return self
end
function Ball:draw()
love.graphics.circle("fill", self._x, self._y, self._radius)
end
My only gripe here is the underscore in the name. Since they're global, they're also public, and underscored variables kinda indicates that the variable's intent is to be private. If you use underscored variables as members of tables (like self._x and self._y), that's kosher.
Personally, I use the global table only for classes, major functions, and namespaces/modules, but it's perfectly fine stuffing it full of variables on smaller games. An alternative to using the global table is to use the local keyword at the file scope (meaning: outside any function, and at the top of the file).
3. Pythonic Indenting of function calls
It's a bit unreadable when you write things like this, and think even the python people shun it:
Code: Select all
if ballIntersect(
_x,
_y,
_radius,
0, love.graphics.getHeight()+10,
love.graphics.getWidth(), 10
) == true then
return false
end
Code: Select all
if ballIntersect(_x, _y, _radius, height, width) then
return false
end
Okay, that's all I have right now. I hope this has been friendly and helpful. When you have a full working game, I'll give it another look. Good luck!
- Roland_Yonaba
- Inner party member
- Posts: 1563
- Joined: Tue Jun 21, 2011 6:08 pm
- Location: Ouagadougou (Burkina Faso)
- Contact:
Re: oopong [WIP]
First if all, resolution is odd. Change it. And set a key to close the app.
Now, looking at your code, it looks like OO, but I won't call it like that.Plus actually, calling Ball.new with add all of its methods in the global env.
I dunno what experts would think, though, but I suggest having this base code for faking a class:
Written from scratch, hope there's no err though.
By the way, there's good documentation on OO on chapter 16 of PIL
Inny ninja'ed me!
Now, looking at your code, it looks like OO, but I won't call it like that.Plus actually, calling Ball.new with add all of its methods in the global env.
I dunno what experts would think, though, but I suggest having this base code for faking a class:
Code: Select all
function class()
local new_class = {}
new_class.__index = new_class
new_class.new = function(self)
return setmetatable({},self)
end
return new_class
end
Code: Select all
-- Example:
Ball = class()
function Ball:move(x,y)
self.x,self.y = x,y
end
-- etc etc
Written from scratch, hope there's no err though.
By the way, there's good documentation on OO on chapter 16 of PIL
Inny ninja'ed me!
Re: oopong [WIP]
Actually sorry Roland. I like that this (fake) Pong have a breakout/arkanoid layout/resolution. Since it's a 1p game only it's more natural (at least imho for me) that ball goes up than goes travel horizontally.Roland_Yonaba wrote:First if all, resolution is odd.
I'm not a coder by nature or formation but actually I have also some doubts in understand some coding decisions you made. Some already discussed so don't matter anymore.
Anyway when you polish a bit of gameplay (no restart?!?) this you will start in right track. Keep working on it...
Re: oopong [WIP]
Thanks for you feedbacks. I am at work atm so i'll check them later and i'll post the result !
Who is online
Users browsing this forum: Ahrefs [Bot] and 0 guests