Quipex

Disk/mem/redworks/libs/GameAPI

Apr 23rd, 2012
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. term.clear()
  2. print [[GameAPI r4
  3. Press Backspace at any time to exit the game.]]
  4. sleep(1)
  5. screen = {}
  6. screen.width = 40
  7. screen.height = 15
  8. screen.actors = {}
  9.  
  10. function round(v,int)
  11. local int = (int~=nil) and int or 1
  12. local n = v%int
  13.  
  14. if n >= int/2 then
  15. return (v-n)+int
  16. else
  17. return (v-n)
  18. end
  19. end
  20.  
  21. for y=1,screen.height do
  22. local t = {}
  23. for x=1,screen.width do
  24. table.insert(t,' ')
  25. end
  26. table.insert(screen.actors,t)
  27. end
  28.  
  29. function draw()
  30. term.clear()
  31. for y,v in pairs(screen.actors) do
  32. local line = ''
  33. for x,vv in pairs(v) do
  34. line = line .. vv
  35. end
  36. print(line)
  37. end
  38. end
  39.  
  40. function format(actor)
  41. if actor.bounded then
  42. actor.x = (actor.x>1) and actor.x or 1
  43. actor.x = (actor.x<screen.width) and actor.x or screen.width
  44. actor.y = (actor.y>1) and actor.y or 1
  45. actor.y = (actor.y<screen.height) and actor.y or screen.height
  46. end
  47. screen.actors[round(actor.y)][round(actor.x)] = actor.char
  48. return actor
  49. end
  50.  
  51. actor = {}
  52.  
  53. function actor:new(x,y,char,b)
  54. actor.x = x
  55. actor.y = y
  56. actor.char = char
  57. actor.bounded = (b~=nil) and b or true
  58. screen.actors[round(y)][round(x)] = char
  59. return format(actor)
  60. end
  61.  
  62. function actor:setPos(x,y)
  63. screen.actors[actor.y][actor.x] = ' '
  64. actor.x = x
  65. actor.y = y
  66. actor = format(actor)
  67. end
  68.  
  69. function actor:setX(x)
  70. screen.actors[actor.y][actor.x] = ' '
  71. actor.x = x
  72. actor = format(actor)
  73. end
  74.  
  75. function actor:setY(y)
  76. screen.actors[actor.y][actor.x] = ' '
  77. actor.y = y
  78. actor = format(actor)
  79. end
  80.  
  81. function actor:addPos(x,y)
  82. screen.actors[actor.y][actor.x] = ' '
  83. actor.x = actor.x + x
  84. actor.y = actor.y + y
  85. actor = format(actor)
  86. end
  87.  
  88. function actor:addX(x)
  89. screen.actors[actor.y][actor.x] = ' '
  90. actor.x = actor.x + x
  91. actor = format(actor)
  92. end
  93.  
  94. function actor:addY(y)
  95. screen.actors[actor.y][actor.x] = ' '
  96. actor.y = actor.y + y
  97. actor = format(actor)
  98. end
  99.  
  100. function actor:setChar(char)
  101. screen.actors[actor.y][actor.x] = char
  102. actor.char = char
  103. end
  104.  
  105. function actor:destroy()
  106. screen.actors[actor.y][actor.x] = ' '
  107. actor = nil
  108. end
  109.  
  110. game = {}
  111. game.state = 'running'
  112.  
  113. function game.quit()
  114. term.clear()
  115. game.state = 'dead'
  116. end
  117.  
  118. function game.start()
  119. os.startTimer(1/30)
  120. while game.state == 'running' do
  121. local t,k = os.pullEvent()
  122. if t == 'timer' then
  123. draw()
  124. if game.update ~= nil then game.update() end
  125. os.startTimer(1/30)
  126. elseif t == 'key' then
  127. if k == 14 then
  128. game.quit()
  129. else
  130. if game.key ~= nil then game.key(k) end
  131. end
  132. end
  133. end
  134. end
  135.  
  136. --put the code for your game down here since i can't figure out how to use "require" and "loadfile" is a bitch
  137.  
  138. --[[
  139. Brief Explanation of Functions:
  140.  
  141. actor:new(x, y, char[, bounded])
  142. returns a new actor at the x and y positions on the terminal
  143. bounded is a boolean which will determine whether or not your actor's movement is limited to the console window, true by default.
  144.  
  145. actor.x
  146. the actor's x position
  147.  
  148. actor.y
  149. the actor's y position
  150.  
  151. actor.char
  152. the character representing the actor on screen
  153.  
  154. actor.bounded
  155. whether or not the actor can go outside the screen
  156.  
  157. actor:setPos(x,y)
  158. sets the actor's position
  159. **this method and other positioning methods must be used in order for the actor's position to be updated onscreen
  160.  
  161. actor:addPos(x,y)
  162. adds to the actor's position
  163.  
  164. actor:setX(n)
  165. sets the actor's x position to n
  166.  
  167. actor:setY(n)
  168. sets the actor's y position to n
  169.  
  170. actor:addX(n)
  171. adds n to the actor's x position
  172.  
  173. actor:addY(n)
  174. adds n to the actor's y position
  175.  
  176. actor:setChar(char)
  177. change the actor's character to char
  178.  
  179. game.key(k)
  180. user defined, triggered when a key is pressed
  181.  
  182. game.update()
  183. user defined, triggered when a new frame is drawn
  184.  
  185. game.start()
  186. required for the game to start, obviously
  187. **any code after this function will not be ran until the game has stopped
  188.  
  189. game.stop()
  190. quit the program
  191. ]]
  192.  
  193. --[[
  194. Changelog:
  195.  
  196. r1:
  197. Released API
  198.  
  199. r4:
  200. Added 'game:setChar()' to change an actor's characters
  201. Added a 'bounded' attribute if you wanted an actor to be able to go outside the screen.
  202. Added 'screen.width' and 'screen.height' as editable attributes to allow for custom widths and heights
  203. ]]
Advertisement
Add Comment
Please, Sign In to add comment