Advertisement
HyperQ

Simon Says - ComputerCraft

Mar 12th, 2013
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.21 KB | None | 0 0
  1. local Seq = {}
  2. local score = 0
  3. local function getColourOf(hex)
  4.         local value = tonumber(hex, 16)
  5.         if not value then return nil end
  6.         value = math.pow(2,value)
  7.         return value
  8. end
  9.  
  10. local function drawPictureTable(image, xinit, yinit)
  11.         for y=1,#image do
  12.                 for x=1,#image[y] do
  13.                         term.setCursorPos(xinit + x-1, yinit + y-1)
  14.                         local col = getColourOf(string.sub(image[y], x, x))
  15.                         if col ~= nil then
  16.                             term.setBackgroundColour(col)
  17.                             term.write(" ")
  18.                         end
  19.                 end
  20.         end
  21. end
  22. local function drawGreen() drawPictureTable({"ffffffffffffffffffffffffff","f555555555555555555555555f","f555555555555555555555555f","f555555555555555555555555f","f555555555555555555555555f","f555555555555555555555ffff","f55555555555555555555ff","f5555555555555555555ff","f5555555555555555555f","fffffffffffffffffffff"},1,1) end
  23. local function drawGreen2() drawPictureTable({"ffffffffffffffffffffffffff","f888888888888588558888888f","f555555555555585588555555f","f888888888888885885588888f","f555555555555555855885555f","f888888888888888858855ffff","f55555555555555555855ff","f8888888888888888885ff","f5555555555555555555f","fffffffffffffffffffff"},1,1) end
  24.  
  25. local function drawRed() drawPictureTable({"ffffffffffffffffffffffff","eeeeeeeeeeeeeeeeeeeeeeeef","eeeeeeeeeeeeeeeeeeeeeeeef","eeeeeeeeeeeeeeeeeeeeeeeef","eeeeeeeeeeeeeeeeeeeeeeeef","fffeeeeeeeeeeeeeeeeeeeeef","  ffeeeeeeeeeeeeeeeeeeeef","   ffeeeeeeeeeeeeeeeeeeef","    feeeeeeeeeeeeeeeeeeef","    fffffffffffffffffffff"},27,1) end
  26. local function drawRed2() drawPictureTable({"ffffffffffffffffffffffff","8888888ee88e888888888888f","eeeeee88ee8eeeeeeeeeeeeef","88888ee88e88888888888888f","eeee88ee8eeeeeeeeeeeeeeef","fffee88e8888888888888888f","  ffee8eeeeeeeeeeeeeeeeef","   ffe888888888888888888f","    feeeeeeeeeeeeeeeeeeef","    fffffffffffffffffffff"},27,1) end
  27.  
  28. local function drawYellow() drawPictureTable({"f4444444444444444444f","f4444444444444444444ff","f44444444444444444444ff","f444444444444444444444ffff","f444444444444444444444444f","f444444444444444444444444f","f444444444444444444444444f","f444444444444444444444444f","ffffffffffffffffffffffffff"},1,11) end
  29. local function drawYellow2() drawPictureTable({"f4444444444444444444f","f8888888888888888884ff","f44444444444444444844ff","f888888888888888848844ffff","f444444444444444844884444f","f888888888888884884488888f","f444444444444484488444444f","f888888888888488448888888f","ffffffffffffffffffffffffff"},1,11) end
  30.  
  31. local function drawBlue() drawPictureTable({"    f3333333333333333333f","   ff3333333333333333333f","  ff33333333333333333333f","fff333333333333333333333f","333333333333333333333333f","333333333333333333333333f","333333333333333333333333f","333333333333333333333333f","fffffffffffffffffffffffff"},27,11) end
  32. local function drawBlue2() drawPictureTable({"    f3333333333333333333f","   ff3888888888888888888f","  ff33833333333333333333f","fff338838888888888888888f","333388338333333333333333f","888883388388888888888888f","333333883383333333333333f","888888833883888888888888f","fffffffffffffffffffffffff"},27,11) end
  33. local function drawCenter() drawPictureTable({"     00000","    0000000","   000000000","   000000000","   000000000","    0000000","     00000"},19,7) end
  34.  
  35. function getSelectedColor(x,y)
  36.     if (x >= 2 and x <= 20 and y <= 9 and y >= 2) or (x == 21 and y <= 7 and y >= 2) or (x == 22 and y <= 6 and y >= 2) or (x >= 23 and x <= 25 and y <= 5 and y >= 2) then return 1 -- Green Section
  37.     elseif (x >= 27 and x <= 29 and y <= 5 and y >= 2) or (x == 30 and y <= 6 and y >= 2) or (x == 31 and y <= 7 and y >= 2) or (x >= 32 and x <= 50 and y <= 9 and y >= 2) then return 2 -- Red Sections
  38.     elseif (x >= 2 and x <= 20 and y <= 18 and y >= 11) or (x == 21 and y <= 18 and y >= 13) or (x == 22 and y <= 18 and y >= 14) or (x >= 23 and x <= 25 and y <= 18 and y >= 15) then return 3 -- Yellow Section
  39.     elseif (x >= 27 and x <= 29 and y <= 18 and y >= 15) or (x == 30 and y <= 18 and y >= 14) or (x == 31 and y <= 18 and y >= 13) or (x >= 32 and x <= 50 and y <= 18 and y >= 11) then return 4 -- Blue Section
  40.     elseif x == 51 and y == 1 then return 5
  41.     else return 0 end
  42. end
  43. function LightColor(num)
  44.     if num == 1 then
  45.         sleep(.5)
  46.         drawGreen2()
  47.         sleep(.5)
  48.         drawGreen()
  49.     elseif num == 2 then
  50.         sleep(.5)
  51.         drawRed2()
  52.         sleep(.5)
  53.         drawRed()
  54.     elseif num == 3 then
  55.         sleep(.5)
  56.         drawYellow2()
  57.         sleep(.5)
  58.         drawYellow()
  59.     elseif num == 4 then
  60.         sleep(.5)
  61.         drawBlue2()
  62.         sleep(.5)
  63.         drawBlue()
  64.     end
  65. end
  66.  
  67. function playThrough()
  68.     for i=1,#Seq do
  69.         local num = Seq[i]
  70.         LightColor(num)
  71.     end
  72. end
  73.  
  74. local function quit()
  75.     term.setBackgroundColor(colors.black)
  76.     term.setTextColor(colors.yellow)
  77.     term.clear()
  78.     term.setCursorPos(1,1)
  79.     error()
  80. end
  81.  
  82. function PlayGame()
  83.     while GameOver ~=true do
  84.         x=1
  85.         table.insert(Seq,math.random(1,4))
  86.         score = #Seq-1
  87.         drawCenter()
  88.         term.setBackgroundColor(colors.white)
  89.         term.setTextColor(colors.black)
  90.         term.setCursorPos(27-(string.len("Score:")/2),9)
  91.         print("Score:")
  92.         term.setCursorPos(26,11)
  93.         print(tostring(score))
  94.         playThrough()
  95.         while x <=#Seq do
  96.             local event,p1,p2,p3 = os.pullEvent()
  97.             if event == "mouse_click" then
  98.                 local result = getSelectedColor(p2,p3)
  99.                 if result == Seq[x] then x=x+1
  100.                 elseif result == 0 then
  101.                 elseif result == 5 then quit()
  102.                 else GameOver = true;break end
  103.             end
  104.         end
  105.     end
  106.     drawCenter()
  107.     term.setCursorPos(27-(string.len("Score:")/2),12)
  108.     print("Score:")
  109.     term.setCursorPos(26,13)
  110.     print(tostring(score))
  111.     GameOver= false
  112.     Seq = {}
  113. end
  114.  
  115. drawGreen();drawRed();drawYellow();drawBlue();drawCenter()
  116. term.setCursorPos(51,1)
  117. term.setBackgroundColor(colors.black)
  118. term.setTextColor(colors.white)
  119. write("X")
  120.  
  121. while true do
  122.     term.setBackgroundColor(colors.lightBlue)
  123.     term.setTextColor(colors.black)
  124.     local w,h = term.getSize()
  125.     term.setCursorPos((w/2)-2,h/2+1)
  126.     print(" Play! ")
  127.     while true do
  128.         local event,p1,p2,p3 = os.pullEvent()
  129.         if event == "mouse_click" and p1 == 1 then
  130.             if p2 >= (w/2)-2 and p2 <= (w/2)+4 and p3 == 10 then
  131.                 PlayGame()
  132.                 break
  133.             elseif p2 == 51 and p3 == 1 then
  134.                 quit()
  135.             end
  136.         end
  137.     end
  138. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement