Page 1 of 2

score counter and putting objects to collect

Posted: Thu Jul 11, 2013 8:18 pm
by Saltyham
Hello everyone I've just recently started using love2d and being a coding novice how would I put a score counter and what would be the quickest way to put down Items that disappear when the player touches them and add to the score counter any hints or tips would be nice :?

Re: score counter and putting objects to collect

Posted: Thu Jul 11, 2013 9:00 pm
by micha
Create a table. Let's say you call it "objectList". Then store all objects that are on screen in this table. Each object itself is a table again, holding data like the coordinates, height, width and the image. In the draw-part you loop over all entries to draw them. In the update-part you have to check for collision with the player. If so, remove the object from objectList.

For a score counter you could create a global variable called score and increase it, whenever a collision with one of the objects happens.

If I remember correctly, you can find some information on collecting items in this tutorial.

Re: Didn't help much :/

Posted: Thu Jul 11, 2013 10:34 pm
by Saltyham
Still Having a hard time figuring this out, How would this code look, what would be in it.

Re: score counter and putting objects to collect

Posted: Thu Jul 11, 2013 10:47 pm
by seanmd
Is there a piece of your code where powerups are marked as collected or destroyed? In that chunk of code increment a score variable that you initialize to zero upon entering the game state. It all sort of depends on how your game is structured as to how you go about keeping track of things.


an overly simplified example:

Code: Select all

local levelScore = 0

function game:enter()
  ...
  levelScore = 0
  ...
end

local function collect(collectible)
  ...
  levelScore = levelScore + collectible.value
  ...
end

Re: score counter and putting objects to collect

Posted: Thu Jul 11, 2013 11:02 pm
by Saltyham
Thanks, but the issue I'm worried about at the moment is making the objects disappear when the player is over them. If you have any code for that I would be most grateful.
:|

Re: score counter and putting objects to collect

Posted: Fri Jul 12, 2013 2:11 am
by davisdude
It would be easier if you posted a .love. Then we could truly help you.

Re: score counter and putting objects to collect

Posted: Fri Jul 12, 2013 2:33 am
by Saltyham
Its a bit bad but here ya go.

Re: score counter and putting objects to collect

Posted: Fri Jul 12, 2013 4:19 am
by davisdude
Your game is not really all that bad (albeit a bit strange)

I chose to make little fruits to eat for your 'based god'
I made it so that about half of you had to be over it in order to be able to eat it.

Notes:
The Title (The adventures of lil B) becomes red when you scroll over 'quit'.
You registered the image 'baseland' twice.
Player movement is rather sluggish.

I fixed some other things too, but you're probably really only interested in the 'fruits.lua' bit.

If you have any questions about the code, feel free to ask!
I mostly did performance related things, although it still loads extremely slowly for me...

The score counter will be homework... :)

Re: score counter and putting objects to collect

Posted: Fri Jul 12, 2013 5:12 am
by Saltyham
Thank you very much for the help, I'm gonna reverse engineer the hell out of additions you put in. also instead of making the fruits disappear could i change them into another graphic, also is there a way to get a sound effect for the picking up of the fruits?

Re: score counter and putting objects to collect

Posted: Fri Jul 12, 2013 7:46 am
by Plu
Certainly. Probably the best way is, on pickup, replace the fruit with a new entity with a different image, but the same location. This will also prevent old code from trying to pick the new image up as well (because the code should only respond to collisions with specific objects)

You can play sound using

Code: Select all

soundFile = love.audio.newSource( 'filename.wav' )
to load the sound (give it a filename to the sound and call it ONCE during loading) and you can play the sound fragment by calling

Code: Select all

love.audio.play( soundFile )