Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local b_RUNNING = true
- local n_UPDATE_INTERVAL = 1 / 20 -- seconds / frames
- local Term = {}
- Term.W, Term.H = term.getSize()
- Board = {
- X = 1,
- Y = 1,
- W = Term.W - 1,
- H = Term.H - 1,
- XMID = Term.W / 2 + 1,
- BLUE_SCORE = 0,
- RED_SCORE = 0
- }
- Player = {}
- Ball = {}
- Player.__index = Player
- Ball.__index = Ball
- local function oppositeDirection(dir1,dir2)
- if (dir1 == 1 and dir2 == 3) or (dir2 == 1 and dir1 == 3) then
- return true
- elseif (dir1 == 2 and dir2 == 4) or (dir2 == 2 and dir1 == 4) then
- return true
- end
- return false
- end
- -- Create a new player
- function Player:new(_n_NUMBER)
- local self = {
- NUMBER = _n_NUMBER,
- DIRECTION = 4,
- HOLDING = nil,
- CATCHING = false,
- CATCHING_CD = false,
- }
- if _n_NUMBER == 1 then
- self.LEFT = keys.a
- self.UP = keys.w
- self.RIGHT = keys.d
- self.DOWN = keys.s
- self.ACTION = keys.leftShift
- self.TEAM = colors.blue
- self.X = 3
- self.Y = 3
- elseif _n_NUMBER == 2 then
- self.LEFT = keys.left
- self.UP = keys.up
- self.RIGHT = keys.right
- self.DOWN = keys.down
- self.ACTION = keys.m
- self.TEAM = colors.red
- self.X = Term.W - 3
- self.Y = Term.H - 3
- end
- self.XSTART = self.X
- self.YSTART = self.Y
- setmetatable(self,Player)
- return self
- end
- -- Move the player left (1)
- function Player:moveLeft()
- self.DIRECTION = 1
- if self.X > (Board.X + 1) then
- self.X = self.X - 1
- end
- end
- -- Move the player up (2)
- function Player:moveUp()
- self.DIRECTION = 2
- if self.Y > (Board.Y + 1) then
- self.Y = self.Y - 1
- end
- end
- -- Move the player right (3)
- function Player:moveRight()
- self.DIRECTION = 3
- if self.X < (Board.X + Board.W - 1) then
- self.X = self.X + 1
- end
- end
- -- Move the player down (4)
- function Player:moveDown()
- self.DIRECTION = 4
- if self.Y < (Board.Y + Board.H - 1) then
- self.Y = self.Y + 1
- end
- end
- -- Reset player position
- function Player:reset()
- self.X = self.XSTART
- self.Y = self.YSTART
- self.HOLDING = nil
- self.DIRECTION = 4
- self.CATCHING = false
- self.CATCHING_CD = false
- end
- -- Draw the player
- function Player:draw()
- if self.HOLDING ~= nil then
- self.HOLDING.X = self.X
- self.HOLDING.Y = self.Y
- end
- term.setCursorPos(self.X, self.Y)
- term.setTextColor(colors.white)
- term.setBackgroundColor(self.TEAM)
- if not self.CATCHING then
- if self.HOLDING == nil then
- write(self.NUMBER)
- else
- write("o")
- end
- else
- if self.DIRECTION == 1 then
- write("<")
- elseif self.DIRECTION == 2 then
- write("^")
- elseif self.DIRECTION == 3 then
- write(">")
- elseif self.DIRECTION == 4 then
- write("v")
- end
- end
- end
- -- Collision detection
- function Player:collidedWith(object)
- if self.X == object.X and self.Y == object.Y then
- return true
- end
- return false
- end
- -- Throw a ball
- function Player:throw()
- if self.HOLDING ~= nil then
- self.HOLDING.DIRECTION = self.DIRECTION
- self.HOLDING.IS_THROWN = true
- self.HOLDING.IS_CAUGHT = false
- self.HOLDING = nil
- end
- end
- -- Create a new ball
- function Ball:new(_n_NUMBER)
- local self = {
- NUMBER = _n_NUMBER,
- IS_THROWN = false,
- IS_CAUGHT = false,
- COLOUR = colors.white,
- DIRECTION = 0,
- TEAM = colors.white
- }
- if _n_NUMBER == 1 or _n_NUMBER == 3 or _n_NUMBER == 5 then
- self.X = 5
- self.Y = math.floor((Term.H - (Term.H / 2) + 0.5))
- else
- self.X = Term.W - 5
- self.Y = math.floor((Term.H - (Term.H / 2) + 1))
- end
- self.XSTART = self.X
- self.YSTART = self.Y
- setmetatable(self,Ball)
- return self
- end
- -- Reset the ball
- function Ball:reset()
- self.X = self.XSTART
- self.Y = self.YSTART
- self.IS_THROWN = false
- self.IS_CAUGHT = false
- self.DIRECTION = 0
- self.TEAM = colors.white
- end
- -- Move the ball's position if it is thrown
- function Ball:move()
- if not self.IS_CAUGHT then
- if self.IS_THROWN then
- if self.DIRECTION == 1 then
- if self.X > (Board.X + 1) then
- self.X = self.X - 1
- else
- self.DIRECTION = 3
- end
- elseif self.DIRECTION == 2 then
- if self.Y > (Board.Y + 1) then
- self.Y = self.Y - 1
- else
- self.DIRECTION = 4
- end
- elseif self.DIRECTION == 3 then
- if self.X < (Board.X + Board.W - 1) then
- self.X = self.X + 1
- else
- self.DIRECTION = 1
- end
- elseif self.DIRECTION == 4 then
- if self.Y < (Board.Y + Board.H - 1) then
- self.Y = self.Y + 1
- else
- self.DIRECTION = 2
- end
- end
- end
- end
- end
- -- Draw the ball
- function Ball:draw()
- term.setCursorPos(self.X,self.Y)
- if self.IS_CAUGHT then
- term.setTextColor(self.COLOUR)
- term.setBackgroundColor(self.TEAM)
- else
- term.setTextColor(self.TEAM)
- term.setBackgroundColor(colors.black)
- end
- write("o")
- end
- -- Initiation
- local UPDATE_TIMER = os.startTimer(1)
- local PLAYER_1_CATCH_TIMER = nil
- local PLAYER_1_CD_CATCH_TIMER = nil
- local PLAYER_2_CATCH_TIMER = nil
- local PLAYER_2_CD_CATCH_TIMER = nil
- local PLAYER_1 = Player:new(1)
- local PLAYER_2 = Player:new(2)
- local BALL_1 = Ball:new(1)
- local BALL_2 = Ball:new(2)
- -- Draw the board
- function updateBoard()
- term.setBackgroundColor(colors.black)
- term.clear()
- paintutils.drawBox(Board.X,Board.Y,Board.X+Board.W,Board.Y+Board.H,colors.gray)
- BALL_1:draw()
- BALL_2:draw()
- PLAYER_1:draw()
- PLAYER_2:draw()
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.blue)
- term.setCursorPos((Term.W / 2),1)
- write(Board.BLUE_SCORE)
- term.setTextColor(colors.red)
- term.setCursorPos((Term.W / 2 + 2),1)
- write(Board.RED_SCORE)
- end
- -- The loop
- updateBoard()
- while b_RUNNING do
- updateBoard()
- local event, button, X, Y = os.pullEvent()
- if event == "key" then
- if button == PLAYER_1.UP then
- PLAYER_1:moveUp()
- elseif button == PLAYER_1.LEFT then
- PLAYER_1:moveLeft()
- elseif button == PLAYER_1.DOWN then
- PLAYER_1:moveDown()
- elseif button == PLAYER_1.RIGHT then
- PLAYER_1:moveRight()
- elseif button == PLAYER_1.ACTION then
- if PLAYER_1.HOLDING ~= nil then
- PLAYER_1:throw()
- else
- if not PLAYER_1.CATCHING and not PLAYER_1.CATCHING_CD then
- PLAYER_1.CATCHING = true
- PLAYER_1_CATCH_TIMER = os.startTimer(0.5)
- end
- end
- elseif button == PLAYER_2.UP then
- PLAYER_2:moveUp()
- elseif button == PLAYER_2.LEFT then
- PLAYER_2:moveLeft()
- elseif button == PLAYER_2.DOWN then
- PLAYER_2:moveDown()
- elseif button == PLAYER_2.RIGHT then
- PLAYER_2:moveRight()
- elseif button == PLAYER_2.ACTION then
- if PLAYER_2.HOLDING ~= nil then
- PLAYER_2:throw()
- else
- if not PLAYER_2.CATCHING and not PLAYER_2.CATCHING_CD then
- PLAYER_2.CATCHING = true
- PLAYER_2_CATCH_TIMER = os.startTimer(0.5)
- end
- end
- end
- elseif event == "timer" then
- if button == UPDATE_TIMER then
- BALL_1:move()
- BALL_2:move()
- UPDATE_TIMER = os.startTimer(n_UPDATE_INTERVAL)
- elseif button == PLAYER_1_CATCH_TIMER then
- if PLAYER_1.CATCHING then
- PLAYER_1.CATCHING = false
- PLAYER_1.CATCHING_CD = true
- PLAYER_1_CD_CATCH_TIMER = os.startTimer(3)
- end
- elseif button == PLAYER_1_CD_CATCH_TIMER then
- PLAYER_1.CATCHING_CD = false
- elseif button == PLAYER_2_CATCH_TIMER then
- if PLAYER_2.CATCHING then
- PLAYER_2.CATCHING = false
- PLAYER_2.CATCHING_CD = true
- PLAYER_2_CD_CATCH_TIMER = os.startTimer(3)
- end
- elseif button == PLAYER_2_CD_CATCH_TIMER then
- PLAYER_2.CATCHING_CD = false
- end
- elseif event == "key_up" then
- if button == PLAYER_1.ACTION then
- PLAYER_1.CATCHING = false
- elseif button == PLAYER_2.ACTION then
- PLAYER_2.CATCHING = false
- end
- end
- if PLAYER_1:collidedWith(BALL_1) then
- if BALL_1.TEAM ~= PLAYER_1.TEAM then
- if not oppositeDirection(BALL_1.DIRECTION, PLAYER_1.DIRECTION) and BALL_1.TEAM ~= colors.white then
- BALL_1:reset()
- BALL_2:reset()
- PLAYER_1:reset()
- PLAYER_2:reset()
- PLAYER_1_CATCH_TIMER = nil
- PLAYER_1_CD_CATCH_TIMER = nil
- PLAYER_2_CATCH_TIMER = nil
- PLAYER_2_CD_CATCH_TIMER = nil
- Board.RED_SCORE = Board.RED_SCORE + 1
- elseif oppositeDirection(BALL_1.DIRECTION, PLAYER_1.DIRECTION) and BALL_1.TEAM ~= colors.white then
- if PLAYER_1.CATCHING then
- PLAYER_1.HOLDING = BALL_1
- BALL_1.DIRECTION = PLAYER_1.DIRECTION
- BALL_1.TEAM = PLAYER_1.TEAM
- BALL_1.IS_CAUGHT = true
- else
- BALL_1:reset()
- BALL_2:reset()
- PLAYER_1:reset()
- PLAYER_2:reset()
- PLAYER_1_CATCH_TIMER = nil
- PLAYER_1_CD_CATCH_TIMER = nil
- PLAYER_2_CATCH_TIMER = nil
- PLAYER_2_CD_CATCH_TIMER = nil
- Board.RED_SCORE = Board.RED_SCORE + 1
- end
- elseif BALL_1.TEAM == colors.white then
- if PLAYER_1.CATCHING then
- PLAYER_1.HOLDING = BALL_1
- BALL_1.DIRECTION = PLAYER_1.DIRECTION
- BALL_1.TEAM = PLAYER_1.TEAM
- BALL_1.IS_CAUGHT = true
- end
- end
- elseif oppositeDirection(BALL_1.DIRECTION, PLAYER_1.DIRECTION) then
- if PLAYER_1.CATCHING then
- PLAYER_1.HOLDING = BALL_1
- BALL_1.DIRECTION = PLAYER_1.DIRECTION
- BALL_1.TEAM = PLAYER_1.TEAM
- BALL_1.IS_CAUGHT = true
- end
- end
- elseif PLAYER_1:collidedWith(BALL_2) then
- if BALL_2.TEAM ~= PLAYER_1.TEAM then
- if not oppositeDirection(BALL_2.DIRECTION, PLAYER_1.DIRECTION) and BALL_2.TEAM ~= colors.white then
- BALL_1:reset()
- BALL_2:reset()
- PLAYER_1:reset()
- PLAYER_2:reset()
- PLAYER_1_CATCH_TIMER = nil
- PLAYER_1_CD_CATCH_TIMER = nil
- PLAYER_2_CATCH_TIMER = nil
- PLAYER_2_CD_CATCH_TIMER = nil
- Board.RED_SCORE = Board.RED_SCORE + 1
- elseif oppositeDirection(BALL_2.DIRECTION, PLAYER_1.DIRECTION) and BALL_2.TEAM ~= colors.white then
- if PLAYER_1.CATCHING then
- PLAYER_1.HOLDING = BALL_2
- BALL_2.DIRECTION = PLAYER_1.DIRECTION
- BALL_2.TEAM = PLAYER_1.TEAM
- BALL_2.IS_CAUGHT = true
- else
- BALL_1:reset()
- BALL_2:reset()
- PLAYER_1:reset()
- PLAYER_2:reset()
- PLAYER_1_CATCH_TIMER = nil
- PLAYER_1_CD_CATCH_TIMER = nil
- PLAYER_2_CATCH_TIMER = nil
- PLAYER_2_CD_CATCH_TIMER = nil
- Board.RED_SCORE = Board.RED_SCORE + 1
- end
- elseif BALL_2.TEAM == colors.white then
- if PLAYER_1.CATCHING then
- PLAYER_1.HOLDING = BALL_2
- BALL_2.DIRECTION = PLAYER_1.DIRECTION
- BALL_2.TEAM = PLAYER_1.TEAM
- BALL_2.IS_CAUGHT = true
- end
- end
- elseif oppositeDirection(BALL_2.DIRECTION, PLAYER_1.DIRECTION) then
- if PLAYER_1.CATCHING then
- PLAYER_1.HOLDING = BALL_2
- BALL_2.DIRECTION = PLAYER_1.DIRECTION
- BALL_2.TEAM = PLAYER_1.TEAM
- BALL_2.IS_CAUGHT = true
- end
- end
- end
- if PLAYER_2:collidedWith(BALL_1) then
- if BALL_1.TEAM ~= PLAYER_2.TEAM then
- if not oppositeDirection(BALL_1.DIRECTION, PLAYER_2.DIRECTION) and BALL_1.TEAM ~= colors.white then
- BALL_1:reset()
- BALL_2:reset()
- PLAYER_1:reset()
- PLAYER_2:reset()
- PLAYER_1_CATCH_TIMER = nil
- PLAYER_1_CD_CATCH_TIMER = nil
- PLAYER_2_CATCH_TIMER = nil
- PLAYER_2_CD_CATCH_TIMER = nil
- Board.BLUE_SCORE = Board.BLUE_SCORE + 1
- elseif oppositeDirection(BALL_1.DIRECTION, PLAYER_2.DIRECTION) and BALL_1.TEAM ~= colors.white then
- if PLAYER_2.CATCHING then
- PLAYER_2.HOLDING = BALL_1
- BALL_1.DIRECTION = PLAYER_2.DIRECTION
- BALL_1.TEAM = PLAYER_2.TEAM
- BALL_1.IS_CAUGHT = true
- else
- BALL_1:reset()
- BALL_2:reset()
- PLAYER_1:reset()
- PLAYER_2:reset()
- PLAYER_1_CATCH_TIMER = nil
- PLAYER_1_CD_CATCH_TIMER = nil
- PLAYER_2_CATCH_TIMER = nil
- PLAYER_2_CD_CATCH_TIMER = nil
- Board.BLUE_SCORE = Board.BLUE_SCORE + 1
- end
- elseif BALL_1.TEAM == colors.white then
- if PLAYER_2.CATCHING then
- PLAYER_2.HOLDING = BALL_1
- BALL_1.DIRECTION = PLAYER_2.DIRECTION
- BALL_1.TEAM = PLAYER_2.TEAM
- BALL_1.IS_CAUGHT = true
- end
- end
- elseif oppositeDirection(BALL_1.DIRECTION, PLAYER_2.DIRECTION) then
- if PLAYER_2.CATCHING then
- PLAYER_2.HOLDING = BALL_1
- BALL_1.DIRECTION = PLAYER_2.DIRECTION
- BALL_1.TEAM = PLAYER_2.TEAM
- BALL_1.IS_CAUGHT = true
- end
- end
- elseif PLAYER_2:collidedWith(BALL_2) then
- if BALL_2.TEAM ~= PLAYER_2.TEAM then
- if not oppositeDirection(BALL_2.DIRECTION, PLAYER_2.DIRECTION) and BALL_2.TEAM ~= colors.white then
- BALL_1:reset()
- BALL_2:reset()
- PLAYER_1:reset()
- PLAYER_2:reset()
- PLAYER_1_CATCH_TIMER = nil
- PLAYER_1_CD_CATCH_TIMER = nil
- PLAYER_2_CATCH_TIMER = nil
- PLAYER_2_CD_CATCH_TIMER = nil
- Board.BLUE_SCORE = Board.BLUE_SCORE + 1
- elseif oppositeDirection(BALL_2.DIRECTION, PLAYER_2.DIRECTION) and BALL_2.TEAM ~= colors.white then
- if PLAYER_2.CATCHING then
- PLAYER_2.HOLDING = BALL_2
- BALL_2.DIRECTION = PLAYER_2.DIRECTION
- BALL_2.TEAM = PLAYER_2.TEAM
- BALL_2.IS_CAUGHT = true
- else
- BALL_1:reset()
- BALL_2:reset()
- PLAYER_1:reset()
- PLAYER_2:reset()
- PLAYER_1_CATCH_TIMER = nil
- PLAYER_1_CD_CATCH_TIMER = nil
- PLAYER_2_CATCH_TIMER = nil
- PLAYER_2_CD_CATCH_TIMER = nil
- Board.BLUE_SCORE = Board.BLUE_SCORE + 1
- end
- elseif BALL_2.TEAM == colors.white then
- if PLAYER_2.CATCHING then
- PLAYER_2.HOLDING = BALL_2
- BALL_2.DIRECTION = PLAYER_2.DIRECTION
- BALL_2.TEAM = PLAYER_2.TEAM
- BALL_2.IS_CAUGHT = true
- end
- end
- elseif oppositeDirection(BALL_2.DIRECTION, PLAYER_2.DIRECTION) then
- if PLAYER_2.CATCHING then
- PLAYER_2.HOLDING = BALL_2
- BALL_2.DIRECTION = PLAYER_2.DIRECTION
- BALL_2.TEAM = PLAYER_2.TEAM
- BALL_2.IS_CAUGHT = true
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement