King0fGamesYami

Stacker

Dec 18th, 2014
2,086
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local nSize, bNew, nBase, stack, maxx, maxy = 8, true, 0, {}, term.getSize()
  2.  
  3. stack[ 0 ] = { math.floor( maxx/2 ) - 4, math.floor( maxx/2 ) + 4, bReverse = false }
  4.  
  5. local function gameOver()
  6. term.clear()
  7. local str = "Game Over! Your Score: " .. #stack - 1
  8. term.setCursorPos( math.floor( (maxx - #str) / 2 ), maxy / 2 )
  9. print( str )
  10. term.setCursorPos( 1, 1 )
  11. error()
  12. end
  13.  
  14. local function renderStack()
  15. if #stack - nBase > maxy / 2 then
  16. nBase = nBase + 1
  17. end
  18. term.setBackgroundColor( colors.white )
  19. for i = 0, #stack do
  20. if i + nBase > #stack then
  21. break
  22. end
  23. term.setCursorPos( stack[ i + nBase ][ 1 ], maxy - i )
  24. term.write( string.rep( " ", stack[ i + nBase ][2] - stack[ i + nBase ][1] ) )
  25. end
  26. term.setBackgroundColor( colors.black )
  27. end
  28.  
  29. local function getInput()
  30. local new = bNew and { 1, nSize + 1, bReverse = false } or stack[ #stack ]
  31. local id = os.startTimer( 0.1 * (0.99^#stack) )
  32. while true do
  33. local event = { os.pullEvent() }
  34. if event[ 1 ] == "timer" and event[ 2 ] == id then
  35. if not new.bReverse then
  36. new[ 1 ] = new[ 1 ] + 1
  37. new[ 2 ] = new[ 2 ] + 1
  38. if new[ 2 ] > maxx then
  39. new.bReverse = true
  40. end
  41. else
  42. new[ 1 ] = new[ 1 ] - 1
  43. new[ 2 ] = new[ 2 ] - 1
  44. if new[ 1 ] <= 1 then
  45. new.bReverse = false
  46. end
  47. end
  48. if bNew then
  49. stack[ #stack + 1 ] = new
  50. bNew = false
  51. end
  52. break
  53. elseif event[ 1 ] == "key" and event[ 2 ] == keys.space then
  54. if new[ 1 ] < ( bNew and stack[ #stack ][ 1 ] or stack[ #stack - 1 ][ 1 ] ) then
  55. new[ 1 ] = ( bNew and stack[ #stack ][ 1 ] or stack[ #stack - 1 ][ 1 ] )
  56. end
  57. if new[ 2 ] > ( bNew and stack[ #stack ][ 2 ] or stack[ #stack - 1 ][ 2 ] ) then
  58. new[ 2 ] = ( bNew and stack[ #stack ][ 2 ] or stack[ #stack - 1 ][ 2 ] )
  59. end
  60. if new[ 2 ] - new[ 1 ] <= 0 then
  61. gameOver()
  62. end
  63. nSize = new[ 2 ] - new[ 1 ]
  64. if bNew then
  65. stack[ #stack + 1 ] = new
  66. else
  67. stack[ #stack ] = new
  68. bNew = true
  69. end
  70. break
  71. end
  72. end
  73. end
  74.  
  75. while true do
  76. term.clear()
  77. renderStack()
  78. getInput()
  79. end
RAW Paste Data