Advertisement
Henness

Fogger

Dec 20th, 2012
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.66 KB | None | 0 0
  1. -- Frogger by Henness
  2. -- Version 1.0 12/19/2012
  3.  
  4. -- Config
  5. screenw,screenh = term.getSize()
  6. colors = {
  7.     ["0"] = 1,
  8.     ["1"] = 2,
  9.     ["2"] = 4,
  10.     ["3"] = 8,
  11.     ["4"] = 16,
  12.     ["5"] = 32,
  13.     ["6"] = 64,
  14.     ["7"] = 128,
  15.     ["8"] = 256,
  16.     ["9"] = 512,
  17.     ["a"] = 1024,
  18.     ["b"] = 2048,
  19.     ["c"] = 4096,
  20.     ["d"] = 8192,
  21.     ["e"] = 16384,
  22.     ["f"] = 32768
  23. }
  24. terrain = {
  25.     [1] =  {"5","5","5","5","5","5","5","5","5","5","5","5","5"},
  26.     [2] =  {"b","5","5","b","5","5","b","5","5","b","5","5","b"},
  27.     [3] =  {"b","b","b","b","b","b","b","b","b","b","b","b","b"},
  28.     [4] =  {"b","b","b","b","b","b","b","b","b","b","b","b","b"},
  29.     [5] =  {"b","b","b","b","b","b","b","b","b","b","b","b","b"},
  30.     [6] =  {"b","b","b","b","b","b","b","b","b","b","b","b","b"},
  31.     [7] =  {"a","a","a","a","a","a","a","a","a","a","a","a","a"},
  32.     [8] =  {"f","f","f","f","f","f","f","f","f","f","f","f","f"},
  33.     [9] =  {"f","f","f","f","f","f","f","f","f","f","f","f","f"},
  34.     [10] = {"f","f","f","f","f","f","f","f","f","f","f","f","f"},
  35.     [11] = {"f","f","f","f","f","f","f","f","f","f","f","f","f"},
  36.     [12] = {"a","a","a","a","a","a","a","a","a","a","a","a","a"},
  37.     [13] = {"f","f","f","f","f","f","f","f","f","f","f","f","f"}
  38. }
  39. terrainw = #terrain[1]
  40. terrainh = #terrain
  41. woffset = (screenw/2)-(terrainw/2)
  42. hoffset = 2
  43. objects = {
  44.     [1] =  {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
  45.     [2] =  {0,1,1,0,1,1,0,1,1,0,1,1,0,0,0,0,0},
  46.     [3] =  {0,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0},
  47.     [4] =  {0,0,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1},
  48.     [5] =  {0,0,0,1,1,1,1,0,0,0,1,1,1,1,0,0,0},
  49.     [6] =  {0,0,0,1,0,0,0,1,0,0,0,1,1,1,1,1,1},
  50.     [7] =  {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  51.     [8] =  {1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0},
  52.     [9] =  {1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0},
  53.     [10] = {1,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0},
  54.     [11] = {1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0},
  55.     [12] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  56.     [13] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  57.     [14] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
  58. }
  59. locations = {
  60.     [1] =  {},
  61.     [2] =  {},
  62.     [3] =  {2,3,4,5,6,10,11,12,13,14,15},
  63.     [4] =  {3,4,5,8,9,10,11,12,13,14,15,16,17},
  64.     [5] =  {4,5,6,7,11,12,13,14},
  65.     [6] =  {4,8,12,13,14,15,16,17},
  66.     [7] =  {},
  67.     [8] =  {1,8},
  68.     [9] =  {1,8},
  69.     [10] = {1,7,12},
  70.     [11] = {1,6,11},
  71.     [12] = {},
  72.     [13] = {}
  73. }
  74. turtlelevel = 3
  75. death = false
  76.  
  77. -- Functions
  78. function printCentered(str, ypos)
  79.     term.setCursorPos(screenw/2 - #str/2, ypos)
  80.     term.write(str)
  81. end
  82.  
  83. function saveTable(table,name)
  84.     local file = fs.open(name,"w")
  85.     file.write(textutils.serialize(table))
  86.     file.close()
  87. end
  88.  
  89. function returnColor(str)
  90.     return colors[str]
  91. end
  92.  
  93. function resetFrog()
  94.     death = false
  95.     posx = 7
  96.     posy = 12
  97.     face = 0
  98. end
  99.  
  100. function moveOjects()
  101.     if math.fmod(gametime, 2) == 0 then -- Logs 1
  102.         if objects[posy][posx] == 0  and posy == 3 and not death then
  103.             posx = posx + 1
  104.         end
  105.         for key,value in pairs(locations[3]) do
  106.             objects[3][value] = 0
  107.             if value < #objects[3] then
  108.                 locations[3][key] = locations[3][key] + 1
  109.             else
  110.                 locations[3][key] = 1
  111.             end
  112.         end
  113.         for key,value in pairs(locations[3]) do
  114.             objects[3][value] = 1
  115.         end
  116.     end
  117.     if math.fmod(gametime, 0.5) == 0 then -- Logs 2
  118.         if objects[posy][posx] == 0  and posy == 5 and not death then
  119.             posx = posx + 1
  120.         end
  121.         for key,value in pairs(locations[5]) do
  122.             objects[5][value] = 0
  123.             if value < #objects[5] then
  124.                 locations[5][key] = locations[5][key] + 1
  125.             else
  126.                 locations[5][key] = 1
  127.             end
  128.         end
  129.         for key,value in pairs(locations[5]) do
  130.             objects[5][value] = 1
  131.         end
  132.     end
  133.     if math.fmod(gametime, 1.5) == 0 then -- Turtles 1
  134.         if objects[posy][posx] == 0  and posy == 4 and not death then
  135.             posx = posx - 1
  136.         end
  137.         for key,value in pairs(locations[4]) do
  138.             objects[4][value] = 0
  139.             if value > 1 then
  140.                 locations[4][key] = locations[4][key] - 1
  141.             else
  142.                 locations[4][key] = #objects[4]
  143.             end
  144.         end
  145.         for key,value in pairs(locations[4]) do
  146.             objects[4][value] = 1
  147.         end
  148.     end
  149.     if math.fmod(gametime, 3) == 0 then -- Turtles 2
  150.         if objects[posy][posx] == 0  and posy == 6 and not death then
  151.             posx = posx - 1
  152.         end
  153.         for key,value in pairs(locations[6]) do
  154.             objects[6][value] = 0
  155.             if value > 1 then
  156.                 locations[6][key] = locations[6][key] - 1
  157.             else
  158.                 locations[6][key] = #objects[6]
  159.             end
  160.         end
  161.         for key,value in pairs(locations[6]) do
  162.             objects[6][value] = 1
  163.         end
  164.     end
  165.     if math.fmod(gametime, 1) == 0 then -- Cars 1
  166.         for key,value in pairs(locations[8]) do
  167.             objects[8][value] = 0
  168.             if value < #objects[8] then
  169.                 objects[8][value+1] = 1
  170.                 locations[8][key] = locations[8][key] + 1
  171.             else
  172.                 objects[8][1] = 1
  173.                 locations[8][key] = 1
  174.             end
  175.         end
  176.     end
  177.     if math.fmod(gametime, 3) == 0 then -- Cars 2
  178.         for key,value in pairs(locations[10]) do
  179.             objects[10][value] = 0
  180.             if value < #objects[10] then
  181.                 objects[10][value+1] = 1
  182.                 locations[10][key] = locations[10][key] + 1
  183.             else
  184.                 objects[10][1] = 1
  185.                 locations[10][key] = 1
  186.             end
  187.         end
  188.     end
  189.     if math.fmod(gametime, 2) == 0 then -- Cars 3
  190.         for key,value in pairs(locations[9]) do
  191.             objects[9][value] = 0
  192.             if value > 1 then
  193.                 objects[9][value-1] = 1
  194.                 locations[9][key] = locations[9][key] - 1
  195.             else
  196.                 objects[9][1] = #objects[9]
  197.                 locations[9][key] = #objects[9]
  198.             end
  199.         end
  200.     end
  201.     if math.fmod(gametime, 3) == 0 then -- Cars 4
  202.         for key,value in pairs(locations[11]) do
  203.             objects[11][value] = 0
  204.             if value > 1 then
  205.                 objects[11][value-1] = 1
  206.                 locations[11][key] = locations[11][key] - 1
  207.             else
  208.                 objects[11][1] = #objects[11]
  209.                 locations[11][key] = #objects[11]
  210.             end
  211.         end
  212.     end
  213. end
  214.  
  215. function checkDeath()
  216.     if objects[posy][posx] == 1 or  posx < 1 or posx > 13 or posy > 13 then
  217.         if posx < 1 then
  218.             posx = posx + 1
  219.         elseif posx > 13 then
  220.             posx = posx - 1
  221.         elseif posy > 13 then
  222.             posy = posy - 1
  223.         end
  224.         if not death then
  225.             lives = lives - 1
  226.             reset = os.startTimer(2.01)
  227.         else
  228.             death = true
  229.         end
  230.     end
  231. end
  232.  
  233. function checkHome()
  234.     if posy == 2 and objects[posy][posx] == 0 then
  235.         objects[posy][posx] = 1
  236.         resetFrog()
  237.     end
  238. end
  239.  
  240. function drawFrogger()
  241.     term.clear()
  242.     for i=1,terrainh do
  243.         for j=1,terrainw do
  244.             term.setCursorPos(j+woffset, i+hoffset)
  245.             term.setBackgroundColor(returnColor(terrain[i][j]))
  246.             write(" ")
  247.             term.setCursorPos(j+woffset, i+hoffset)
  248.             if i == 2 then
  249.                 if objects[i][j] == 2 then
  250.                     term.setTextColor(returnColor("4"))
  251.                     write("*")
  252.                 elseif (j == 1 or j == 4 or j == 7 or j == 10 or j == 13) and objects[i][j] == 1 then
  253.                     term.setTextColor(returnColor("5"))
  254.                     write("@")
  255.                 end
  256.             elseif i == 3 or i == 5 then
  257.                 if objects[i][j]  == 0 then
  258.                     term.setBackgroundColor(returnColor("c"))
  259.                     write(" ")
  260.                 end
  261.             elseif i == 4 or i == 6 then
  262.                 if objects[i][j]  == 0 then
  263.                     if turtlelevel == 3 then
  264.                         term.setBackgroundColor(returnColor("e"))
  265.                         term.setTextColor(returnColor("5"))
  266.                         write("O")
  267.                     elseif turtlelevel == 2 then
  268.                         term.setTextColor(returnColor("5"))
  269.                         write("O")
  270.                     elseif turtlelevel == 1 then
  271.                         term.setTextColor(returnColor("5"))
  272.                         write("o")
  273.                     end
  274.                 end
  275.             elseif i == 8 or i == 10 then
  276.                 if objects[i][j]  == 1 then
  277.                     term.setTextColor(returnColor("e"))
  278.                     write(">")
  279.                 end
  280.             elseif i == 9 or i == 11 then
  281.                 if objects[i][j]  == 1 then
  282.                     term.setTextColor(returnColor("a"))
  283.                     write("<")
  284.                 end
  285.             end
  286.             if posx == j and posy == i and not death then
  287.                 term.setCursorPos(j+woffset, i+hoffset)
  288.                 term.setTextColor(returnColor("5"))
  289.                 if face == 0 then
  290.                     write("^")
  291.                 elseif face == 1 then
  292.                     write(">")
  293.                 elseif face == 2 then
  294.                     write("v")
  295.                 elseif face == 3 then
  296.                     write("<")
  297.                 end
  298.             elseif posx == j and posy == i and death then
  299.                 term.setCursorPos(j+woffset, i+hoffset)
  300.                 term.setTextColor(returnColor("e"))
  301.                 write("X")
  302.             end
  303.         end
  304.     end
  305. end
  306.  
  307. function runFrogger()
  308.     resetFrog()
  309.     lives = 3
  310.     runcount = 0
  311.     gametimer = os.startTimer(0.05)
  312.     while true do
  313.         local event, key = os.pullEvent()
  314.         if event == "timer" and key == gametimer then
  315.             runcount = runcount + 1
  316.             gametime = runcount * 0.05
  317.             moveOjects()
  318.             checkDeath()
  319.             checkHome()
  320.             drawFrogger()
  321.             gametimer = os.startTimer(0.05)
  322.         elseif event == "key" and key == 200 and not death then
  323.             posy = posy-1
  324.             face = 0
  325.         elseif event == "key" and key == 208 and not death then
  326.             posy = posy+1
  327.             face = 2
  328.         elseif event == "key" and key == 203 and not death then
  329.             posx = posx-1
  330.             face = 3
  331.         elseif event == "key" and key == 205 and not death then
  332.             posx = posx+1
  333.             face = 1
  334.         elseif event == "timer" and key == reset then
  335.             if lives == 0 then
  336.                 break
  337.             end
  338.             resetFrog()
  339.         end
  340.     end
  341. end
  342.  
  343. if term.isColor() then
  344.     runFrogger()
  345. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement