Posted 01 October 2012 - 06:40 PM
First of all, this is my first attempt at Lua in any form, now on to the API:
The intro:
The code:
The functions are:
-maxX/Y is the max X/Y coordinate (min is always 1)
-step is the space between the characters
-default is the default map tile (string)
-generated the map, must be called once at the beginning - do no loop unless you are sure what your doing!
set(x, y, set)
-x/y is the x/y coordinate on the map
-set is the string you are going to set the tile to
-sets the selected tile to the set string
get(x,y)
-x/y is the x/y coordinate on the map
-returns the string of the tile selected
getinput()
-returns input (oi.read())
draw(xmin, ymin, xmax, ymax)
-x/ymin is the minumum x/y coordinate to be used in drawing the map if <1 it is set to 1
-x/ymax is the maximum x/y coordinate to be used in drawing the map if >maxX/Y it is set to maxX/Y
-draws the selected sections of the map
checkx(x)
-x is the x coordinate being checked
-returns a valid x coordinate (one inside the x/y min/max range if it is outside the range, else stays the same)
checky(y)
-y is the y coordinate being checked
-same as checkx(x) but with a y coordinate
Example:
Please give it a go, or at least look at it, and tell me what you think along with errors/improvements!
The intro:
Spoiler
this is an API which simplifys making little games on your computers, its not complicated to use and makes it easy to get a whole range of games on your little minecraft computer!The code:
Spoiler
local data = { }
local xmax = 3
local ymax = 3
local step = 1
function create(xx, yx, stp, set)
local x = 1
local y = 1
xmax = xx
ymax = yx
step = stp
for x=1,xmax,step do
for y=1,ymax,step do
local ydat = data[x]
if ydat == nil then
data[x] = {set}
else
ydat[y] = set
end
end
end
end
function set(x, y, set)
local ndat = data[x]
ndat[y] = set
end
function get(x, y)
local ndat = data[x]
local dat = ndat[y]
return dat
end
function getinput()
input = io.read()
return input
end
function draw(xl, yl, xh, yh)
xl = checkx(xl)
xh = checkx(xh)
yl = checky(yl)
yh = checky(yh)
for x=xl,xh,step do
local line = ""
for y=yl,yh,step do
line = line..get(x,y)
end
print(line)
end
end
function checkx(x)
if x < 1 then
return 1
elseif x > xmax then
return xmax
else
return x
end
end
function checky(y)
if y < 1 then
return 1
elseif y > ymax then
return ymax
else
return y
end
end
The functions are:
Spoiler
create(maxX, maxY, step, default)-maxX/Y is the max X/Y coordinate (min is always 1)
-step is the space between the characters
-default is the default map tile (string)
-generated the map, must be called once at the beginning - do no loop unless you are sure what your doing!
set(x, y, set)
-x/y is the x/y coordinate on the map
-set is the string you are going to set the tile to
-sets the selected tile to the set string
get(x,y)
-x/y is the x/y coordinate on the map
-returns the string of the tile selected
getinput()
-returns input (oi.read())
draw(xmin, ymin, xmax, ymax)
-x/ymin is the minumum x/y coordinate to be used in drawing the map if <1 it is set to 1
-x/ymax is the maximum x/y coordinate to be used in drawing the map if >maxX/Y it is set to maxX/Y
-draws the selected sections of the map
checkx(x)
-x is the x coordinate being checked
-returns a valid x coordinate (one inside the x/y min/max range if it is outside the range, else stays the same)
checky(y)
-y is the y coordinate being checked
-same as checkx(x) but with a y coordinate
Example:
Spoiler
local on = true
game.create(5,5,1,"-")
game.set(3,2,"@")
while on do
term.clear()
term.setCursorPos(1,1)
game.draw(2,2,4,4)
local input = game.getinput()
while input == nil do
sleep(2)
input = game.getinput()
end
if input == "q" then
on = false
end
term.clear()
end
term.clear()
term.setCursorPos(1,1)
Please give it a go, or at least look at it, and tell me what you think along with errors/improvements!