Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- https://pastebin.com/nyMwe2HK
- -- pastebin get nyMwe2HK startup
- -- autoloader config
- -- pastebin get XcX1FQmw startup
- -- {pastebin='nyMwe2HK ', program='stacker'}
- pcall(require, 'computercraft')
- local HEIGHT = 19
- local WIDTH = 18
- local MAX_SCORE = 7
- local click_stack = {}
- local ball = {}
- local paddle2 = {}
- local paddle1 = {}
- local predict2 = 1
- local predict1 = 1
- local display = {}
- local state = 0
- local round = 0
- local countin = 0
- local score1 = 0
- local score2 = 0
- local ballstart = HEIGHT-1
- function connectMonitor()
- for k,v in pairs(redstone.getSides()) do
- if peripheral.getType(v) == 'monitor' then
- return peripheral.wrap(v)
- end
- end
- print('monitor not found')
- return false
- end
- function delay(seconds)
- -- safe version of 'sleep' - will requeue dropped events
- local timer = os.startTimer(seconds)
- local q = {}
- while true do
- local data = {os.pullEvent()}
- if data[1] == "timer" and data[2] == timer then
- break
- else
- table.insert(q, data)
- end
- end
- for i,v in ipairs(q) do
- os.queueEvent(unpack(v))
- end
- end
- function drawBackground(display)
- display.setTextScale(1)
- display.setCursorPos(1,1)
- for i=1,HEIGHT do
- display.setBackgroundColor(colors.white)
- display.setCursorPos(1,i)
- display.write(string.rep(" ",WIDTH))
- end
- end
- function drawball()
- drawBackground(display)
- display.setCursorPos(ball.x, ball.y)
- display.setBackgroundColor(colors.red)
- display.write(" ")
- end
- function drawpaddle(paddle)
- display.setCursorPos(paddle.x, paddle.y)
- display.setBackgroundColor(colors.blue)
- display.write(string.rep(" ",paddle.width))
- end
- function drawMessage(msg)
- display.setBackgroundColor(colors.green)
- display.setCursorPos(1, HEIGHT/2)
- display.write(string.rep(" ",WIDTH))
- display.setTextColor(colors.white)
- display.setCursorPos(2, HEIGHT/2)
- display.write(msg)
- end
- function parseStartClick()
- while #click_stack > 0 do
- local pos = table.remove(click_stack)
- -- if close to paddle1, move paddel1
- return 1
- end
- return 0
- end
- function parseClicks()
- while #click_stack > 0 do
- local pos = table.remove(click_stack)
- -- if close to paddle1, move paddel1
- if pos.y <= 3 then
- if pos.x < paddle1.x then
- paddle1.x = paddle1.x - 1
- end
- if pos.x >= (paddle1.x + paddle1.width) then
- paddle1.x = paddle1.x + 1
- end
- end
- -- if close to paddle2, move paddel2
- if pos.y >= HEIGHT-3 then
- if pos.x < paddle2.x then
- paddle2.x = paddle2.x - 1
- end
- if pos.x >= (paddle2.x + paddle2.width) then
- paddle2.x = paddle2.x + 1
- end
- end
- end
- end
- function moveBall()
- if ball.y == 1 or ball.y == HEIGHT then
- return ball.y==1 and 1 or 2;
- else
- if ball.diry == 1 then
- ball.y = ball.y + .5
- else
- ball.y = ball.y - .5
- end
- if ball.dirx == 1 then
- ball.x = ball.x + 1
- else
- ball.x = ball.x - 1
- end
- --x bounce
- if ball.x == WIDTH then
- ball.dirx = -1
- end
- if ball.x == 1 then
- ball.dirx = 1
- end
- end
- --y bounce
- if ball.y == HEIGHT-1 and ball.diry == 1 then
- print("2)", ball.x, paddle2.x, paddle2.x+paddle2.width-1)
- if ball.x >= paddle2.x and ball.x <= (paddle2.x+paddle2.width-1) then
- ball.diry = 1
- end
- end
- if ball.y == 2 and ball.diry == -1 then
- print("1)", ball.x, paddle1.x, paddle1.x+paddle1.width)
- if ball.x >= paddle1.x and ball.x <= (paddle1.x+paddle1.width-1) then
- ball.diry = -1
- end
- end
- return 0
- end
- function predictBall()
- local targetPosition = 0
- if ball.y == 2 and ball.diry == 1 then
- if ball.dirx == 1 then
- targetPosition = ball.x - 38
- else
- targetPosition = ball.x + 38
- end
- predict2 = (targetPosition ) % WIDTH
- if predict2 == 0 then
- predict2 = WIDTH
- end
- print(predict2)
- end
- if ball.y == HEIGHT-1 and ball.diry == -1 then
- if ball.dirx == 1 then
- targetPosition = ball.x - 38
- else
- targetPosition = ball.x + 38
- end
- predict1 = (targetPosition ) % WIDTH
- if predict2 == 0 then
- predict2 = WIDTH
- end
- print(predict1)
- end
- end
- function autoPlay()
- if predict1 < paddle1.x then
- paddle1.x = paddle1.x - 1
- print("1 go left")
- elseif predict1 >= (paddle1.x+paddle1.width-1) then
- paddle1.x = paddle1.x + 1
- print("1 go right")
- end
- if predict2 < paddle2.x then
- paddle2.x = paddle2.x - 1
- print("2 go left")
- elseif predict2 >= (paddle2.x+paddle2.width-1) then
- paddle2.x = paddle2.x + 1
- print("2 go right")
- end
- end
- function init()
- -- which side the ball starts on
- local startDir=1
- if ballstart == HEIGHT-1 then
- ballstart = 2
- startDir = 1
- else
- ballstart = HEIGHT-1
- startDir = -1
- end
- print(ballstart)
- ball = {
- x=2,
- y=ballstart,
- dirx=1,
- diry=startDir,
- }
- paddle1 = {
- x=1,
- y=1,
- width=5,
- }
- paddle2 = {
- x=1,
- y=HEIGHT,
- width=5,
- }
- end
- function tick()
- -- table is started
- if state == 0 then
- init()
- state = 1
- -- is waiting to start game
- elseif state == 1 then
- moveBall()
- predictBall()
- autoPlay()
- drawball()
- drawpaddle(paddle1)
- drawpaddle(paddle2)
- drawMessage("Click to start")
- if parseStartClick() == 1 then
- init()
- state = 2
- countin = 10
- end
- -- countdown to start
- elseif state == 2 then
- drawball()
- drawpaddle(paddle1)
- drawpaddle(paddle2)
- drawMessage("Starting.. "..countin)
- -- drawScore(score1, score2)
- countin = countin - 1
- if countin == 0 then
- parseStartClick() -- resets the clicks stack
- state = 3
- end
- -- game is running
- elseif state == 3 then
- -- moves the paddle
- parseClicks()
- -- moves the ball and returns if the ball is scored
- local pointScored = moveBall()
- -- if no point is scored
- if pointScored == 0 then
- drawball()
- drawpaddle(paddle1)
- drawpaddle(paddle2)
- -- drawScore(score1, score2)
- else
- if pointScored == 1 then
- score1 = score1+1
- else
- score2 = score2+1
- end
- -- pause to show the scored point
- delay(1)
- -- go back to countdown to start
- round = round + 1
- -- either end game OR new round
- if score1 == MAX_SCORE or score2 == MAX_SCORE then
- state = 4
- else
- init()
- countin = 10
- state = 2
- end
- end
- elseif state == 4 then
- -- show win screen
- delay(1)
- -- go back to init
- state = 1
- end
- delay(.1)
- end
- function waitforclicks()
- event, button, x, y = os.pullEvent("monitor_touch")
- table.insert(click_stack, {x=x, y=y})
- end
- function main()
- display = connectMonitor()
- init()
- while true do
- parallel.waitForAny(tick, waitforclicks)
- end
- end
- main()
RAW Paste Data