Advertisement
GravityScore

Pong

Jan 15th, 2013
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.67 KB | None | 0 0
  1.  
  2. --  
  3. --  Pong
  4. --  Made by GravityScore
  5. --  
  6.  
  7.  
  8. --  -------- Variables
  9.  
  10. local w, h = term.getSize()
  11. local refresh = 0.15
  12.  
  13. local paddle1 = {
  14.     x = 1,
  15.     y = 2,
  16.     height = 5
  17. }
  18.  
  19. local paddle2 = {
  20.     x = w,
  21.     y = 2,
  22.     height = 5
  23. }
  24.  
  25. local ball = {
  26.     x = math.floor(w/2),
  27.     y = math.floor(h/2),
  28.     velx = -1,
  29.     vely = 1,
  30.     update = function(self)
  31.         self.x = self.x + self.velx
  32.         self.y = self.y + self.vely
  33.     end
  34. }
  35.  
  36.  
  37. --  -------- Utilities
  38.  
  39. local function pointOnPaddle(x, y, paddle)
  40.     return x == paddle.x and y >= paddle.y and y <= paddle.y + paddle.height - 1
  41. end
  42.  
  43. local function redraw()
  44.     term.setBackgroundColor(colors.black)
  45.     term.setTextColor(colors.white)
  46.     term.clear()
  47.     term.setBackgroundColor(colors.white)
  48.     term.setCursorPos(ball.x, ball.y)
  49.     write(" ")
  50.     for i = 1, paddle1.height do
  51.         term.setCursorPos(paddle1.x, paddle1.y + i - 1)
  52.         write(" ")
  53.     end for i = 1, paddle2.height do
  54.         term.setCursorPos(paddle2.x, paddle2.y + i - 1)
  55.         write(" ")
  56.     end
  57. end
  58.  
  59.  
  60. --  -------- Main
  61.  
  62. local function main()
  63.     local timerId = os.startTimer(refresh)
  64.     while true do
  65.         local e, but, x, y = os.pullEvent()
  66.         if e == "key" then
  67.             if but == keys.up and paddle2.y > 1 then
  68.                 paddle2.y = paddle2.y - 1
  69.                 redraw()
  70.             elseif but == keys.down and paddle2.y + paddle1.height <= h then
  71.                 paddle2.y = paddle2.y + 1
  72.                 redraw()
  73.             elseif but == keys.w and paddle1.y > 1 then
  74.                 paddle1.y = paddle1.y - 1
  75.                 redraw()
  76.             elseif but == keys.s and paddle1.y + paddle1.height <= h then
  77.                 paddle1.y = paddle1.y + 1
  78.                 redraw()
  79.             elseif but == keys.q then
  80.                 term.setBackgroundColor(colors.black)
  81.                 term.setTextColor(colors.white)
  82.                 term.clear()
  83.                 term.setCursorPos(1, 1)
  84.                 return "quit"
  85.             end
  86.         elseif e == "timer" and but == timerId then
  87.             ball.update(ball)
  88.             redraw()
  89.             if ball.x == 2 then
  90.                 if pointOnPaddle(ball.x - 1, ball.y, paddle1) then
  91.                     ball.velx = 1
  92.                 else
  93.                     ball.update(ball)
  94.                     redraw()
  95.                     sleep(2)
  96.                     return "game over"
  97.                 end
  98.             end if ball.x == w - 1 then
  99.                 if pointOnPaddle(ball.x + 1, ball.y, paddle2) then
  100.                     ball.velx = -1
  101.                 else
  102.                     ball.update(ball)
  103.                     redraw()
  104.                     sleep(1)
  105.                     return "game over"
  106.                 end
  107.             end
  108.             if ball.y == 1 then ball.vely = 1 end
  109.             if ball.y == h then ball.vely = -1 end
  110.             timerId = os.startTimer(refresh)
  111.         end
  112.     end
  113. end
  114.  
  115. -- Redraw
  116. redraw()
  117.  
  118. -- Play
  119. local exit = main()
  120.  
  121. -- Exit
  122. term.setBackgroundColor(colors.black)
  123. term.setTextColor(colors.white)
  124. term.clear()
  125. if exit == "game over" then
  126.     term.setCursorPos(3, 3)
  127.     write("Game Over!")
  128.     sleep(2)
  129.     term.clear()
  130.     term.setCursorPos(1, 1)
  131. elseif exit == "quit" then
  132.     term.setCursorPos(1, 1)
  133. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement