Don't like ads? PRO users don't see any ads ;-)
Guest

Tunel

By: Henesy13 on Apr 23rd, 2012  |  syntax: None  |  size: 6.23 KB  |  hits: 26  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. --Vars
  2. width, height = term.getSize()
  3. i = 0
  4. i_max = width*height
  5. x = 1
  6. y = 1
  7. score = 0
  8. tun_y = math.ceil(height/2)-4
  9. tun_h = 7
  10. s_time = 10
  11. timer2 = 5+math.ceil(math.random(0,s_time))
  12. dir = 0
  13. timer3 = 400
  14. lives = 3
  15.  
  16. --Highscore
  17. if not fs.exists("tunnel_hs") then
  18.         local tmp=io.open("tunnel_hs", "w")
  19.         tmp:write("Name")
  20.         tmp:write(1)
  21.         tmp:close()
  22. end
  23.  
  24. hs = io.open("tunnel_hs","r")
  25. hs_name = hs:read("*l")
  26. hs_score = hs:read("*l")
  27. hs:close()
  28.  
  29. --Table
  30. screen = {}
  31. for a=1,width-1,1 do
  32.         screen[a] = {}
  33. end
  34. for a=1,width-1,1 do
  35.         for b=3,height-2,1 do
  36.                 screen[a][b] = " "
  37.         end
  38. end
  39.  
  40. --Functions
  41. function drawPlayer()
  42.         term.setCursorPos(2,y)
  43.         write(">")
  44.         if y == 3 then
  45.                 term.setCursorPos(2,y-1)
  46.                 write("-")
  47.         end
  48.         if y == height-2 then
  49.                 term.setCursorPos(2,y+1)
  50.                 write("-")
  51.         end
  52. end
  53.  
  54. function tunnel()
  55.         for a=1,width-2,1 do
  56.                 for b=3,height-2,1 do
  57.                         screen[a][b] = screen[a+1][b]
  58.                         term.setCursorPos(a,B)
  59.                         write(screen[a][b])
  60.                 end
  61.         end
  62.         for a=1,height-2,1 do
  63.                 screen[width-1][a] = "#"
  64.                 if a >= tun_y and a <= tun_y+tun_h then screen[width-1][a] = " " end
  65.                 term.setCursorPos(width-1,a)
  66.                 if screen[a][width-1] then write(screen[a][width-1]) end
  67.         end
  68.         if timer2 == 0 then
  69.                 dir = math.ceil(math.random(0,2))
  70.                 if dir == 1 then tun_y = tun_y + 1 end
  71.                 if dir == 2 then tun_y = tun_y - 1 end
  72.                 if tun_y == 2 then tun_y = 3 end
  73.                 if tun_y == height-1-tun_h then tun_y = height-2-tun_h end
  74.                 timer2 = 5+math.ceil(math.random(0,s_time))
  75.         end
  76.         timer2 = timer2 - 1
  77.         if timer3 == 0 then
  78.                 tun_h = tun_h - 1
  79.                 timer3 = 160+tun_h*40
  80.                 if tun_h == 1 then
  81.                         tun_h = 2
  82.                         s_time = s_time-1
  83.                         if s_time == 0 then s_time = 1 end
  84.                 end
  85.         end
  86.         timer3 = timer3 - 1
  87. end
  88.  
  89. function gameover()
  90.         lives = lives-1
  91.         timer3 = 400
  92.         tun_h = 7
  93.         tun_y = math.ceil(height/2)-4
  94.         y = math.ceil(height/2)
  95.         s_time = 10
  96.         if lives == -1 then
  97.                 term.clear()
  98.                 term.setCursorPos(math.ceil(width/2)-5,math.ceil(height/2)-4)
  99.                 write("Game Over!")
  100.                 term.setCursorPos(math.ceil(width/2)-10,math.ceil(height/2))
  101.                 write("Your score is: "..score)
  102.                 term.setCursorPos(math.ceil(width/2)-13,math.ceil(height/2)+2)
  103.                 if score > tonumber(hs_score) then
  104.                         write("You have beaten \n                  "..hs_name.."'s highscore of "..hs_score.."!")
  105.                         term.setCursorPos(math.ceil(width/2)-10,math.ceil(height/2)+5)
  106.                         write("Please enter your name: ")
  107.                         hs = io.open("tunnel_hs","w")
  108.                         name = io.read()
  109.                         hs:write(name.."\n")
  110.                         hs:write(score.."\n")
  111.                         hs:close()
  112.                 else
  113.                         write("Too bad, you didn't beat \n                 "..hs_name.."'s highscore of "..hs_score..".")
  114.                 end
  115.                 term.setCursorPos(1,height)
  116.                 write("Press <Enter> to continue.")
  117.                 read()
  118.                 score = 0
  119.         else
  120.                 term.setCursorPos(1,1)
  121.                 write("Score: 0         ")
  122.                 for a=1,width-1,1 do
  123.                         for b=3,height-2,1 do
  124.                                 screen[a][b] = " "
  125.                                 term.setCursorPos(a,B)
  126.                                 write(screen[a][b])
  127.                         end
  128.                 end
  129.         end
  130. end
  131.  
  132. function timerupdate()
  133.         tunnel()
  134.         timer1 = os.startTimer(.05)
  135. end
  136.  
  137. --Intro
  138. term.clear()
  139. term.setCursorPos(1,1)
  140. print("")
  141. print("")
  142. print("  X==---------------------------------==X")
  143. print("  | CC Games: Tunnel, made by Chub1337! |")
  144. print("  X==---------------------------------==X")
  145. print("")
  146. print("")
  147. print("")
  148. print("            Controls:")
  149. print("")
  150. print("            UP: ArrowKey-Up")
  151. print("            DOWN: ArrowKey-Down")
  152. print("")
  153. print("            Current Highscore:")
  154. print("            "..hs_name.." with "..hs_score)
  155. print("")
  156. print("")
  157. write("Press <Enter> to continue.")
  158. io.read()
  159.  
  160. term.clear()
  161. term.setCursorPos(1,1)
  162.  
  163. --Screen
  164. write("Score: "..score)
  165.  
  166. for i=1,width-1,1 do
  167.         term.setCursorPos(i,2)
  168.         write("-")
  169. end
  170. term.setCursorPos(1,height-1)
  171. for i=1,width-1,1 do
  172.         term.setCursorPos(i,height-1)
  173.         write("-")
  174. end
  175.  
  176. --Tunnel
  177. x = 2
  178. y = math.ceil(height/2)
  179.  
  180. timer1 = os.startTimer(.05)
  181.  
  182. while lives~=-1 do
  183.         event, par = os.pullEvent()
  184.         if event == "timer" and par == timer1 then timerupdate() end
  185.         if event == "key" then
  186.                 if par == 200 then
  187.                         y = y - 1
  188.                         if y < 3 then
  189.                                 y = 3
  190.                         end
  191.                         term.setCursorPos(2,y+1)
  192.                         write(" ")
  193.                 elseif par == 208 then
  194.                         y = y + 1
  195.                         if y > height-2 then
  196.                                 y = height-2
  197.                                
  198.                         end
  199.                         term.setCursorPos(2,y-1)
  200.                         write(" ")
  201.                 end
  202.         end
  203.         drawPlayer()
  204.         score = score+1
  205.         term.setCursorPos(1,1)
  206.         write("Score: "..score)
  207.         term.setCursorPos(math.ceil(width/2),1)
  208.         write("Lives: "..lives)
  209.         term.setCursorPos(1,height)
  210.         --write("tun_h: "..tun_h.."   s_time: "..s_time.."   timer3: "..timer3.."  ")
  211.         write("Highscore: "..hs_score.." by "..hs_name..".")
  212.         if screen[2][y] == "#" then
  213.                 gameover()
  214.         end
  215. end