android
Prev Previous Post   Next Post Next
  #1  
Old 12-08-2009, 01:09 PM
ZaPx64's Avatar
ZaPx64 ZaPx64 is offline
Member
 
Join Date: Nov 2009
Location: Germany
Posts: 136
Default X-Fi2 LUA Scripting - Showcase & Questions

Okay, since the firmware thread has gone a little offtopic, I think its better to make a seperate thread for this (Maybe this could be a sticky). The Scripts get more complex as well. Thanks go to all the people that helped unvealing the mysteries of the Zen (although there's something to discover yet).

So let's begin. I worked on a Mahjongg game yesterday and want to share it. There is still some content creation needed, but I guess the code might be useful for some of you, because it's already working fine (at least it almost does... there's a bug that prevents the player to combine the last two tiles).

Current Version: Alpha 1

Download zipped with media files (~12 kb):
http://vteam.vt.ohost.de/releases/Zen/Mahjongg.zip

The plain code:

Code:
--[[
    Mahjongg for Creative's Zen X-Fi2
    by ZaP 2009 for Freemotion-Studios
    
    This is freeware. Feel free to spread.
    Any commercial use is forbidden.
]]

-- colors
cblack = color.new(0,0,0);

-- load images
i1 = image.load("TILE1.PNG");
i2 = image.load("TILE2.PNG");
imgFrame = image.load("FRAME.PNG");
imgRed = image.load("FRAMERED.PNG");
imgGreen = image.load("FRAMEGREEN.PNG");

-- tile structarray
tile = {x = {},y = {},img = {},num = {},z = {},value = {}};

-- some globals
tilecount = 0;
touch_x = 0;
touch_y = 0;
clickedtile = -1;


function placetile(img,fx,fy,fz,value)

    tile.x[tilecount] = fx;
    tile.y[tilecount] = fy;
    tile.z[tilecount] = fz;
    tile.img[tilecount] = img;
    tile.num[tilecount] = tilecount;
    tile.value[tilecount] = value;
    
    tilecount = tilecount + 1;
    
end;


function drawtiles()

    for i = 0, tilecount - 1 do
        
        tile.img[i]:draw(tile.x[i] * 20, tile.y[i] * 26 - tile.z[i] * 4);
        
    end;

end;


function updatetiles()

    local highest = -1;

    for i = 0, tilecount - 1 do
        
        -- tile clicked?
        if touch_x > 0 and touch_y > 0 then
            
            if touch_x >= (tile.x[i] * 20) and touch_x <= ((tile.x[i] + 1) * 20) and touch_y <= (((tile.y[i] + 1) - tile.z[i] * 4) * 30) and touch_y >= ((tile.y[i] - tile.z[i] * 4) * 30) then
            
                if clickedtile == -1 then -- draw frame over touched tile
                    if highest == -1 then
                        highest = i;
                        clickedtile = highest;
                    else
                        if tile.z[highest] < tile.z[i] then
                            highest = tile.num[i];
                            clickedtile = highest;
                        end;
                    end;
                else -- compare both clicked tiles
                    -- tiles equal --> remove both
                    if tile.value[clickedtile] == tile.value[i] and tile.num[clickedtile] ~= tile.num[i] and tileIsFree(tile.num[i]) and tileIsFree(tile.num[clickedtile]) then
                        imgFrame:draw(tile.x[clickedtile] * 20 - 7, tile.y[clickedtile] * 26 - tile.z[clickedtile] * 4 - 7);
                        imgGreen:draw(tile.x[i] * 20 - 7, tile.y[i] * 26 - tile.z[i] * 4 - 7);
                        removetiles(i,clickedtile);
                    else
                        imgFrame:draw(tile.x[clickedtile] * 20 - 7, tile.y[clickedtile] * 26 - tile.z[clickedtile] * 4 - 7);
                        imgRed:draw(tile.x[i] * 20 - 7, tile.y[i] * 26 - tile.z[i] * 4 - 7);
                    end;
                
                    screen.update();
                    os.sleep(50);
                
                    clickedtile = -1; -- reset
                    break;
                end;
                
            end;
            
        end;
        
    end;
    
    -- draw blue frame on clicked tile
    if clickedtile >= 0 then
        imgFrame:draw(tile.x[clickedtile] * 20 - 7, tile.y[clickedtile] * 26 - tile.z[clickedtile] * 4 - 7);
    end;
    
end;


function removetiles(a,b)

    -- shift down array
    -- set last array pos zero
    
    -- shift + remove a
    for i = a, tilecount - 2 do
        tile.x[i] = tile.x[i+1];
        tile.y[i] = tile.y[i+1];
        tile.z[i] = tile.z[i+1];
        tile.img[i] = tile.img[i+1];
        tile.num[i] = tile.num[i+1];
        tile.value[i] = tile.value[i+1];
    end;

    tilecount = tilecount - 1;
    
    tile.x[tilecount] = nil;
    tile.y[tilecount] = nil;
    tile.z[tilecount] = nil;
    tile.img[tilecount] = nil;
    tile.num[tilecount] = nil;
    tile.value[tilecount] = nil;
    
        
    -- shift + remove b
    for i = b, tilecount - 2 do
        tile.x[i] = tile.x[i+1];
        tile.y[i] = tile.y[i+1];
        tile.z[i] = tile.z[i+1];
        tile.img[i] = tile.img[i+1];
        tile.num[i] = tile.num[i+1];
        tile.value[i] = tile.value[i+1];
    end;

    tilecount = tilecount - 1;    
    
    tile.x[tilecount] = nil;
    tile.y[tilecount] = nil;
    tile.z[tilecount] = nil;
    tile.img[tilecount] = nil;
    tile.num[tilecount] = nil;
    tile.value[tilecount] = nil;
    
end;


function tileIsFree(n)

    -- tile must be on top level / has no tiles above it
    -- at least two non parallel edges must be free

    local edge_vert = 0;
    local edge_horz = 0;
    
    for i = 0, tilecount - 1 do
    
        -- tile above -> return flase
        if tile.x[i] == tile.x[n] and tile.y[i] == tile.y[n] and tile.z[i] > tile.z[n] then
            return false;
        end;
    
        if tile.x[i] == (tile.x[n] + 1) and tile.z[i] == tile.z[n] then edge_vert = edge_vert + 1; end;
        if tile.x[i] == (tile.x[n] - 1) and tile.z[i] == tile.z[n] then edge_vert = edge_vert + 1; end;
        if tile.y[i] == (tile.y[n] + 1) and tile.z[i] == tile.z[n] then edge_horz = edge_horz + 1; end;
        if tile.y[i] == (tile.y[n] - 1) and tile.z[i] == tile.z[n] then edge_horz = edge_horz + 1; end;
    
    end;
    
    if edge_horz == 2 or edge_vert == 2 then return 0; end;
    
    return true;
    
end;


-- !!!! game init --> this is the puzzle structure !!!!
placetile(i1,1,1,0,1);
placetile(i2,2,1,0,2);
placetile(i2,1,2,0,2);
placetile(i2,2,2,0,1);
placetile(i1,2,2,1,1);
placetile(i2,3,2,0,2);

screen.clear();

-- main loop
while true do

    screen.fillrect(0,0,400,240,cblack);
    
    if control.read() == 1 then
        if (control.isTouch() == 1) and (touch.click() == 1) then 
            touch_x, touch_y = touch.pos();
        end;
        
        if (control.isButton() == 1) and (button.home() == 1) and (button.up() == 1) then 
            break;
        end;
    end;
    
    drawtiles();
    updatetiles();
        
    screen.update();
    os.sleep(10);

    -- reset touch vars for later use    
    touch_x = 0;
    touch_y = 0;
    
end;
Reply With Quote

Advertisement [Remove Advertisement]

 

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 09:16 AM.