Sloppy code?

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.
Post Reply
trlestew
Prole
Posts: 9
Joined: Sun Dec 12, 2010 6:55 pm

Sloppy code?

Post 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



User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Sloppy code?

Post 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?").
When I write def I mean function.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Sloppy code?

Post 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. :)
Help us help you: attach a .love.
User avatar
Mud
Citizen
Posts: 98
Joined: Fri Nov 05, 2010 4:54 am

Re: Sloppy code?

Post 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)
trlestew
Prole
Posts: 9
Joined: Sun Dec 12, 2010 6:55 pm

Re: Sloppy code?

Post 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
User avatar
Mud
Citizen
Posts: 98
Joined: Fri Nov 05, 2010 4:54 am

Re: Sloppy code?

Post 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:

Code: Select all

t = {
  foo = "bar"
}
t.foo = "zip"
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).
Last edited by Mud on Sun Dec 12, 2010 9:35 pm, edited 1 time in total.
trlestew
Prole
Posts: 9
Joined: Sun Dec 12, 2010 6:55 pm

Re: Sloppy code?

Post by trlestew »

Thanks, I have it working again.
That cleared up a few things too.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest