Advertisement
Guest User

PONG for ComputerCraft

a guest
Feb 22nd, 2012
7,681
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.32 KB | None | 0 0
  1. --Vars
  2. width, height = term.getSize()
  3. i = 0
  4. i_max = width*height
  5. x = 1
  6. y = 1
  7. p1 = 0
  8. p2 = 0
  9. pad1 = math.ceil(height/2)
  10. pad2 = math.ceil(height/2)
  11. up = false
  12. right = true
  13.  
  14. --Functions
  15. function drawPads()
  16.     term.setCursorPos(1,pad1-1)
  17.     write("#")
  18.     term.setCursorPos(1,pad1)
  19.     write("#")
  20.     term.setCursorPos(1,pad1+1)
  21.     write("#")
  22.     term.setCursorPos(1,pad1-2)
  23.     write(" ")
  24.     term.setCursorPos(1,pad1+2)
  25.     write(" ")
  26.     if pad1 == 4 then
  27.         term.setCursorPos(1,pad1-2)
  28.         write("-")
  29.     end
  30.     if pad1 == 15 then
  31.         term.setCursorPos(1,pad1+2)
  32.         write("-")
  33.     end
  34.    
  35.     term.setCursorPos(width-1,pad2-1)
  36.     write("#")
  37.     term.setCursorPos(width-1,pad2)
  38.     write("#")
  39.     term.setCursorPos(width-1,pad2+1)
  40.     write("#")
  41.     term.setCursorPos(width-1,pad2-2)
  42.     write(" ")
  43.     term.setCursorPos(width-1,pad2+2)
  44.     write(" ")
  45.     if pad2 == 4 then
  46.         term.setCursorPos(width-1,pad2-2)
  47.         write("-")
  48.     end
  49.     if pad2 == 15 then
  50.         term.setCursorPos(width-1,pad2+2)
  51.         write("-")
  52.     end
  53. end
  54.  
  55. function ball()
  56.     term.setCursorPos(x,y)
  57.     write(" ")
  58.     if up == true then
  59.         y = y - 1
  60.     else
  61.         y = y + 1
  62.     end
  63.     if right == true then
  64.         if (x == width - 2) then
  65.             if y == pad2 or y == pad2-1 or y == pad2+1 then
  66.                 x = x - 1
  67.                 right = false
  68.             elseif up == true and y == pad2-2 then
  69.                 x = x - 1
  70.                 right = false
  71.             elseif up == false and y == pad2+2 then
  72.                 x = x - 1
  73.                 right = false
  74.             else
  75.                 x = x + 1
  76.             end
  77.         else
  78.                 x = x + 1
  79.         end
  80.     else
  81.         if (x == 2) then
  82.             if y == pad1 or y == pad1-1 or y == pad1+1 then
  83.                 x = x + 1
  84.                 right = true
  85.             elseif up == true and y == pad1-2 then
  86.                 x = x + 1
  87.                 right = true
  88.             elseif up == false and y == pad1+2 then
  89.                 x = x + 1
  90.                 right = true
  91.             else
  92.                 x = x - 1
  93.             end
  94.         else
  95.             x = x - 1
  96.         end
  97.     end
  98.     if x == 1 then
  99.         right = true
  100.         p2 = p2 + 1
  101.         x = math.ceil(width/2)
  102.         y = math.ceil(height/2)
  103.         term.setCursorPos(math.ceil(width/2)-3,1)
  104.         write(p1.." | "..p2)
  105.     end
  106.     if x == (width - 1) then
  107.         right = false
  108.         p1 = p1 + 1
  109.         x = math.ceil(width/2)
  110.         y = math.ceil(height/2)
  111.         term.setCursorPos(math.ceil(width/2)-3,1)
  112.         write(p1.." | "..p2)
  113.     end
  114.     if y == 3 then up = false end
  115.     if y == height-2 then up = true end
  116. end
  117.  
  118. --Intro
  119. term.clear()
  120. term.setCursorPos(1,1)
  121. print("")
  122. print("")
  123. print("     X==---------------------------------==X")
  124. print("     |ComputerCraft Pong, made by Chub1337!|")
  125. print("     X==---------------------------------==X")
  126. print("")
  127. print("")
  128. print("")
  129. print("       Player 1 controls: W = up, S = down")
  130. print("")
  131. print("       Player 2 controls: O = up, L = down")
  132. print("")
  133. print("")
  134. print("")
  135. print("")
  136. print("")
  137. print("")
  138. write("Press <Enter> to continue.")
  139. io.read()
  140.  
  141. term.clear()
  142. term.setCursorPos(1,1)
  143.  
  144. write("Player 1")
  145. term.setCursorPos(width-8,1)
  146. write("Player 2")
  147. term.setCursorPos(math.ceil(width/2)-3,1)
  148. write(p1.." | "..p2)
  149. drawPads()
  150. term.setCursorPos(1,height)
  151. write(pad1.."|"..pad2)
  152.  
  153. for i=1,width-1,1 do
  154.     term.setCursorPos(i,2)
  155.     write("-")
  156. end
  157. term.setCursorPos(1,height-1)
  158. for i=1,width-1,1 do
  159.     term.setCursorPos(i,height-1)
  160.     write("-")
  161. end
  162.  
  163. --Pong
  164. x = math.ceil(width/2)
  165. y = math.ceil(height/2)
  166. dir = math.ceil(math.random(0,4))
  167.  
  168.  
  169. if dir == 1 then
  170.     up = true
  171.     right = true
  172. elseif dir == 2 then
  173.     up = true
  174.     right = false
  175. elseif dir == 3 then
  176.     up = false
  177.     right = true
  178. elseif dir == 4 then
  179.     up = false
  180.     right = false
  181. end
  182.  
  183. function timerupdate()
  184.     ball()
  185.    
  186.     term.setCursorPos(7,height)
  187.     write("X:"..x.." ")
  188.     term.setCursorPos(13,height)
  189.     write("Y:"..y.." ")
  190.     term.setCursorPos(18,height)
  191.     write("up:"..tostring(up).." ")
  192.     term.setCursorPos(30,height)
  193.     write("right:"..tostring(right).." ")
  194.     term.setCursorPos(x,y)
  195.     write("O")
  196.     term.setCursorPos(1,height)
  197.     write(pad1.."|"..pad2.." ")
  198.         timer1 = os.startTimer(.1)
  199. end
  200. timer1 = os.startTimer(.1)
  201. while true do
  202.     event, par = os.pullEvent()
  203.     if event == "timer" and par == timer1 then timerupdate() end
  204.     if event == "char" then
  205.         if par == "w" then
  206.             pad1 = pad1 - 1
  207.             if pad1 < 4 then
  208.                 pad1 = 4
  209.             end
  210.         elseif par == "s" then
  211.             pad1 = pad1 + 1
  212.             if pad1 > height-3 then
  213.                 pad1 = height-3
  214.             end
  215.         end
  216.         if par == "o" then
  217.             pad2 = pad2 - 1
  218.             if pad2 < 4 then
  219.                 pad2 = 4
  220.             end
  221.         elseif par == "l" then
  222.             pad2 = pad2 + 1
  223.             if pad2 > height-3 then
  224.                 pad2 = height-3
  225.             end
  226.         end
  227.     end
  228.     drawPads()
  229. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement