Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local sx, sy = term.getSize()
- local pong1pos, pong2pos, ballxpos, ballypos, ballxvelocity, ballyvelocity, p1score, p2score = math.ceil(sy / 2), math.ceil(sy / 2), math.ceil(sx / 2), math.ceil(sy / 2), math.ceil(math.random() - 1), math.ceil(math.random() - 1), 0, 0
- if ballxvelocity == 0 then
- ballxvelocity = -1
- end
- if ballyvelocity == 0 then
- ballyvelocity = -1
- end
- local function gameGraphics()
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setBackgroundColor(colors.white)
- for a = -1, 1 do
- term.setCursorPos(4, pong1pos + a)
- io.write(" ")
- term.setCursorPos(sx - 3, pong2pos + a)
- io.write(" ")
- end
- term.setCursorPos(ballxpos, ballypos)
- io.write(" ")
- term.setCursorPos(1, 1)
- io.write(p1score)
- term.setCursorPos(sx, 1)
- io.write(p2score)
- end
- local function gameTime()
- local timer = os.startTimer(0.07)
- local input1, input2
- while true do
- local event, par1 = os.pullEvent()
- if event == "timer" and par1 == timer then
- if input1 == "up" then
- pong1pos = pong1pos - 2
- elseif input1 == "down" then
- pong1pos = pong1pos + 2
- end
- if input2 == "up" then
- pong2pos = pong2pos - 2
- elseif input2 == "down" then
- pong2pos = pong2pos + 2
- end
- break
- elseif event == "key" then
- if par1 == keys.up and pong1pos > 2 then
- input2 = "up"
- elseif par1 == keys.down and pong1pos < sy - 1 then
- input2 = "down"
- end
- if par1 == keys.w and pong2pos > 2 then
- input1 = "up"
- elseif par1 == keys.s and pong2pos < sy - 1 then
- input1 = "down"
- end
- end
- end
- end
- local function gameFunctions()
- ballxpos, ballypos = ballxpos + ballxvelocity, ballypos + ballyvelocity
- if ballypos == 1 then
- ballyvelocity = 1
- elseif ballypos == sy then
- ballyvelocity = -1
- end
- if ballxpos == 5 and ballypos >= pong1pos - 1 and ballypos <= pong1pos + 1 then
- ballxvelocity = 1
- elseif ballxpos == sx - 4 and ballypos >= pong2pos - 1 and ballypos <= pong2pos + 1 then
- ballxvelocity = -1
- end
- end
- while true do
- while true do
- gameGraphics()
- gameFunctions()
- gameTime()
- if ballxpos == 1 then
- p2score = p2score + 1
- pong1pos, pong2pos, ballxpos, ballypos, ballxvelocity, ballyvelocity = math.ceil(sy / 2), math.ceil(sy / 2), math.ceil(sx / 2), math.ceil(sy / 2), math.ceil(math.random() - 1), math.ceil(math.random() - 1)
- if ballxvelocity == 0 then
- ballxvelocity = -1
- end
- if ballyvelocity == 0 then
- ballyvelocity = -1
- end
- elseif ballxpos == sx then
- p1score = p1score + 1
- pong1pos, pong2pos, ballxpos, ballypos, ballxvelocity, ballyvelocity = math.ceil(sy / 2), math.ceil(sy / 2), math.ceil(sx / 2), math.ceil(sy / 2), math.ceil(math.random() - 1), math.ceil(math.random() - 1)
- if ballxvelocity == 0 then
- ballxvelocity = -1
- end
- if ballyvelocity == 0 then
- ballyvelocity = -1
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement