Page 3 of 3

Re: NUMBERF/CKER (formerly Numberwang) - math game (not 2048

Posted: Thu Nov 19, 2015 12:08 pm
by Doctory
this is very simple yet one of the most fun games i have ever played. very nicely done

Re: NUMBERF/CKER (formerly Numberwang) - math game (not 2048

Posted: Mon Nov 23, 2015 2:56 am
by Ref
Hi unek!
I looked at your code and was quite impressed.
I was wondering about one construct.
You define anim_after() function in merge.

Code: Select all

local function merge(x1, y1, x2, y2)
  launchAnim(x1, y1, x2, y2, board[y2][x2], board[y1][x1])

  board[y1][x1] = board[y1][x1] + board[y2][x2]
  board[y2][x2] = 0

  anim_after = function()
    update(x1, y1)
  end
end
No parameters are passed to the internal function update via the anim_after() function.
You then call anim_after() outside of merge - in love.update().

Code: Select all

 if anim_dt - dt < anim_time and anim_dt > anim_time and anim_after then
    anim_after()
  end
This works but what parameters are being passed to the local update(x1,y1) function?
Just don't know how this works.
Appreciate an explaination.

Re: NUMBERF/CKER (formerly Numberwang) - math game (not 2048

Posted: Thu Nov 26, 2015 10:27 am
by Nixola
When merge is called, its arguments are stored as local variables to the function; defining anim_after in the same function using variables local to the function creates what is called a closure. The local variables x1,y1 are "saved" and bound to anim_after, so every time merge is called it defines anim_after to use whatever x1,y1 merge received.

TL,DR: anim_after uses whatever x1,y1 the "merge" function received last.

EDIT: Look here for some closures explanation from the PIL.

Re: NUMBERF/CKER (formerly Numberwang) - math game (not 2048

Posted: Thu Nov 26, 2015 3:23 pm
by davisdude
edit:
Image

Image
Not as good as Micha's...

Re: NUMBERF/CKER (formerly Numberwang) - math game (not 2048

Posted: Tue Dec 01, 2015 3:48 pm
by Ref
Belated thanks to Nixola for explanation of closures.

The highScore function posted by Beelz sure is slick.
Added it to my version of Numberf/cker.
Only required modifying two lines of code.

Posting my slightly modified version of this interesting game - no credit claimed.