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:
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
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:
Code: Select all
if ballIntersect(
_x,
_y,
_radius,
0, love.graphics.getHeight()+10,
love.graphics.getWidth(), 10
) == true then
return false
end
This way is usually more legible:
Code: Select all
if ballIntersect(_x, _y, _radius, height, width) then
return false
end
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!