Advertisement
CodeCrafter

pong!

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