King0fGamesYami

[1.1]Jumping Game

Aug 2nd, 2015
1,740
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local playery, score, objects, maxx, maxy = 2, 0, {}, term.getSize()
  2. local objects = { {maxx - 2, math.random( 1, 4 )} }
  3.  
  4. local function newObject()
  5.     local last = objects[ #objects ]
  6.     if last[ 2 ] <= 2 then
  7.         objects[ #objects + 1 ] = {last[1] + math.random( 5, 10 ), math.random( 1, 4 )}
  8.     else
  9.         objects[ #objects + 1 ] = {last[1] + math.random( 15, 20 ), math.random( 1, 4 )}
  10.     end
  11. end
  12.  
  13. local function drawPlayer()
  14.   term.setCursorPos( 2, math.floor( maxy - playery ) )
  15.   term.setBackgroundColor( colors.blue )
  16.   term.setTextColor( colors.lightBlue )
  17.   term.write( "$" )
  18. end
  19.  
  20. local function drawField()
  21.   term.setTextColor( colors.lime )
  22.   term.setBackgroundColor( colors.green )
  23.   term.setCursorPos( 1, maxy - 1 )
  24.   term.write( string.rep( "=", maxx ) )
  25. end
  26.  
  27. local function drawScore()
  28.   term.setTextColor( colors.black )
  29.   term.setBackgroundColor( colors.white )
  30.   term.setCursorPos( 1, 1 )
  31.   term.write( score )
  32. end
  33.  
  34. local function drawObjects()
  35.   term.setBackgroundColor( colors.red )
  36.   for k, v in pairs( objects ) do
  37.     for i = 1, v[ 2 ] do
  38.       term.setCursorPos( v[1], maxy - i - 1 )
  39.       term.write( " " )
  40.     end
  41.   end
  42. end
  43.  
  44. local function gameOver()
  45.   local text = "GAME OVER"
  46.   term.setCursorPos( math.ceil( (maxx-#text)/2 ), maxy/2 )
  47.   term.write( text )
  48.   os.pullEvent( "key" )
  49.   term.setBackgroundColor( colors.black )
  50.   term.clear()
  51.   term.setCursorPos( 1, 1 )
  52.   error()
  53. end
  54.  
  55.  
  56. local function draw()
  57.   term.setBackgroundColor( colors.black )
  58.   for i = 2, 9 do
  59.     term.setCursorPos( 1, maxy - i )
  60.     term.clearLine()
  61.   end
  62.   drawObjects()
  63.   drawPlayer()
  64.   drawScore()
  65. end
  66.  
  67. term.setBackgroundColor( colors.black )
  68. term.clear()
  69. drawField()
  70.  
  71. repeat
  72.     newObject()
  73. until #objects == 5
  74.  
  75. draw()
  76. sleep( 1 )
  77. local id = os.startTimer( 0.1 )
  78. local isJumping = false
  79. local doubleJump = false
  80. local jumpValue = 0
  81.  
  82. while true do
  83.   local event = {os.pullEvent()}
  84.   if event[ 1 ] == "timer" and event[ 2 ] == id then
  85.     id = os.startTimer( 0.1 - (score/20000) )
  86.     score = score + 1
  87.     for k, v in ipairs( objects ) do
  88.       objects[ k ][ 1 ] = v[ 1 ] - 1
  89.       if objects[ k ][1] < 1 then
  90.         table.remove( objects, k )
  91.         newObject()
  92.       elseif objects[ k ][ 1 ] == 2 then
  93.         if math.ceil( playery - 1 ) <= objects[ k ][ 2 ] then
  94.           draw()
  95.           gameOver()
  96.         end
  97.       end
  98.     end
  99.     if isJumping then
  100.       playery = playery + jumpValue
  101.       jumpValue = jumpValue * 0.7
  102.     end
  103.     playery = math.max( 2, playery - 1 )
  104.     if playery < 2.5 then
  105.       jumpValue = 2
  106.       isJumping = false
  107.       doubleJump = false
  108.     end
  109.     draw()
  110.   elseif event[ 1 ] == "key" and event[ 2 ] == keys.space then
  111.     if isJumping and not doubleJump then
  112.       doubleJump = true
  113.       jumpValue = jumpValue + 1.5
  114.     elseif not isJumping then
  115.       isJumping = true
  116.       jumpValue = 2.5
  117.     end
  118.   end
  119. end
RAW Paste Data