|
#1
|
||||
|
||||
|
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;
|
|
|
|||
|
|
|
#2
|
|||
|
|||
|
Great! Congratulations!
|
|
#3
|
|||
|
|||
|
Would it be OK for me to copy code I've posted in the original thread over to here, so it's easier to find? Or maybe just supply links?
|
|
#4
|
||||
|
||||
|
You're (probably) a free man, go ahead!
I just created this thread to un-offtopic the firmware thread.
|
|
#5
|
|||
|
|||
|
I would highly recommend changing button.up() to button.click() to remove any chance of screwing up the messaging queue. Otherwise good job! I hope we can get a little homebrew scene going =)~
Edit: Help fill in the blanks if you wish =): http://en.wikipedia.org/wiki/User:Thieving6/X-FI_2_Lua Edit2: Most of the functions are documented now. The 1.1 Whats needed section contains the functions and sections with need new or more complete information. Please follow the format if you can. If you know how a function works and have tested it please add it to the correct placeholder and remove it from the Whats needed section. After all the functions are documented I would like to start a examples branch page! Edit3: Just reversed the file format for the RES.BIN they included with the Sudoku application. While I'd like to release the white papers and a compiler/decompiler it is a proprietary file format(although VERY simple). I'm not sure how to continue with this, should I ask Creative first? If so where the heck is a support email, couldn't find one. Or, any other ideas on how or if I should proceed? Last edited by ThievingSix; 12-13-2009 at 02:56 AM. |
|
#6
|
|||
|
|||
|
Can any of you excellent programmers Please please program a tic-tac-toe game for it?
It would make the XFi2 just incredible! http://www.prongo.com/tictac/ http://en.wikipedia.org/wiki/Tic-tac-toe |
|
#7
|
||||
|
||||
|
I thought of a Four in a Row game, but Tic Tac Toe is of course easier to finish, if you don't need an AI. I'll have a try anyway (Mahjongg is currently on ice - too small buttons, I need to find an alternative...)
|
|
#8
|
|||
|
|||
|
Yes please thanks a lot! AI is not essential but if you can manage that then awesome!
|
|
#9
|
|||
|
|||
|
Has anyone attempted a file directory listing? This is starting to piss me off.
From what I can tell we have no os.execute() or it's been removed from the enviornment and io.popen seems to fail every time. Here's what I've been fussing with, I get error occurred almost immediately: Code:
clBlack = color.new(0,0,0);
clWhite = color.new(255,255,255);
screen.clear();
text.color(clWhite);
text.size(25);
screen.fillrect(0,0,400,240,clBlack);
screen.update();
i = 0;
-- This code will execute `ls -a' and print its output
cmd = io.popen("ls -a", "r");
if (cmd) then
line = cmd:read();
while (line) do
text.draw(0,(i * 30),line);
i = i + 1;
line = cmd:read();
os.sleep(2);
screen.update();
end
cmd:close();
else
text.draw(0,0,"Error");
end;
|
|
#10
|
||||
|
||||
|
Just a little demo of the display orientation.
screen.orientation(1) = vertical display 240x400 screen.orientation(0) = horizontal display 400x240 Remember to reset to orientation(0) before exiting the application (or the Zen crashes), when using screen.orientation function. Code:
--Program to display Control, Button and Touch value in 240x400 orientation mode
function initscreen()
color_blk = color.new(0,70,0);
screen.orientation(1); --set screen orientation
screen.clear();
screen.fillrect(0,0,240,400,color_blk); -- new values
screen.update();
end;
function drawtext()
color_red = color.new(255,0,0);
color_white = color.new(255,255,255);
color_black = color.new(0,0,0);
text.color(color_red);
text.size(12);
text.draw(10,10, "CONTROL READ = "..control.read());
text.draw(10,25, "CONTROL IS BUTTON = "..control.isButton());
text.draw(10,40, "CONTROL IS TOUCH = "..control.isTouch());
text.draw(10,55, "CONTROL IS SENSOR = "..control.isSensor());
text.draw(10,90, "BUTTON HOME = "..button.home());
text.draw(10,105, "BUTTON POWER = "..button.power());
text.draw(10,120, "BUTTON UP = "..button.up());
text.draw(10,135, "BUTTON HOLD = "..button.hold());
text.draw(10,150, "BUTTON EVENT = "..button.up());
text.draw(10,165, "BUTTON CLICK = "..button.click());
text.draw(10,185, "TOUCH CLICK = "..touch.click());
text.draw(10,200, "TOUCH HOLD = "..touch.hold());
text.draw(10,215, "TOUCH UP = "..touch.up());
text.draw(10,230, "TOUCH MOVE = "..touch.move());
text.draw(10,245, "TOUCH DOWN = "..touch.down());
text.draw(10,260, "TOUCH EVENT = "..touch.event());
text.color(color_white);
x, y = touch.pos();
text.draw(10,280, "TOUCH POS X = "..x);
text.draw(10,295, "TOUCH POS Y = "..y);
text.draw(10,380, "Date = "..os.date(), "center", 240); -- new values
screen.update();
screen.fillrect(0,0,240,400,color_black); -- new values
end;
initscreen();
drawtext();
while true do
--Touch
if (control.read() == 1) and (control.isTouch() == 1) and (touch.click() == 1) then
drawtext();
os.sleep(1);
end;
--Hold Home button to exit
if (control.read() == 1) and (control.isButton() == 1) and
(button.home() == 1) and (button.hold() == 1) then
screen.orientation(0); --Reset screen orientation
break;
end;
--Home button Click
if (control.read() == 1) and (control.isButton() == 1) and
(button.home() == 1) and (button.click() == 1)then
audio.beep(1000,200);
end;
--Power button click
if (control.read() == 1) and (control.isButton() == 1) and
(button.power() == 1) and (button.click() == 1)then
audio.beep(2000,200);
end;
drawtext();
os.sleep(10);
end;
|
|
#11
|
||||
|
||||
|
I didn't have too much time but finally here it is:
Tic Tac Toe, without AI but with a cool X-Mas design ![]() ![]() Download (~157 kB): http://vteam.vt.ohost.de/releases/Ze.../TicTacToe.zip (outdated! See http://sites.google.com/site/zenapplications/ for a download.) Have fun! Last edited by ZaPx64; 02-01-2010 at 10:28 AM. |
|
#12
|
|||
|
|||
|
Thanks a ton. It's an amazing game!
|
|
#13
|
|||
|
|||
|
i think pong would be a great game for the xfi2 if someone could perfect the slide motion interface
the game: http://en.wikipedia.org/wiki/Pong the code (c): http://library.thinkquest.org/18268/...pongsource.htm (vb): http://www.planet-source-code.com/vb...=1971&lngWId=1 |
|
#14
|
||||
|
||||
|
Here's a app based on the game "Boink" which came bundled with Missile Master.
http://www.mediafire.com/file/zywyyadmgm4/Boink.zip if anyone wants to improve the code or graphics go ahead, same goes for copying sections of code :P Code:
-- load image files
imgBack = image.load("background.png");
imgSmile = image.load("smile.png");
imgCross = image.load("cross.png");
imgButton = image.load("button.png");
imgIns = image.load("ins.png");
-- set up global variables
touch_x = 0;
touch_y = 0;
smilenum = 0;
maxsmilenum = 1;
time = 20;
score = 0;
misses = 0;
field = {};
playing = false;
endgame = false;
-- set up the values for field
for i = 1, 20 do
field[i] = 0;
end;
-- Draw the Background
function clearScreen()
if playing == true then
imgBack:draw(0,0);
else
imgIns:draw(0,0);
end;
end;
-- Draw the values of touch_x and touch_y
function drawPos()
local itext = "Score:" .. tostring(score);
local ktext = "Misses:" .. tostring(misses);
text.draw(304,16,itext);
text.draw(304,32,ktext);
end;
-- Checks if the touch is within a region
function touchRegion(rx, ry, w, h)
if touch_x >= rx and touch_x <= (rx + w) and touch_y >= ry and touch_y <= (ry + h) then
return true;
else
return false;
end;
end;
-- Change a random value of field to 2 if it is = to 0
function addSmile()
local ivalue = math.random(1,20);
if field[ivalue] == 0 then
field[ivalue] = 2;
smilenum = smilenum + 1;
end;
end;
-- Draw a smile in the correct place
function drawSmile()
for i = 0,19 do
local line = math.floor(i/5);
local x = ((i-(line*5))*56)+9+4;
local y = (line*56)+8+4;
if field[i+1] >= 2 then
imgSmile:draw(x,y);
field[i+1] = field[i+1] + 1;
if field[i+1] == time then
field[i+1] = 0;
smilenum = smilenum - 1;
misses = misses + 1;
end;
end;
if field[i+1] < 0 then
imgCross:draw(x,y);
field[i+1] = field[i+1] + 1;
end;
end;
end;
-- Calculate the pressed region
function pressSmile()
--find the pressed region
if touchRegion(9,8,289,232) then
local fx = math.floor((touch_x-9)/56)+1;
local fy = math.floor((touch_y-8)/56)+1;
local f = (fy-1)*5+fx;
if field[f] >= 2 then
field[f] = -8;
smilenum = smilenum - 1;
score = score + 1;
end;
--text.draw(310,20,tostring(f));
end;
end;
-- Increasing difficulty
function incDif()
maxsmilenum = math.floor(score/(20*maxsmilenum))+1;
end;
-- Lose
function lose()
if misses == 10 then playing = false; end;
end;
-- Buttons
function buttons()
--quit button
imgButton:draw(304,200);
text.draw(308,204,"Quit");
if touchRegion(304,200,85,27) == true then
endgame = true;
end;
--Play butoon
if playing == false then
imgButton:draw(304,170);
text.draw(308,174,"Play");
if touchRegion(304,174,85,27) == true then
playing = true;
end;
end;
end;
-- Program Loop
while true do
-- Set the screen to blank
clearScreen();
drawPos();
buttons();
if playing == true then
if(smilenum < maxsmilenum) then addSmile(); end;
pressSmile();
drawSmile();
incDif();
lose();
end;
--If a control has been red
if control.read() == 1 then
--If the touch has changed then update values
if (control.isTouch() == 1) and (touch.click() == 1) then
touch_x, touch_y = touch.pos();
else
touch_x = 0;
touch_y = 0;
end;
--If home has been pressed then exit
if (control.isButton() == 1) and (button.home() == 1) and (button.click() == 1) then
break;
end;
end;
screen.update();
os.sleep(8);
if endgame == true then break; end;
end;
Pong next.. |
|
#15
|
|||
|
|||
|
Many thanks for the "Boink" game! It's heaps loads of fun! It's perfect to get used too the players touch-screen as well.
|
|
#16
|
|||
|
|||
|
Hey,
Haven't coded anything fun yet, just been poking around really... Have made a short (after I ripped out all the "testing" code) script to rip the contents of the global table to an almost readable XML format. It also pulls out a little information on the functions it sees, using the debug libs, so there'll be stuff like "what" available... which is just C for all of the standard libraries... Zipped code, icon and sample output here or just the output here. You may want to note that this program doesn't provide any direct user feedback, unless a big error occurs that doesn't get caught and logged. Code:
-- Some logging, also a convenient way of outputting to file
logfile = io.open("logs.txt", "w+")
function addlogline(log)
logfile:write(log);
logfile:write("\r\n");
-- Flush the output - useful for debugging, might want to remove
-- for anything that you care about the performance of
logfile:flush();
end;
-- Print out everything in the current table in a nice XML-ish format
function printtable(table)
for ka,va in pairs(table) do
-- Figure out what type of table entry this is
atype = type(va)
-- Make our XML element for this entry
addlogline("<var name=\""..ka.."\" type=\""..atype.."\">")
-- Does this have children?
-- Also takes into account _G, loaded and func, which all
-- (effectively) contain themselves in some way
if (atype == "table" and ka ~= "_G" and ka ~= "loaded") then
printtable(va)
elseif (atype == "function" and ka ~= "func") then
printtable(debug.getinfo(va))
else
addlogline(tostring(va))
end
-- Close our new XML element
addlogline("</var>")
end
end;
-- Start the XML doc
addlogline("<globalData>")
-- Safely run the global inspector
err2, err1 = pcall(printtable, _G);
-- Handle errors
if (not err2) then
addlogline("Error pulling global data: "..tostring(err1))
end
-- Printing done, end the XML doc
addlogline("</globalData>")
-- Clean up after ourselves
logfile:close()
Quote:
Quote:
Anyway, my first time with an embedded scripting language and my first time with Lua... hope I've come up with something helpful... Now time to see if I can figure out how to use the accelerometer funcs... Anyone been able to run accelerometer.open yet? |
|
#17
|
|||
|
|||
|
I've played...and played..and cursed at the accelerometer table. I've gotten open to "work" just doesn't seem to do squat.
http://en.wikipedia.org/wiki/User:Th...#Control_table control.IsSensor() I'm guessing is for the accelerometer as well but I can't seem to get that do trigger either. |
|
#18
|
|||
|
|||
|
Mind if I ask what you did to get it "working"?
|
|
#19
|
||||
|
||||
|
Do any of you more advanced lua programmers know how to get it to read and write to an external file? This is definitely possible because suduku does it. This would be useful as it would allow us make save games in the apps, as well as a way to record highscores..
|
|
#20
|
|||
|
|||
|
|
![]() |
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
All times are GMT -5. The time now is 01:56 PM.











I just created this thread to un-offtopic the firmware thread.


Linear Mode
