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

Show off your games, demos and other (playable) creations.
User avatar
Doctory
Party member
Posts: 441
Joined: Fri Dec 27, 2013 4:53 pm

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

Post by Doctory »

this is very simple yet one of the most fun games i have ever played. very nicely done
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

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

Post 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.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

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

Post 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.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

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

Post by davisdude »

edit:
Image

Image
Not as good as Micha's...
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

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

Post 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.
Attachments
numDiv.love
Refactored version of number/cker
(23.13 KiB) Downloaded 136 times
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests