Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local max_x, max_y = term.getSize()
- local p1 = 1
- local p1_score = 0
- local p2 = 1
- local p2_score = 0
- local ball = {
- x = max_x/2,
- y = max_y/2,
- direction_x = -1,
- direction_y = 1
- }
- local pressing = {}
- local win = 0
- local function reset()
- term.setCursorPos(1,1)
- term.clear()
- term.setBackgroundColor(colors.black)
- end
- local function update()
- while true do
- if pressing[keys.w] then
- p1 = (p1 == 1) and p1 or p1 - 1
- end
- if pressing[keys.s] then
- p1 = (p1+5 == max_y) and p1 or p1 + 1
- end
- if pressing[keys.up] then
- p2 = (p2 == 1) and p2 or p2 - 1
- end
- if pressing[keys.down] then
- p2 = (p2+5 == max_y) and p2 or p2 + 1
- end
- local addx = (ball.x + ball.direction_x) --addx >= max_x or addx <= 1 bounce
- local addy = (ball.y + ball.direction_y)
- if (addx < 4 or addx > max_x-3) then -- in bounds
- if (addx < max_x/2 and (p1 <= addy and addy <= p1+6)) or (addx > max_x/2 and (p2 <= addy and addy <= p2+6)) then -- left side
- ball.direction_x = -ball.direction_x
- end
- end
- if (ball.x < 3) then --out of bounds
- p2_score = p2_score + 1
- ball.x = max_x/2
- ball.y = max_y/2
- elseif (ball.x > max_x-2) then --out of bounds
- p1_score = p1_score + 1
- ball.x = max_x/2
- ball.y = max_y/2
- end
- if p1_score == win or p2_score == win then
- reset()
- write(p1_score>p2_score and "PLAYER 1 WINS" or "PLAYER 2 WINS")
- os.sleep(5)
- p1_score = 0
- p2_score = 0
- return
- end
- if addy >= max_y or addy <= 1 then
- ball.direction_y = -ball.direction_y
- end
- ball.x = ball.x + ball.direction_x
- ball.y = ball.y + ball.direction_y
- paintutils.drawFilledBox(1,1,max_x,max_y,colors.black)
- paintutils.drawLine(max_x/2,1,max_x/2,max_y,colors.gray) -- line
- paintutils.drawFilledBox(2,p1,3,p1+5,colors.white)
- paintutils.drawFilledBox(max_x-2,p2,max_x-3,p2+5,colors.white)
- paintutils.drawPixel(ball.x,ball.y,colors.blue)
- term.setCursorPos((max_x/2)-#tostring(p1_score),1)
- write(p1_score)
- term.setCursorPos((max_x/2)+1,1)
- write(p2_score)
- os.sleep(0.1)
- end
- end
- local function input_control()
- while true do
- local event, key = os.pullEvent()
- if event == "key" then
- pressing[key] = true
- elseif event == "key_up" then
- pressing[key] = false
- end
- end
- end
- while true do
- reset()
- write("PRESS ENTER TO START PONGING\n")
- local _, key = os.pullEvent("key")
- if key == keys.enter then
- write("ENTER WIN NUMBER: ")
- win = tonumber(read())
- if win then
- parallel.waitForAny(update, input_control)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement