Advertisement
AquaJD

DJBALL

Sep 5th, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.31 KB | None | 0 0
  1. local b_RUNNING = true
  2. local n_UPDATE_INTERVAL = 1 / 20  -- seconds / frames
  3.  
  4. local Term = {}
  5. Term.W, Term.H = term.getSize()
  6.  
  7. Board = {
  8.     X = 1,
  9.     Y = 1,
  10.     W = Term.W - 1,
  11.     H = Term.H - 1,
  12.     XMID = Term.W / 2 + 1,
  13.     BLUE_SCORE = 0,
  14.     RED_SCORE = 0
  15. }
  16.  
  17. Player = {}
  18. Ball = {}
  19. Player.__index = Player
  20. Ball.__index = Ball
  21.  
  22. local function oppositeDirection(dir1,dir2)
  23.     if (dir1 == 1 and dir2 == 3) or (dir2 == 1 and dir1 == 3) then
  24.         return true
  25.     elseif (dir1 == 2 and dir2 == 4) or (dir2 == 2 and dir1 == 4) then
  26.         return true
  27.     end
  28.     return false
  29. end
  30.  
  31. -- Create a new player
  32. function Player:new(_n_NUMBER)
  33.     local self =  {
  34.         NUMBER = _n_NUMBER,
  35.         DIRECTION = 4,
  36.         HOLDING = nil,
  37.         CATCHING = false,
  38.         CATCHING_CD = false,
  39.     }
  40.     if _n_NUMBER == 1 then
  41.         self.LEFT = keys.a
  42.         self.UP = keys.w
  43.         self.RIGHT = keys.d
  44.         self.DOWN = keys.s
  45.         self.ACTION = keys.leftShift
  46.         self.TEAM = colors.blue
  47.         self.X = 3
  48.         self.Y = 3
  49.     elseif _n_NUMBER == 2 then
  50.         self.LEFT = keys.left
  51.         self.UP = keys.up
  52.         self.RIGHT = keys.right
  53.         self.DOWN = keys.down
  54.         self.ACTION = keys.m
  55.         self.TEAM = colors.red
  56.         self.X = Term.W - 3
  57.         self.Y = Term.H - 3
  58.     end
  59.     self.XSTART = self.X
  60.     self.YSTART = self.Y
  61.     setmetatable(self,Player)
  62.     return self
  63. end
  64.  
  65. -- Move the player left (1)
  66. function Player:moveLeft()
  67.     self.DIRECTION = 1
  68.     if self.X > (Board.X + 1) then
  69.         self.X = self.X - 1
  70.     end
  71. end
  72.  
  73. -- Move the player up (2)
  74. function Player:moveUp()
  75.     self.DIRECTION = 2
  76.     if self.Y > (Board.Y + 1) then
  77.         self.Y = self.Y - 1
  78.     end
  79. end
  80.  
  81. -- Move the player right (3)
  82. function Player:moveRight()
  83.     self.DIRECTION = 3
  84.     if self.X < (Board.X + Board.W - 1) then
  85.         self.X = self.X + 1
  86.     end
  87. end
  88.  
  89. -- Move the player down (4)
  90. function Player:moveDown()
  91.     self.DIRECTION = 4
  92.     if self.Y < (Board.Y + Board.H - 1) then
  93.         self.Y = self.Y + 1
  94.     end
  95. end
  96.  
  97. -- Reset player position
  98. function Player:reset()
  99.     self.X = self.XSTART
  100.     self.Y = self.YSTART
  101.     self.HOLDING = nil
  102.     self.DIRECTION = 4
  103.     self.CATCHING = false
  104.     self.CATCHING_CD = false
  105. end
  106.  
  107. -- Draw the player
  108. function Player:draw()
  109.     if self.HOLDING ~= nil then
  110.         self.HOLDING.X = self.X
  111.         self.HOLDING.Y = self.Y
  112.     end
  113.     term.setCursorPos(self.X, self.Y)
  114.     term.setTextColor(colors.white)
  115.     term.setBackgroundColor(self.TEAM)
  116.     if not self.CATCHING then
  117.         if self.HOLDING == nil then
  118.             write(self.NUMBER)
  119.         else
  120.             write("o")
  121.         end
  122.     else
  123.         if self.DIRECTION == 1 then
  124.             write("<")
  125.         elseif self.DIRECTION == 2 then
  126.             write("^")
  127.         elseif self.DIRECTION == 3 then
  128.             write(">")
  129.         elseif self.DIRECTION == 4 then
  130.             write("v")
  131.         end
  132.     end
  133. end
  134.  
  135. -- Collision detection
  136. function Player:collidedWith(object)
  137.     if self.X == object.X and self.Y == object.Y then
  138.         return true
  139.     end
  140.     return false
  141. end
  142.  
  143. -- Throw a ball
  144. function Player:throw()
  145.     if self.HOLDING ~= nil then
  146.         self.HOLDING.DIRECTION = self.DIRECTION
  147.         self.HOLDING.IS_THROWN = true
  148.         self.HOLDING.IS_CAUGHT = false
  149.         self.HOLDING = nil
  150.     end
  151. end
  152.  
  153. -- Create a new ball
  154. function Ball:new(_n_NUMBER)
  155.     local self = {
  156.         NUMBER = _n_NUMBER,
  157.         IS_THROWN = false,
  158.         IS_CAUGHT = false,
  159.         COLOUR = colors.white,
  160.         DIRECTION = 0,
  161.         TEAM = colors.white
  162.     }
  163.     if _n_NUMBER == 1 or _n_NUMBER == 3 or _n_NUMBER == 5 then
  164.         self.X = 5
  165.         self.Y = math.floor((Term.H - (Term.H / 2) + 0.5))
  166.     else
  167.         self.X = Term.W - 5
  168.         self.Y = math.floor((Term.H - (Term.H / 2) + 1))
  169.     end
  170.     self.XSTART = self.X
  171.     self.YSTART = self.Y
  172.     setmetatable(self,Ball)
  173.     return self
  174. end
  175.  
  176. -- Reset the ball
  177. function Ball:reset()
  178.     self.X = self.XSTART
  179.     self.Y = self.YSTART
  180.     self.IS_THROWN = false
  181.     self.IS_CAUGHT = false
  182.     self.DIRECTION = 0
  183.     self.TEAM = colors.white
  184. end
  185.  
  186. -- Move the ball's position if it is thrown
  187. function Ball:move()
  188.     if not self.IS_CAUGHT then
  189.         if self.IS_THROWN then
  190.             if self.DIRECTION == 1 then
  191.                 if self.X > (Board.X + 1) then
  192.                     self.X = self.X - 1
  193.                 else
  194.                     self.DIRECTION = 3
  195.                 end
  196.             elseif self.DIRECTION == 2 then
  197.                 if self.Y > (Board.Y + 1) then
  198.                     self.Y = self.Y - 1
  199.                 else
  200.                     self.DIRECTION = 4
  201.                 end
  202.             elseif self.DIRECTION == 3 then
  203.                 if self.X < (Board.X + Board.W - 1) then
  204.                     self.X = self.X + 1
  205.                 else
  206.                     self.DIRECTION = 1
  207.                 end
  208.             elseif self.DIRECTION == 4 then
  209.                 if self.Y < (Board.Y + Board.H - 1) then
  210.                     self.Y = self.Y + 1
  211.                 else
  212.                     self.DIRECTION = 2
  213.                 end
  214.             end
  215.         end
  216.     end
  217. end
  218.  
  219. -- Draw the ball
  220. function Ball:draw()
  221.     term.setCursorPos(self.X,self.Y)
  222.     if self.IS_CAUGHT then
  223.         term.setTextColor(self.COLOUR)
  224.         term.setBackgroundColor(self.TEAM)
  225.     else
  226.         term.setTextColor(self.TEAM)
  227.         term.setBackgroundColor(colors.black)
  228.     end
  229.     write("o")
  230. end
  231.  
  232. -- Initiation
  233. local UPDATE_TIMER = os.startTimer(1)
  234. local PLAYER_1_CATCH_TIMER = nil
  235. local PLAYER_1_CD_CATCH_TIMER = nil
  236. local PLAYER_2_CATCH_TIMER = nil
  237. local PLAYER_2_CD_CATCH_TIMER = nil
  238. local PLAYER_1 = Player:new(1)
  239. local PLAYER_2 = Player:new(2)
  240. local BALL_1 = Ball:new(1)
  241. local BALL_2 = Ball:new(2)
  242.  
  243. -- Draw the board
  244. function updateBoard()
  245.     term.setBackgroundColor(colors.black)
  246.     term.clear()
  247.     paintutils.drawBox(Board.X,Board.Y,Board.X+Board.W,Board.Y+Board.H,colors.gray)
  248.     BALL_1:draw()
  249.     BALL_2:draw()
  250.     PLAYER_1:draw()
  251.     PLAYER_2:draw()
  252.     term.setBackgroundColor(colors.gray)
  253.     term.setTextColor(colors.blue)
  254.     term.setCursorPos((Term.W / 2),1)
  255.     write(Board.BLUE_SCORE)
  256.     term.setTextColor(colors.red)
  257.     term.setCursorPos((Term.W / 2 + 2),1)
  258.     write(Board.RED_SCORE)
  259. end
  260.  
  261. -- The loop
  262. updateBoard()
  263. while b_RUNNING do
  264.     updateBoard()
  265.     local event, button, X, Y = os.pullEvent()
  266.     if event == "key" then
  267.         if button == PLAYER_1.UP then
  268.             PLAYER_1:moveUp()
  269.         elseif button == PLAYER_1.LEFT then
  270.             PLAYER_1:moveLeft()
  271.         elseif button == PLAYER_1.DOWN then
  272.             PLAYER_1:moveDown()
  273.         elseif button == PLAYER_1.RIGHT then
  274.             PLAYER_1:moveRight()
  275.         elseif button == PLAYER_1.ACTION then
  276.             if PLAYER_1.HOLDING ~= nil then
  277.                 PLAYER_1:throw()
  278.             else
  279.                 if not PLAYER_1.CATCHING and not PLAYER_1.CATCHING_CD then
  280.                     PLAYER_1.CATCHING = true
  281.                     PLAYER_1_CATCH_TIMER = os.startTimer(0.5)
  282.                 end
  283.             end
  284.         elseif button == PLAYER_2.UP then
  285.             PLAYER_2:moveUp()
  286.         elseif button == PLAYER_2.LEFT then
  287.             PLAYER_2:moveLeft()
  288.         elseif button == PLAYER_2.DOWN then
  289.             PLAYER_2:moveDown()
  290.         elseif button == PLAYER_2.RIGHT then
  291.             PLAYER_2:moveRight()
  292.         elseif button == PLAYER_2.ACTION then
  293.             if PLAYER_2.HOLDING ~= nil then
  294.                 PLAYER_2:throw()
  295.             else
  296.                 if not PLAYER_2.CATCHING and not PLAYER_2.CATCHING_CD then
  297.                     PLAYER_2.CATCHING = true
  298.                     PLAYER_2_CATCH_TIMER = os.startTimer(0.5)
  299.                 end
  300.             end
  301.         end
  302.     elseif event == "timer" then
  303.         if button == UPDATE_TIMER then
  304.             BALL_1:move()
  305.             BALL_2:move()
  306.             UPDATE_TIMER = os.startTimer(n_UPDATE_INTERVAL)
  307.         elseif button == PLAYER_1_CATCH_TIMER then
  308.             if PLAYER_1.CATCHING then
  309.                 PLAYER_1.CATCHING = false
  310.                 PLAYER_1.CATCHING_CD = true
  311.                 PLAYER_1_CD_CATCH_TIMER = os.startTimer(3)
  312.             end
  313.         elseif button == PLAYER_1_CD_CATCH_TIMER then
  314.             PLAYER_1.CATCHING_CD = false
  315.         elseif button == PLAYER_2_CATCH_TIMER then
  316.             if PLAYER_2.CATCHING then
  317.                 PLAYER_2.CATCHING = false
  318.                 PLAYER_2.CATCHING_CD = true
  319.                 PLAYER_2_CD_CATCH_TIMER = os.startTimer(3)
  320.             end
  321.         elseif button == PLAYER_2_CD_CATCH_TIMER then
  322.             PLAYER_2.CATCHING_CD = false
  323.         end
  324.     elseif event == "key_up" then
  325.         if button == PLAYER_1.ACTION then
  326.             PLAYER_1.CATCHING = false
  327.         elseif button == PLAYER_2.ACTION then
  328.             PLAYER_2.CATCHING = false
  329.         end
  330.     end
  331.     if PLAYER_1:collidedWith(BALL_1) then
  332.         if BALL_1.TEAM ~= PLAYER_1.TEAM then
  333.             if not oppositeDirection(BALL_1.DIRECTION, PLAYER_1.DIRECTION) and BALL_1.TEAM ~= colors.white then
  334.                 BALL_1:reset()
  335.                 BALL_2:reset()
  336.                 PLAYER_1:reset()
  337.                 PLAYER_2:reset()
  338.                 PLAYER_1_CATCH_TIMER = nil
  339.                 PLAYER_1_CD_CATCH_TIMER = nil
  340.                 PLAYER_2_CATCH_TIMER = nil
  341.                 PLAYER_2_CD_CATCH_TIMER = nil
  342.                 Board.RED_SCORE = Board.RED_SCORE + 1
  343.             elseif oppositeDirection(BALL_1.DIRECTION, PLAYER_1.DIRECTION) and BALL_1.TEAM ~= colors.white then
  344.                 if PLAYER_1.CATCHING then
  345.                     PLAYER_1.HOLDING = BALL_1
  346.                     BALL_1.DIRECTION = PLAYER_1.DIRECTION
  347.                     BALL_1.TEAM = PLAYER_1.TEAM
  348.                     BALL_1.IS_CAUGHT = true
  349.                 else
  350.                     BALL_1:reset()
  351.                     BALL_2:reset()
  352.                     PLAYER_1:reset()
  353.                     PLAYER_2:reset()
  354.                     PLAYER_1_CATCH_TIMER = nil
  355.                     PLAYER_1_CD_CATCH_TIMER = nil
  356.                     PLAYER_2_CATCH_TIMER = nil
  357.                     PLAYER_2_CD_CATCH_TIMER = nil
  358.                     Board.RED_SCORE = Board.RED_SCORE + 1
  359.                 end
  360.             elseif BALL_1.TEAM == colors.white then
  361.                 if PLAYER_1.CATCHING then
  362.                     PLAYER_1.HOLDING = BALL_1
  363.                     BALL_1.DIRECTION = PLAYER_1.DIRECTION
  364.                     BALL_1.TEAM = PLAYER_1.TEAM
  365.                     BALL_1.IS_CAUGHT = true
  366.                 end
  367.             end
  368.         elseif oppositeDirection(BALL_1.DIRECTION, PLAYER_1.DIRECTION) then
  369.             if PLAYER_1.CATCHING then
  370.                 PLAYER_1.HOLDING = BALL_1
  371.                 BALL_1.DIRECTION = PLAYER_1.DIRECTION
  372.                 BALL_1.TEAM = PLAYER_1.TEAM
  373.                 BALL_1.IS_CAUGHT = true
  374.             end
  375.         end
  376.     elseif PLAYER_1:collidedWith(BALL_2) then
  377.         if BALL_2.TEAM ~= PLAYER_1.TEAM then
  378.             if not oppositeDirection(BALL_2.DIRECTION, PLAYER_1.DIRECTION) and BALL_2.TEAM ~= colors.white then
  379.                 BALL_1:reset()
  380.                 BALL_2:reset()
  381.                 PLAYER_1:reset()
  382.                 PLAYER_2:reset()
  383.                 PLAYER_1_CATCH_TIMER = nil
  384.                 PLAYER_1_CD_CATCH_TIMER = nil
  385.                 PLAYER_2_CATCH_TIMER = nil
  386.                 PLAYER_2_CD_CATCH_TIMER = nil
  387.                 Board.RED_SCORE = Board.RED_SCORE + 1
  388.             elseif oppositeDirection(BALL_2.DIRECTION, PLAYER_1.DIRECTION) and BALL_2.TEAM ~= colors.white then
  389.                 if PLAYER_1.CATCHING then
  390.                     PLAYER_1.HOLDING = BALL_2
  391.                     BALL_2.DIRECTION = PLAYER_1.DIRECTION
  392.                     BALL_2.TEAM = PLAYER_1.TEAM
  393.                     BALL_2.IS_CAUGHT = true
  394.                 else
  395.                     BALL_1:reset()
  396.                     BALL_2:reset()
  397.                     PLAYER_1:reset()
  398.                     PLAYER_2:reset()
  399.                     PLAYER_1_CATCH_TIMER = nil
  400.                     PLAYER_1_CD_CATCH_TIMER = nil
  401.                     PLAYER_2_CATCH_TIMER = nil
  402.                     PLAYER_2_CD_CATCH_TIMER = nil
  403.                     Board.RED_SCORE = Board.RED_SCORE + 1
  404.                 end
  405.             elseif BALL_2.TEAM == colors.white then
  406.                 if PLAYER_1.CATCHING then
  407.                     PLAYER_1.HOLDING = BALL_2
  408.                     BALL_2.DIRECTION = PLAYER_1.DIRECTION
  409.                     BALL_2.TEAM = PLAYER_1.TEAM
  410.                     BALL_2.IS_CAUGHT = true
  411.                 end
  412.             end
  413.         elseif oppositeDirection(BALL_2.DIRECTION, PLAYER_1.DIRECTION) then
  414.             if PLAYER_1.CATCHING then
  415.                 PLAYER_1.HOLDING = BALL_2
  416.                 BALL_2.DIRECTION = PLAYER_1.DIRECTION
  417.                 BALL_2.TEAM = PLAYER_1.TEAM
  418.                 BALL_2.IS_CAUGHT = true
  419.             end
  420.         end
  421.     end
  422.     if PLAYER_2:collidedWith(BALL_1) then
  423.         if BALL_1.TEAM ~= PLAYER_2.TEAM then
  424.             if not oppositeDirection(BALL_1.DIRECTION, PLAYER_2.DIRECTION) and BALL_1.TEAM ~= colors.white then
  425.                 BALL_1:reset()
  426.                 BALL_2:reset()
  427.                 PLAYER_1:reset()
  428.                 PLAYER_2:reset()
  429.                 PLAYER_1_CATCH_TIMER = nil
  430.                 PLAYER_1_CD_CATCH_TIMER = nil
  431.                 PLAYER_2_CATCH_TIMER = nil
  432.                 PLAYER_2_CD_CATCH_TIMER = nil
  433.                 Board.BLUE_SCORE = Board.BLUE_SCORE + 1
  434.             elseif oppositeDirection(BALL_1.DIRECTION, PLAYER_2.DIRECTION) and BALL_1.TEAM ~= colors.white then
  435.                 if PLAYER_2.CATCHING then
  436.                     PLAYER_2.HOLDING = BALL_1
  437.                     BALL_1.DIRECTION = PLAYER_2.DIRECTION
  438.                     BALL_1.TEAM = PLAYER_2.TEAM
  439.                     BALL_1.IS_CAUGHT = true
  440.                 else
  441.                     BALL_1:reset()
  442.                     BALL_2:reset()
  443.                     PLAYER_1:reset()
  444.                     PLAYER_2:reset()
  445.                     PLAYER_1_CATCH_TIMER = nil
  446.                     PLAYER_1_CD_CATCH_TIMER = nil
  447.                     PLAYER_2_CATCH_TIMER = nil
  448.                     PLAYER_2_CD_CATCH_TIMER = nil
  449.                     Board.BLUE_SCORE = Board.BLUE_SCORE + 1
  450.                 end
  451.             elseif BALL_1.TEAM == colors.white then
  452.                 if PLAYER_2.CATCHING then
  453.                     PLAYER_2.HOLDING = BALL_1
  454.                     BALL_1.DIRECTION = PLAYER_2.DIRECTION
  455.                     BALL_1.TEAM = PLAYER_2.TEAM
  456.                     BALL_1.IS_CAUGHT = true
  457.                 end
  458.             end
  459.         elseif oppositeDirection(BALL_1.DIRECTION, PLAYER_2.DIRECTION) then
  460.             if PLAYER_2.CATCHING then
  461.                 PLAYER_2.HOLDING = BALL_1
  462.                 BALL_1.DIRECTION = PLAYER_2.DIRECTION
  463.                 BALL_1.TEAM = PLAYER_2.TEAM
  464.                 BALL_1.IS_CAUGHT = true
  465.             end
  466.         end
  467.     elseif PLAYER_2:collidedWith(BALL_2) then
  468.         if BALL_2.TEAM ~= PLAYER_2.TEAM then
  469.             if not oppositeDirection(BALL_2.DIRECTION, PLAYER_2.DIRECTION) and BALL_2.TEAM ~= colors.white then
  470.                 BALL_1:reset()
  471.                 BALL_2:reset()
  472.                 PLAYER_1:reset()
  473.                 PLAYER_2:reset()
  474.                 PLAYER_1_CATCH_TIMER = nil
  475.                 PLAYER_1_CD_CATCH_TIMER = nil
  476.                 PLAYER_2_CATCH_TIMER = nil
  477.                 PLAYER_2_CD_CATCH_TIMER = nil
  478.                 Board.BLUE_SCORE = Board.BLUE_SCORE + 1
  479.             elseif oppositeDirection(BALL_2.DIRECTION, PLAYER_2.DIRECTION) and BALL_2.TEAM ~= colors.white then
  480.                 if PLAYER_2.CATCHING then
  481.                     PLAYER_2.HOLDING = BALL_2
  482.                     BALL_2.DIRECTION = PLAYER_2.DIRECTION
  483.                     BALL_2.TEAM = PLAYER_2.TEAM
  484.                     BALL_2.IS_CAUGHT = true
  485.                 else
  486.                     BALL_1:reset()
  487.                     BALL_2:reset()
  488.                     PLAYER_1:reset()
  489.                     PLAYER_2:reset()
  490.                     PLAYER_1_CATCH_TIMER = nil
  491.                     PLAYER_1_CD_CATCH_TIMER = nil
  492.                     PLAYER_2_CATCH_TIMER = nil
  493.                     PLAYER_2_CD_CATCH_TIMER = nil
  494.                     Board.BLUE_SCORE = Board.BLUE_SCORE + 1
  495.                 end
  496.             elseif BALL_2.TEAM == colors.white then
  497.                 if PLAYER_2.CATCHING then
  498.                     PLAYER_2.HOLDING = BALL_2
  499.                     BALL_2.DIRECTION = PLAYER_2.DIRECTION
  500.                     BALL_2.TEAM = PLAYER_2.TEAM
  501.                     BALL_2.IS_CAUGHT = true
  502.                 end
  503.             end
  504.         elseif oppositeDirection(BALL_2.DIRECTION, PLAYER_2.DIRECTION) then
  505.             if PLAYER_2.CATCHING then
  506.                 PLAYER_2.HOLDING = BALL_2
  507.                 BALL_2.DIRECTION = PLAYER_2.DIRECTION
  508.                 BALL_2.TEAM = PLAYER_2.TEAM
  509.                 BALL_2.IS_CAUGHT = true
  510.             end
  511.         end
  512.     end
  513. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement