Advertisement
Graveyboi

Pong ComputerCraft

Feb 25th, 2020
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.87 KB | None | 0 0
  1. local sx, sy = term.getSize()
  2. local pong1pos, pong2pos, ballxpos, ballypos, ballxvelocity, ballyvelocity, p1score, p2score = math.ceil(sy / 2), math.ceil(sy / 2), math.ceil(sx / 2), math.ceil(sy / 2), math.ceil(math.random() - 1), math.ceil(math.random() - 1), 0, 0
  3.  
  4. if ballxvelocity == 0 then
  5.     ballxvelocity = -1
  6. end
  7.  
  8. if ballyvelocity == 0 then
  9.     ballyvelocity = -1
  10. end
  11.  
  12. local function gameGraphics()
  13.     term.setBackgroundColor(colors.black)
  14.     term.clear()
  15.     term.setBackgroundColor(colors.white)
  16.  
  17.     for a = -1, 1 do
  18.         term.setCursorPos(4, pong1pos + a)
  19.         io.write(" ")
  20.         term.setCursorPos(sx - 3, pong2pos + a)
  21.         io.write(" ")
  22.     end
  23.  
  24.     term.setCursorPos(ballxpos, ballypos)
  25.     io.write(" ")
  26.     term.setCursorPos(1, 1)
  27.     io.write(p1score)
  28.     term.setCursorPos(sx, 1)
  29.     io.write(p2score)
  30. end
  31.  
  32. local function gameTime()
  33.     local timer = os.startTimer(0.07)
  34.     local input1, input2
  35.  
  36.     while true do
  37.         local event, par1 = os.pullEvent()
  38.  
  39.         if event == "timer" and par1 == timer then
  40.             if input1 == "up" then
  41.                 pong1pos = pong1pos - 2
  42.             elseif input1 == "down" then
  43.                 pong1pos = pong1pos + 2
  44.             end
  45.  
  46.             if input2 == "up" then
  47.                 pong2pos = pong2pos - 2
  48.             elseif input2 == "down" then
  49.                 pong2pos = pong2pos + 2
  50.             end
  51.  
  52.             break
  53.         elseif event == "key" then
  54.             if par1 == keys.up and pong1pos > 2 then
  55.                 input2 = "up"
  56.             elseif par1 == keys.down and pong1pos < sy - 1 then
  57.                 input2 = "down"
  58.             end
  59.  
  60.             if par1 == keys.w and pong2pos > 2 then
  61.                 input1 = "up"
  62.             elseif par1 == keys.s and pong2pos < sy - 1 then
  63.                 input1 = "down"
  64.             end
  65.         end
  66.     end
  67. end
  68.  
  69. local function gameFunctions()
  70.     ballxpos, ballypos = ballxpos + ballxvelocity, ballypos + ballyvelocity
  71.  
  72.     if ballypos == 1 then
  73.         ballyvelocity = 1
  74.     elseif ballypos == sy then
  75.         ballyvelocity = -1
  76.     end
  77.  
  78.     if ballxpos == 5 and ballypos >= pong1pos - 1 and ballypos <= pong1pos + 1 then
  79.         ballxvelocity = 1
  80.     elseif ballxpos == sx - 4 and ballypos >= pong2pos - 1 and ballypos <= pong2pos + 1 then
  81.         ballxvelocity = -1
  82.     end
  83. end
  84.  
  85. while true do
  86.     while true do
  87.         gameGraphics()
  88.         gameFunctions()
  89.         gameTime()
  90.  
  91.         if ballxpos == 1 then
  92.             p2score = p2score + 1
  93.             pong1pos, pong2pos, ballxpos, ballypos, ballxvelocity, ballyvelocity = math.ceil(sy / 2), math.ceil(sy / 2), math.ceil(sx / 2), math.ceil(sy / 2), math.ceil(math.random() - 1), math.ceil(math.random() - 1)
  94.  
  95.             if ballxvelocity == 0 then
  96.                 ballxvelocity = -1
  97.             end
  98.  
  99.             if ballyvelocity == 0 then
  100.                 ballyvelocity = -1
  101.             end
  102.         elseif ballxpos == sx then
  103.             p1score = p1score + 1
  104.             pong1pos, pong2pos, ballxpos, ballypos, ballxvelocity, ballyvelocity = math.ceil(sy / 2), math.ceil(sy / 2), math.ceil(sx / 2), math.ceil(sy / 2), math.ceil(math.random() - 1), math.ceil(math.random() - 1)
  105.  
  106.             if ballxvelocity == 0 then
  107.                 ballxvelocity = -1
  108.             end
  109.  
  110.             if ballyvelocity == 0 then
  111.                 ballyvelocity = -1
  112.             end
  113.         end
  114.     end
  115. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement