Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!

ComputerCraft game api r5

By: Kingdaro on Dec 26th, 2011  |  syntax: Lua  |  size: 4.32 KB  |  views: 12,565  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. --[[
  2. place this file under .minecraft/mods/ComputerCraft/lua/rom/programs
  3.  
  4. to make a game with this api, make a new file and type shell.run('gameapi') at the top, the functions will then be available for you to use.
  5.  
  6. Breif Explanation of Functions:
  7.  
  8. actor:new(x, y, char[, bounded])
  9. returns a new actor at the x and y positions on the terminal
  10. bounded is a boolean which will determine whether or not your actor's movement is limited to the console window, true by default.
  11.  
  12. actor.x
  13. the actor's x position
  14.  
  15. actor.y
  16. the actor's y position
  17.  
  18. actor.char
  19. the character representing the actor on screen
  20.  
  21. actor.bounded
  22. whether or not the actor can go outside the screen
  23.  
  24. actor:setPos(x,y)
  25. sets the actor's position
  26. **this method and other positioning methods must be used in order for the actor's position to be updated onscreen
  27.  
  28. actor:addPos(x,y)
  29. adds to the actor's position
  30.  
  31. actor:setX(n)
  32. sets the actor's x position to n
  33.  
  34. actor:setY(n)
  35. sets the actor's y position to n
  36.  
  37. actor:addX(n)
  38. adds n to the actor's x position
  39.  
  40. actor:addY(n)
  41. adds n to the actor's y position
  42.  
  43. actor:setChar(char)
  44. change the actor's character to char
  45.  
  46. game.key(k)
  47. user defined, triggered when a key is pressed
  48.  
  49. game.update()
  50. user defined, triggered when a new frame is drawn
  51.  
  52. game.start()
  53. required for the game to start, obviously
  54. **any code after this function will not be ran until the game has stopped
  55.  
  56. game.stop()
  57. quit the program
  58.  
  59. Example at http://pastebin.com/D78fft65
  60.  
  61. /*---*/
  62.  
  63. Changelog:
  64.  
  65. r1:
  66. Released API
  67.  
  68. r4:
  69. Added 'game:setChar()' to change an actor's characters
  70. Added a 'bounded' attribute if you wanted an actor to be able to go outside the screen.
  71. Added 'screen.width' and 'screen.height' as editable attributes to allow for custom widths and heights
  72.  
  73. r5:
  74. Changed instructions for usage.
  75. ]]
  76.  
  77.  
  78. term.clear()
  79. print [[GameAPI r5
  80. Press Backspace at any time to exit the game.]]
  81.  
  82. sleep(2)
  83. screen = {}
  84. screen.width = 40
  85. screen.height = 15
  86. screen.actors = {}
  87.  
  88. function round(v,int)
  89.         local int = (int~=nil) and int or 1
  90.         local n = v%int
  91.        
  92.         if n >= int/2 then
  93.                 return (v-n)+int
  94.         else
  95.                 return (v-n)
  96.         end
  97. end
  98.  
  99. for y=1,screen.height do
  100.         local t = {}
  101.         for x=1,screen.width do
  102.                 table.insert(t,' ')
  103.         end
  104.         table.insert(screen.actors,t)
  105. end
  106.  
  107. function draw()
  108.         term.clear()
  109.         for y,v in pairs(screen.actors) do
  110.                 local line = ''
  111.                 for x,vv in pairs(v) do
  112.                         line = line .. vv
  113.                 end
  114.                 print(line)
  115.         end
  116. end
  117.  
  118. function format(actor)
  119.         if actor.bounded then
  120.                 actor.x = (actor.x>1) and actor.x or 1
  121.                 actor.x = (actor.x<screen.width) and actor.x or screen.width
  122.                 actor.y = (actor.y>1) and actor.y or 1
  123.                 actor.y = (actor.y<screen.height) and actor.y or screen.height
  124.         end
  125.         screen.actors[round(actor.y)][round(actor.x)] = actor.char
  126.         return actor
  127. end
  128.  
  129. actor = {}
  130.  
  131. function actor:new(x,y,char,b)
  132.         actor.x = x
  133.         actor.y = y
  134.         actor.char = char
  135.         actor.bounded = (b~=nil) and b or true
  136.         screen.actors[round(y)][round(x)] = char
  137.         return format(actor)
  138. end
  139.  
  140. function actor:setPos(x,y)
  141.         screen.actors[actor.y][actor.x] = ' '
  142.         actor.x = x
  143.         actor.y = y
  144.         actor = format(actor)
  145. end
  146.  
  147. function actor:setX(x)
  148.         screen.actors[actor.y][actor.x] = ' '
  149.         actor.x = x
  150.         actor = format(actor)
  151. end
  152.  
  153. function actor:setY(y)
  154.         screen.actors[actor.y][actor.x] = ' '
  155.         actor.y = y
  156.         actor = format(actor)
  157. end
  158.  
  159. function actor:addPos(x,y)
  160.         screen.actors[actor.y][actor.x] = ' '
  161.         actor.x = actor.x + x
  162.         actor.y = actor.y + y
  163.         actor = format(actor)
  164. end
  165.  
  166. function actor:addX(x)
  167.         screen.actors[actor.y][actor.x] = ' '
  168.         actor.x = actor.x + x
  169.         actor = format(actor)
  170. end
  171.  
  172. function actor:addY(y)
  173.         screen.actors[actor.y][actor.x] = ' '
  174.         actor.y = actor.y + y
  175.         actor = format(actor)
  176. end
  177.  
  178. function actor:setChar(char)
  179.         screen.actors[actor.y][actor.x] = char
  180.         actor.char = char
  181. end
  182.  
  183. function actor:destroy()
  184.         screen.actors[actor.y][actor.x] = ' '
  185.         actor = nil
  186. end
  187.  
  188. game = {}
  189. game.state = 'running'
  190.  
  191. function game.quit()
  192.         term.clear()
  193.         game.state = 'dead'
  194. end
  195.  
  196. function game.start()
  197.         os.startTimer(1/30)
  198.         while game.state == 'running' do
  199.                 local t,k = os.pullEvent()
  200.                 if t == 'timer' then
  201.                         draw()
  202.                         if game.update ~= nil then game.update() end
  203.                         os.startTimer(1/30)
  204.                 elseif t == 'key' then
  205.                         if k == 14 then
  206.                                 game.quit()
  207.                         else
  208.                                 if game.key ~= nil then game.key(k) end
  209.                         end
  210.                 end
  211.         end
  212. end