This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
billysback's profile picture

[API] Game API

Started by billysback, 01 October 2012 - 04:40 PM
billysback #1
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:
Spoilerthis 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:
Spoilercreate(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!
Zudo #2
Posted 08 December 2012 - 09:21 AM
No pics, no clicks
billysback #3
Posted 08 December 2012 - 09:24 AM
lol, please do not use this.
Also what you are saying is irrelivant, this is an API; what would I take picks of. Look at the code it's like 100 lines long
either way as I said don't use this use the much more complex CGE…
This was the first thing I posted on these forums -.-
bjornir90 #4
Posted 08 December 2012 - 06:48 PM
No pics, no clicks
Lol you bump a 2 months old topic to say that ?