Alyssa

Tetris

Jul 31st, 2013
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.59 KB | None | 0 0
  1. term.clear() -- clear the screen before we start
  2. foundblock = false -- Just to be sure we haven't found the block
  3. width, height = term.getSize() -- We need to know the size of your computer screen
  4. blocks = {} -- No blocks yet, creating the table.
  5. blocksy = {} -- Same as above
  6. blocksy2 = {} -- Same as above
  7. blocksy3 = {} -- Same as Above
  8. blocksy4 = {} -- Same as Above
  9. blocksx = {} -- same as above
  10. blocksx2 = {} -- same as above
  11. blocksx3 = {} -- same as... above!
  12. blocksx4 = {} -- Same as above
  13. blocksshape = {} -- Same as above
  14. rotation = {} -- same as the above
  15. blockscreated = 0 -- We haven't created any blocks yet
  16. debug = true -- As this is not finished, I need to debug the program
  17.  
  18. function align() -- This function is so big, Im not going to bother to explain it.
  19.  for a = 1, blockscreated do
  20.   if blocksshape[blockscreated] == "L" then
  21.    if rotation[blockscreated] == 1 then
  22.     blocksy2[blockscreated] = blocksy[blockscreated]
  23.     blocksy3[blockscreated] = blocksy[blockscreated] -1
  24.     blocksy4[blockscreated] = blocksy[blockscreated] -2
  25.     blocksx2[blockscreated] = blocksx[blockscreated] +1
  26.     blocksx3[blockscreated] = blocksx[blockscreated]
  27.     blocksx4[blockscreated] = blocksx[blockscreated]
  28.    elseif rotation[blockscreated] == 2 then
  29.     blocksy2[blockscreated] = blocksy[blockscreated] -1
  30.     blocksy3[blockscreated] = blocksy[blockscreated]
  31.     blocksy4[blockscreated] = blocksy[blockscreated]
  32.     blocksx2[blockscreated] = blocksx[blockscreated]
  33.     blocksx3[blockscreated] = blocksx[blockscreated] -1
  34.     blocksx4[blockscreated] = blocksx[blockscreated] -2
  35.    elseif rotation[blockscreated] == 3 then
  36.     blocksy2[blockscreated] = blocksy[blockscreated]
  37.     blocksy3[blockscreated] = blocksy[blockscreated] +1
  38.     blocksy4[blockscreated] = blocksy[blockscreated] +2
  39.     blocksx2[blockscreated] = blocksx[blockscreated] +1
  40.     blocksx3[blockscreated] = blocksx[blockscreated]
  41.     blocksx4[blockscreated] = blocksx[blockscreated]
  42.    elseif rotation[blockscreated] == 4 then
  43.     blocksy2[blockscreated] = blocksy[blockscreated] +1
  44.     blocksy3[blockscreated] = blocksy[blockscreated]
  45.     blocksy4[blockscreated] = blocksy[blockscreated]
  46.     blocksx2[blockscreated] = blocksx[blockscreated]
  47.     blocksx3[blockscreated] = blocksx[blockscreated] +1
  48.     blocksx4[blockscreated] = blocksx[blockscreated] +2
  49.    end
  50.   end
  51.  end
  52. end
  53.  
  54. function newblock() -- Create a new block
  55.  foundblock = false -- We haven't found an uncreated block yet
  56.  i = 0 -- We haven't started looking for a block yet, so make sure it knows that
  57.  while not foundblock == true do -- Keep doing this until we find a block we haven't created yet
  58.   i = i+1 -- Let search for a different block
  59.   if blocks[i] == true then -- This block isn't new!
  60.    --Do nothing
  61.   else -- It is new!
  62.    blocks[i] = true --Lets make sure we record the new block
  63.    newblocknum = i -- For later use
  64.    blockscreated = i -- Just so we know how many blocks we have created
  65.    foundblock = true -- We found the block!
  66.   end -- End that if statement
  67.  end -- End the while loop
  68.  shape = math.random(1, 7) -- Randomly set the shape
  69.  if shape == 1 then -- Define the blocks shape
  70.   shape = "L" -- Make it shaped Like an L
  71.  elseif shape == 2 then -- If its not an L, then maybe its a...
  72.   shape = "J" -- Backwards L
  73.  elseif shape == 3 then -- Guess not, maybe its a
  74.   shape = "Z" -- Z, or in our case |## |
  75.  elseif shape == 4 then--          | ##| If its not a z, then
  76.   shape = "S" -- Its a backwards z!
  77.  elseif shape == 5 then -- Guess not, its probably a
  78.   shape = "T" -- T
  79.  elseif shape == 6 then -- Or not, Probably a
  80.   shape = "O" -- Square
  81.  elseif shape == 7 then -- Lastly, it must be a
  82.   shape = "I" -- Square
  83.  end -- End the shape defining if statement
  84.  blocksshape[newblocknum] = shape -- Make the block have a shape
  85.  blocksx[newblocknum] = 5 --Set the block in the middle
  86.  blocksy[newblocknum] = 3 --Set the block high up
  87. end -- End the function
  88.  
  89. function blockfall() --Makes all blocks fall
  90.  for b = 1, blockscreated do --Go through all the blocks
  91.   if blocksshape[b] then -- Too lazy to do anything with this yet
  92.    if blocksy[b] == 19 then --Only drop the block if not touching the bottom
  93.     --do nothing
  94.    else -- Do something!
  95.     if blocksy[b] then
  96.      blocksy[b] = blocksy[b] +1 --Make the selected block fall 1 space, currently does not collide
  97.     end -- End the if statement
  98.    end -- End the if statement
  99.   end -- End the if statement
  100.  end -- End the for statement
  101. end -- End the function
  102.  
  103. function draw() --Draw the screen
  104.  term.clear()
  105.  for z = 1, blockscreated do --Go through all the blocks
  106.   if blocksshape[z] == "L" then -- Is the block L-shaped?
  107.    term.setTextColor(colors.red) -- L blocks are now red
  108.    term.setCursorPos(blocksx[z], blocksy[z])  --All Of
  109.    write("#")                                 -- This
  110.    term.setCursorPos(blocksx4[z], blocksy2[z])
  111.    write("3")
  112.    term.setCursorPos(blocksx2[z], blocksy2[z])-- Draws
  113.    write("#")                                 -- The
  114.    term.setCursorPos(blocksx3[z], blocksy3[z])-- L
  115.    write("#")                                 -- Block
  116.   elseif blocksshape[z] == "J" then -- Is the block J-shaped?
  117.    term.setTextColor(colors.blue) -- J blocks are now blue
  118.    term.setCursorPos(blocksx[z]-1, blocksy[z])--All Of
  119.    write("##")                                -- This
  120.    term.setCursorPos(blocksx[z], blocksy[z]-1)-- Draws
  121.    write("#")                                 -- The
  122.    term.setCursorPos(blocksx[z], blocksy[z]-1)-- J
  123.    write("#")                                 -- Block
  124.   elseif blocksshape[z] == "Z" then -- Is the block Z-shaped?
  125.    term.setTextColor(colors.pink) -- Z blocks are now pink
  126.    term.setCursorPos(blocksx[z], blocksy[z])    --All Of
  127.    write("##")                                  -- This Draws
  128.    term.setCursorPos(blocksx[z]-1, blocksy[z]-1)-- The Z
  129.    write("##")                                  -- Block
  130.   elseif blocksshape[z] == "S" then -- Is the block S-shaped?
  131.    term.setTextColor(colors.orange) -- S blocks are now orange
  132.    term.setCursorPos(blocksx[z]-1, blocksy[z])  -- All Of
  133.    write("##")                                  -- This Draws
  134.    term.setCursorPos(blocksx[z], blocksy[z]-1)  -- The Z
  135.    write("##")                                  -- Block
  136.   elseif blocksshape[z] == "I" then -- Is the block I-shaped?
  137.    term.setTextColor(colors.lime) -- I blocks are now green
  138.    term.setCursorPos(blocksx[z], blocksy[z])  -- All
  139.    write("#")                                 -- of
  140.    term.setCursorPos(blocksx[z], blocksy[z]-1)-- This
  141.    write("#")                                 -- Draws
  142.    term.setCursorPos(blocksx[z], blocksy[z]-2)-- The
  143.    write("#")                                 -- I
  144.    term.setCursorPos(blocksx[z], blocksy[z]-3)-- Block
  145.    write("#")
  146.   elseif blocksshape[z] == "O" then -- Is the block O-shaped?
  147.    term.setTextColor(colors.cyan) -- O blocks are now cyan
  148.    term.setCursorPos(blocksx[z], blocksy[z])  -- All Of
  149.    write("##")                                -- This Draws
  150.    term.setCursorPos(blocksx[z], blocksy[z]-1)-- The
  151.    write("##")                                -- O block
  152.   elseif blocksshape[z] == "T" then -- Is the block T-shaped?
  153.    term.setTextColor(colors.purple) -- T blocks are now purple
  154.    term.setCursorPos(blocksx[z],blocksy[z])     -- All of
  155.    write("#")                                   -- This draws
  156.    term.setCursorPos(blocksx[z]-1, blocksy[z]-1)-- The
  157.    write("###")                                 -- T block
  158.   end --End the if statement
  159.  end --End the For loop
  160. end -- End the function
  161.  
  162. if debug == true then -- Lets debug the program.
  163.  align()
  164.  newblock() -- create the first block
  165.  wait = 0 --Create the variable for later
  166.  while true do -- Debug forever!
  167.   draw() -- Draw the new area
  168.   os.startTimer(0.3) -- Start a timer in case the player doesn't do anything
  169.   oldtime = os.clock() -- Start!
  170.   args = {os.pullEvent()} -- Rednet will mess this up, will fix that later
  171.   newtime = os.clock() -- How long did the event take to happen
  172.   oldtime = tonumber(oldtime) -- Just in case
  173.   newtime = tonumber(newtime) -- Just in case
  174.   time = newtime - oldtime --Calculate how long that took
  175.   time = tonumber(time) -- Just in case
  176.   if time == 0.35 then -- Boring
  177.    sleept = 0 -- Boring
  178.   elseif time == 0.30 then  -- Boring
  179.    sleept = 0.05  -- Boring
  180.   elseif time == 0.25 then -- Boring
  181.    sleept = 0.10 -- Boring
  182.   elseif time == 0.20 then -- Boring
  183.    sleept = 0.15 -- Boring
  184.   elseif time == 0.15 then -- Boring
  185.    sleept = 0.20 -- Boring
  186.   elseif time == 0.10 then -- Boring
  187.    sleept = 0.25 -- Boring
  188.   elseif time == 0.5 then -- Boring
  189.    sleept = 0.3 -- Boring
  190.   elseif time == 0 then -- Boring
  191.    sleept = 0.30 -- Boring
  192.   end --End the boring if statement.
  193.   sleept = tonumber(sleept) -- Just in case
  194.   if args[2] == 208 and args[1] == "key" and blocks[blockscreated] then -- move piece down
  195.    if blocksy[blockscreated] >= 17 then -- Make sure its not near the bottom
  196.     --do nothing
  197.    elseif blocksy == 16 then -- If its close, but not too close
  198.     blocksy[blockscreated] = blocksy[blockscreated] +1 -- Go down 1
  199.    else -- If its higher up then that,
  200.     blocksy[blockscreated] = blocksy[blockscreated] +2 -- go down 2
  201.    end -- End the if statement
  202.   end -- End the if statement
  203.   if args[2] == 205 and args[1] == "key" then -- move piece right
  204.    if blocksx[blockscreated] == 10 then -- leave room for edge, score, level, etc.    
  205.     -- do nothing
  206.    else -- Do something
  207.     if blocks[blockscreated] == true then -- make sure it exist to move it
  208.      blocksx[blockscreated] = blocksx[blockscreated] +1 -- Move it right
  209.      draw() --redraw
  210.     end -- end if statement
  211.    end -- end the if statement
  212.   end -- end if statement
  213.   if args[1] == "key" and args[2] == 203 then -- move piece left
  214.    if blocksx[blockscreated] == 2 then -- Leave room for edge
  215.     -- do nothing
  216.    else -- Do something
  217.     if blocks[blockscreated] == true and args[2] == 203 then -- make sure it exist to move it
  218.      blocksx[blockscreated] = blocksx[blockscreated] -1 -- move it left
  219.      draw() --redraw
  220.     end -- end the if statement
  221.    end -- end if statement
  222.   end -- end if statement
  223.  
  224.   if sleept then -- It said it couldn't find a number somewhere, was it here?
  225.    sleep(sleept) -- I try to pause .3 of a second every loop
  226.   end -- End that if statement
  227.   if blocksy[blockscreated] == 19 then -- Make sure it has reached the bottom
  228.    newblock() -- it reached the bottom, time for a new block.
  229.   else -- Not statements keep messing up
  230.    blockfall() -- Make the blocks fall
  231.   end -- End if statement
  232.  end -- End if statement
  233. end -- End while loop
Advertisement
Add Comment
Please, Sign In to add comment