Quipex icon

Ping Pong

Quipex | PRO | 04/23/12 01:31:07 PM UTC | 0 ⭐ | 406 👁️ | Never ⏰ | []
Lua |

6.27 KB

|

None

|

0 👍

/

0 👎

--Vars
width, height = term.getSize()
i = 0
i_max = width*height
x = 1
y = 1
p1 = 0
p2 = 0
pad1 = math.ceil(height/2)
pad2 = math.ceil(height/2)
up = false
right = true
 
--Functions
function drawPads()
        term.setCursorPos(1,pad1-1)
        write("#")
        term.setCursorPos(1,pad1)
        write("#")
        term.setCursorPos(1,pad1+1)
        write("#")
        term.setCursorPos(1,pad1-2)
        write(" ")
        term.setCursorPos(1,pad1+2)
        write(" ")
        if pad1 == 4 then
                term.setCursorPos(1,pad1-2)
                write("-")
        end
        if pad1 == 15 then
                term.setCursorPos(1,pad1+2)
                write("-")
        end
        
        term.setCursorPos(width-1,pad2-1)
        write("#")
        term.setCursorPos(width-1,pad2)
        write("#")
        term.setCursorPos(width-1,pad2+1)
        write("#")
        term.setCursorPos(width-1,pad2-2)
        write(" ")
        term.setCursorPos(width-1,pad2+2)
        write(" ")
        if pad2 == 4 then
                term.setCursorPos(width-1,pad2-2)
                write("-")
        end
        if pad2 == 15 then
                term.setCursorPos(width-1,pad2+2)
                write("-")
        end
end
 
function ball()
        term.setCursorPos(x,y)
        write(" ")
        if up == true then
                y = y - 1
        else
                y = y + 1
        end
        if right == true then
                if (x == width - 2) then
                        if y == pad2 or y == pad2-1 or y == pad2+1 then
                                x = x - 1
                                right = false
                        elseif up == true and y == pad2-2 then
                                x = x - 1
                                right = false
                        elseif up == false and y == pad2+2 then
                                x = x - 1
                                right = false
                        else
                                x = x + 1
                        end
                else
                                x = x + 1
                end
        else
                if (x == 2) then
                        if y == pad1 or y == pad1-1 or y == pad1+1 then
                                x = x + 1
                                right = true
                        elseif up == true and y == pad1-2 then
                                x = x + 1
                                right = true
                        elseif up == false and y == pad1+2 then
                                x = x + 1
                                right = true
                        else
                                x = x - 1
                        end
                else
                        x = x - 1
                end
        end
        if x == 1 then
                right = true
                p2 = p2 + 1
                x = math.ceil(width/2)
                y = math.ceil(height/2)
                term.setCursorPos(math.ceil(width/2)-3,1)
                write(p1.." | "..p2)
        end
        if x == (width - 1) then
                right = false
                p1 = p1 + 1
                x = math.ceil(width/2)
                y = math.ceil(height/2)
                term.setCursorPos(math.ceil(width/2)-3,1)
                write(p1.." | "..p2)
        end
        if y == 3 then up = false end
        if y == height-2 then up = true end
end
 
--Intro
term.clear()
term.setCursorPos(1,1)
print("")
print("")
print("  X==---------------------------------==X")
print("  |ComputerCraft Pong, made by Chub1337!|")
print("  X==---------------------------------==X")
print("")
print("")
print("")
print("    Player 1 controls: W = up, S = down")
print("")
print("    Player 2 controls: O = up, L = down")
print("")
print("")
print("")
print("")
print("")
print("")
write("Press <Enter> to continue.")
io.read()
 
term.clear()
term.setCursorPos(1,1)
 
write("Player 1")
term.setCursorPos(width-8,1)
write("Player 2")
term.setCursorPos(math.ceil(width/2)-3,1)
write(p1.." | "..p2)
drawPads()
term.setCursorPos(1,height)
write(pad1.."|"..pad2)
 
for i=1,width-1,1 do
        term.setCursorPos(i,2)
        write("-")
end
term.setCursorPos(1,height-1)
for i=1,width-1,1 do
        term.setCursorPos(i,height-1)
        write("-")
end
 
--Pong
x = math.ceil(width/2)
y = math.ceil(height/2)
dir = math.ceil(math.random(0,4))
 
 
if dir == 1 then
        up = true
        right = true
elseif dir == 2 then
        up = true
        right = false
elseif dir == 3 then
        up = false
        right = true
elseif dir == 4 then
        up = false
        right = false
end
 
function timerupdate()
        ball()
        
        term.setCursorPos(7,height)
        write("X:"..x.." ")
        term.setCursorPos(13,height)
        write("Y:"..y.." ")
        term.setCursorPos(18,height)
        write("up:"..tostring(up).." ")
        term.setCursorPos(30,height)
        write("right:"..tostring(right).." ")
        term.setCursorPos(x,y)
        write("O")
        term.setCursorPos(1,height)
        write(pad1.."|"..pad2.." ")
                timer1 = os.startTimer(.1)
end
timer1 = os.startTimer(.1)
while true do
        event, par = os.pullEvent()
        if event == "timer" and par == timer1 then timerupdate() end
        if event == "char" then
                if par == "w" then
                        pad1 = pad1 - 1
                        if pad1 < 4 then
                                pad1 = 4
                        end
                elseif par == "s" then
                        pad1 = pad1 + 1
                        if pad1 > height-3 then
                                pad1 = height-3
                        end
                end
                if par == "o" then
                        pad2 = pad2 - 1
                        if pad2 < 4 then
                                pad2 = 4
                        end
                elseif par == "l" then
                        pad2 = pad2 + 1
                        if pad2 > height-3 then
                                pad2 = height-3
                        end
                end
        end
        drawPads()
end

Comments