Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- term.clear()
- print [[GameAPI r5
- Press Backspace at any time to exit the game.]]
- sleep(2)
- screen = {}
- screen.width = 40
- screen.height = 15
- screen.actors = {}
- function round(v,int)
- local int = (int~=nil) and int or 1
- local n = v%int
- if n >= int/2 then
- return (v-n)+int
- else
- return (v-n)
- end
- end
- for y=1,screen.height do
- local t = {}
- for x=1,screen.width do
- table.insert(t,' ')
- end
- table.insert(screen.actors,t)
- end
- function draw()
- term.clear()
- for y,v in pairs(screen.actors) do
- local line = ''
- for x,vv in pairs(v) do
- line = line .. vv
- end
- print(line)
- end
- end
- function format(actor)
- if actor.bounded then
- actor.x = (actor.x>1) and actor.x or 1
- actor.x = (actor.x<screen.width) and actor.x or screen.width
- actor.y = (actor.y>1) and actor.y or 1
- actor.y = (actor.y<screen.height) and actor.y or screen.height
- end
- screen.actors[round(actor.y)][round(actor.x)] = actor.char
- return actor
- end
- actor = {}
- function actor:new(x,y,char,b)
- actor.x = x
- actor.y = y
- actor.char = char
- actor.bounded = (b~=nil) and b or true
- screen.actors[round(y)][round(x)] = char
- return format(actor)
- end
- function actor:setPos(x,y)
- screen.actors[actor.y][actor.x] = ' '
- actor.x = x
- actor.y = y
- actor = format(actor)
- end
- function actor:setX(x)
- screen.actors[actor.y][actor.x] = ' '
- actor.x = x
- actor = format(actor)
- end
- function actor:setY(y)
- screen.actors[actor.y][actor.x] = ' '
- actor.y = y
- actor = format(actor)
- end
- function actor:addPos(x,y)
- screen.actors[actor.y][actor.x] = ' '
- actor.x = actor.x + x
- actor.y = actor.y + y
- actor = format(actor)
- end
- function actor:addX(x)
- screen.actors[actor.y][actor.x] = ' '
- actor.x = actor.x + x
- actor = format(actor)
- end
- function actor:addY(y)
- screen.actors[actor.y][actor.x] = ' '
- actor.y = actor.y + y
- actor = format(actor)
- end
- function actor:setChar(char)
- screen.actors[actor.y][actor.x] = char
- actor.char = char
- end
- function actor:destroy()
- screen.actors[actor.y][actor.x] = ' '
- actor = nil
- end
- game = {}
- game.state = 'running'
- function game.quit()
- term.clear()
- game.state = 'dead'
- end
- function game.start()
- os.startTimer(1/30)
- while game.state == 'running' do
- local t,k = os.pullEvent()
- if t == 'timer' then
- draw()
- if game.update ~= nil then game.update() end
- os.startTimer(1/30)
- elseif t == 'key' then
- if k == 14 then
- game.quit()
- else
- if game.key ~= nil then game.key(k) end
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement