Murga-Projects Forums

Full Version: another tile game clone
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This one is a clone of the concentration/memory game where you have an array of overturned cards and you flip two at a time trying to match the image on them. It was a whim, and I decided to see how quickly I could whip out a beta.

The script grabs the first 24 images from a given directory and sets each image to two randomly selected tiles. Click on two tiles, and if they match then the images stay visible and their buttons are disabled. If they don't match, they are hidden again after half a second.

The script is based around the use of 64px images, which on a Linux (and OS X?) system are easy to find in /usr/share/icons and /usr/share/pixmaps. No idea if Windows still uses just *.ico icons or if they've also begun to include other formats.

I want to include some default images in the future, but haven't bothered with that yet. Also haven't bothered to check what happens if there are fewer than 24 usable images in the directory.

Feel free to mess with this script if you want to make it better.

#!/usr/bin/env murgaLua
-- concentration
-- 2011 mikshaw

ts = 100 -- tile size
cols = 8 -- number of horizontal tiles
rows = 6 -- vertical tiles
img_dir = "/usr/share/icons/crystalsvg/64x64/apps/"

function tile_cb(self)
if not first_tile then first_tile = self
  moves = moves + 1
  if first_tile:tooltip() == self:tooltip() then
    active = active - 2
    if active == 0 then fltk.fl_alert(moves.." moves") end
first_tile = nil

used_numbers = "x"
function unique_random()
local rand = math.random(0, rows * cols - 1)
-- try again if number is already used
while string.find(used_numbers, "x"..rand.."x") do
rand = math.random(0, rows * cols - 1)
-- save used numbers in a string
used_numbers = used_numbers..rand.."x"
return rand

win = fltk:Fl_Double_Window(ts * cols , ts * rows, "Memory Game")

tile = {}
r, c = 0, 0 -- row and column positions
for i = 0, rows * cols - 1 do
  tile[i] = fltk:Fl_Button(c, r, ts, ts)
  c = c + ts
  -- start the next row
  if c == win:w() then c = 0; r = r + ts end


-- apply images to random tiles
-- two copies of each image
count = 0
img = {}
for file in lfs.dir(img_dir) do
  if count < cols * rows / 2 then -- stop grabbing images after tiles are filled
    img_check = Fl_Shared_Image.get(img_dir..file)
    if img_check then
      img[count] = img_check
      local first = unique_random() -- first instance of the image
      local second = unique_random() -- second instance
      count = count + 1
  else break

moves = 0
active = rows * cols
Fl_Tooltip:disable() -- tooltip values are used only to compare tiles

Reference URL's