Advertisement
RodrickLord

Falling Game

Aug 1st, 2015
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.85 KB | None | 0 0
  1. local sx, sy = term.getSize()
  2. local cx, cy = sx/2+1, sy/2+1
  3. local gx, gy = 20, sy-1
  4.  
  5. local gameWindow
  6. local spaceWidth = 3
  7. local playerY = 3
  8. local emptyY = 3
  9. local playerSymbol = "&"
  10.  
  11. local function randomObstacle( y )
  12.   local x = math.random( 2, gx-1-spaceWidth )
  13.   term.setCursorPos( 1, y )
  14.   term.setBackgroundColor( colors.white )
  15.   term.clearLine()
  16.   paintutils.drawLine( x, y, x + spaceWidth, y, colors.black )
  17.   return x
  18. end
  19.  
  20. local function play()
  21.   term.setBackgroundColor(colors.gray)
  22.   term.clear()
  23.  
  24.   gameWindow = window.create( term.current(), cx-gx/2, 2, gx, gy )
  25.   term.redirect(gameWindow)
  26.   term.clear()
  27.  
  28.   local playerX = gx/2
  29.   parallel.waitForAny( function()
  30.     local i = -1
  31.     local spaces = {}
  32.     while true do
  33.       while #spaces>7 do
  34.         spaces[#spaces] = nil
  35.       end
  36.       i = i<emptyY and i+1 or 0
  37.       if i == 0 then
  38.         table.insert( spaces, 1, randomObstacle( gy ) )
  39.       elseif i == 3 and #spaces > 3 and ( playerX < spaces[4] or playerX > spaces[4] + spaceWidth ) then
  40.         term.setBackgroundColor(colors.red)
  41.         term.setTextColor(colors.red)
  42.         term.setCursorPos( playerX, playerY )
  43.         term.write " "
  44.         return
  45.       end
  46.       sleep(.5)
  47.       term.setCursorPos( playerX, playerY )
  48.       term.write " "
  49.       gameWindow.scroll( 1 )
  50.       term.setCursorPos( playerX, playerY )
  51.      
  52.       term.write "&"
  53.     end
  54.   end, function()
  55.     while true do
  56.       local _, key = os.pullEvent "key"
  57.       if key == keys.a and playerW ~= 1 then
  58.         playerX = playerX-1
  59.         term.setCursorPos( playerX, playerY )
  60.         term.write( playerSymbol .. " " )
  61.       elseif key == keys.d and playerW ~= gx then
  62.         playerX = playerX+1
  63.         term.setCursorPos( playerX-1, playerY )
  64.         term.write( " " .. playerSymbol )
  65.       end
  66.     end
  67.   end )
  68. end
  69.  
  70. play()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement