Advertisement
RobotBubble

Stackerz

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