Advertisement
Guest User

pong

a guest
Sep 28th, 2021
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.85 KB | None | 0 0
  1. term.redirect(peripheral.wrap("top"))
  2. term.setBackgroundColor(colors.black)
  3. term.clear()
  4. local w,h = term.getSize()
  5. local p1y = (h/2)-2
  6. local p2y = (h/2)-2
  7. local score1 = 0
  8. local score2 = 0
  9. local ballx = w/2
  10. local bally = h/2
  11. local ballvx = -1
  12. local ballvy = 1
  13. function draw()
  14.   term.setBackgroundColor(colors.black)
  15.   term.clear()
  16.   term.setBackgroundColor(colors.white)
  17.   drawPaddle(2, p1y)
  18.   drawPaddle(w-1, p2y)
  19.   term.setCursorPos(ballx, bally)
  20.   term.write(" ")
  21.   term.setTextColor(colors.white)
  22.   term.setBackgroundColor(colors.black)
  23.   term.setCursorPos(2, 1)
  24.   term.write(score1)
  25.   term.setCursorPos(w-1, 1)
  26.   term.write(score2)
  27. end
  28. function drawPaddle(x, y)
  29.   for i=y,y+5 do
  30.     term.setCursorPos(x, i)
  31.     term.write(" ")
  32.   end
  33. end
  34. function keyPress()
  35.   while true do
  36.     event, side,x, y = os.pullEvent("monitor_touch")
  37.     if (x >= 15  and y <= 5) then
  38.       p2y = p2y-1
  39.     end
  40.     if (x >= 15 and y >= 5) then
  41.       p2y = p2y+1
  42.     end
  43.     if (x <= 5 and y <= 5) then
  44.       p1y = p1y-1
  45.     end
  46.     if (x <= 5 and y >= 5) then
  47.       p1y = p1y+1
  48.     end
  49.     draw()
  50.   end
  51. end
  52. function moveball()
  53.   while true do
  54.     sleep(0.1)
  55.     ballx = ballx + ballvx
  56.     bally = bally + ballvy
  57.     if bally > h-1 then
  58.       ballvy = -ballvy
  59.     end
  60.     if bally < 1 then
  61.       ballvy = -ballvy
  62.     end
  63.     if ballx > 2 and ballx < 3 and bally > p1y and bally < p1y+5 then
  64.       ballvx = -ballvx
  65.     end
  66.     if ballx > w-1 and ballx < w and bally > p2y and bally < p2y+5 then
  67.       ballvx = -ballvx
  68.     end
  69.     if ballx > w then
  70.       score1 = score1+1
  71.       ballx = w/2
  72.       bally = h/2
  73.       ballvx = -1
  74.       ballvy = 1
  75.     end
  76.     if ballx < 1 then
  77.       score2 = score2+1
  78.       ballx = w/2
  79.       bally = h/2
  80.       ballvx = 1
  81.       ballvy = 1
  82.     end
  83.     draw()
  84.   end
  85. end
  86. parallel.waitForAll(keyPress, moveball)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement