Page 1 of 1
Sloppy code?
Posted: Sun Dec 12, 2010 7:05 pm
by trlestew
Hello, I'm new.
I'm also completely new to programming and went from several languages such as C#,C++, and now LUA. I'm currently studying up on the free primer of LUA. So far, I'm at least able to comprehend it. So I decided to try out the LOVE engine in order to (attempt to) make 2D games. My question here is this; is this code sloppy or formatted incorrectly? I'm not too sure if it should look like this :/
Code: Select all
-- Main initialization setting resolution and font along with defining variables
function love.load()
love.graphics.setMode(1024, 768,false, true, 0)
love.graphics.setCaption("LOVE Break Demonstration")
local f = love.graphics.newFont(32)
love.graphics.setFont(f)
love.graphics.setBackgroundColor(0,0,0)
player = love.graphics.newImage("images/bat.png")
title = love.graphics.newImage("images/splash.png")
tset1 = love.graphics.newImage("images/barrier1shdw.png")
tset2 = love.graphics.newImage("images/barrier1shdwr.png")
ball = love.graphics.newImage("images/ball.png")
Texts = {"Play","Quit"}
end
-- Drawing the menu
function love.draw()
love.graphics.print(Texts[1] ,450, 420)
love.graphics.print(Texts[2] ,450, 480)
love.graphics.draw(title ,275, 120)
love.graphics.draw(tset1 ,200, 120)
love.graphics.draw(tset2 ,750, 120)
end
Re: Sloppy code?
Posted: Sun Dec 12, 2010 7:19 pm
by kikito
Hi there!
First, your tabbing is a bit off. Lines inside a function, a condition, or a loop should have tabulation. Everything between the 'end' keyword and the thing 'closed' by that end should be tabified.
Code: Select all
-- Main initialization setting resolution and font along with defining variables
function love.load()
love.graphics.setMode(1024, 768,false, true, 0)
love.graphics.setCaption("LOVE Break Demonstration")
local f = love.graphics.newFont(32)
love.graphics.setFont(f)
love.graphics.setBackgroundColor(0,0,0)
player = love.graphics.newImage("images/bat.png")
title = love.graphics.newImage("images/splash.png")
tset1 = love.graphics.newImage("images/barrier1shdw.png")
tset2 = love.graphics.newImage("images/barrier1shdwr.png")
ball = love.graphics.newImage("images/ball.png")
Texts = {"Play","Quit"}
end
-- Drawing the menu
function love.draw()
love.graphics.print(Texts[1] ,450, 420)
love.graphics.print(Texts[2] ,450, 480)
love.graphics.draw(title ,275, 120)
love.graphics.draw(tset1 ,200, 120)
love.graphics.draw(tset2 ,750, 120)
end
This might not look important now, because your source code is small. But when it gets mid-sized, not having proper identation makes it very difficult to manage. Try to be very strict with that.
The other thing I'd mention is the naming convention. Most of your variables start with lowercase (player, title, ball) while Texts starts with uppercase. There doesn't seem to be a good reason for making this exception. Again, with a small code it's no big deal, but once it starts growing it will be a pain ("was this called EnemyFiring, enemyFiring or enemy_firing?").
Re: Sloppy code?
Posted: Sun Dec 12, 2010 8:08 pm
by Robin
Also for the future:
- Do things in loops. for ... do blah end is much better than blah blah blah, to give a nonsensical example.
- Organise things in tables. For example, the images:
Code: Select all
images = {
player = love.graphics.newImage("images/bat.png"),
title = love.graphics.newImage("images/splash.png"),
tset1 = love.graphics.newImage("images/barrier1shdw.png"),
tset2 = love.graphics.newImage("images/barrier1shdwr.png"),
ball = love.graphics.newImage("images/ball.png")
}
Don't forget to keep it consistent, though.
Code: Select all
love.graphics.setMode(1024, 768,false, true, 0)
We have a wonderful special file for that, called conf.lua, which handles that much better.
Good luck.
Re: Sloppy code?
Posted: Sun Dec 12, 2010 9:08 pm
by Mud
trlestew wrote:I'm currently studying up on the free primer of LUA.
Which primer, if you don't mind me asking?
trlestew wrote:formatted incorrectly?
Indent your code to reflect it's block structure (i.e. indent the bodies of functions, control flow statements, etc.). Makes it a lot easier to follow the code, to see it's structure. Compare:
Code: Select all
function manchu(yin,yang)
if yin then
return foo(yin)
elseif yang then
while yang do
yang = foo(yang)
yin = bar(yang)
showstatus(bar(yang))
end
end
manchu(yin,yang)
end
manchu(i,a)
Code: Select all
function manchu(yin,yang)
if yin then
return foo(yin)
elseif yang then
while yang do
yang = foo(yang)
yin = bar(yang)
showstatus(bar(yang))
end
end
manchu(yin,yang)
end
manchu(i,a)
Re: Sloppy code?
Posted: Sun Dec 12, 2010 9:09 pm
by trlestew
I followed those tips, thanks.
but now when I try to draw the images, I get an error at line 23; when the images are about to be drawn to the screen. "Incorrect parameter type, userdata expected."
And it's this primer.
http://www.lua.org/pil/index.html
Re: Sloppy code?
Posted: Sun Dec 12, 2010 9:28 pm
by Mud
trlestew wrote:when I try to draw the images, I get an error [..] "Incorrect parameter type, userdata expected."
love.graphics.draw(tset1 ,200, 120)
love.graphics.draw(images.tset1 ,200, 120)
Tables are just collections of key=value pairs.
Code: Select all
t = {} -- new table
t["foo"] = "bar"
print(t["foo"])
Keys and values can be any type, but Lua allows an alternative syntax for keys which are strings:
Is exactly equivalent to:
Code: Select all
t = {
["foo"] = "bar"
}
t["foo"] = "zip"
Your images are now stored in a table, indexed by their name (a string).
Re: Sloppy code?
Posted: Sun Dec 12, 2010 9:30 pm
by trlestew
Thanks, I have it working again.
That cleared up a few things too.