Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- mon = peripheral.wrap("top")
- mon.setBackgroundColor(colors.black)
- mon.setTextScale(0.5)
- mon.setCursorPos(1,1)
- mon.clear()
- --mon.write(x.." "..y)
- --variables
- x,y = mon.getSize() --5x2 screen: x = 100, y = 38 | non scalable on this alpha
- score1 = 0 --P1's score
- score2 = 0 --P2's score
- dx = 1
- dy = 0
- y1 = 19
- y2 = 19
- pos = {} --pos of the ball
- pos[1] = 50
- pos[2] = 19
- ite1 = 1
- ite2 = 1
- multip = 1
- --functions
- function updater(y1,y2,score1,score2)
- mon.clear()
- local i = -2
- local j = 0
- while(i<=3) do
- mon.setCursorPos(3,y1+i)
- mon.setBackgroundColor(colors.white)
- mon.write(" ")
- mon.setCursorPos(4,y1+i)
- mon.write(" ")
- i = i + 1
- mon.setBackgroundColor(colors.black)
- end
- i = -2
- while(i<=3) do
- mon.setCursorPos(97,y2+i)
- mon.setBackgroundColor(colors.white)
- mon.write(" ")
- mon.setCursorPos(98,y2+i)
- mon.write(" ")
- i = i + 1
- mon.setBackgroundColor(colors.black)
- end
- i = 3
- mon.setCursorPos(50,i)
- while (i<=38) do
- if(j==0) then
- mon.setCursorPos(50,i)
- mon.setBackgroundColor(colors.white)
- mon.write(" ")
- j = 1
- else
- mon.setCursorPos(50,i)
- mon.setBackgroundColor(colors.black)
- j = 0
- end
- i = i+1
- end
- mon.setBackgroundColor(colors.gray)
- i = 1
- while(i<=100) do
- mon.setCursorPos(i,1)
- mon.write(" ")
- i = i + 1
- end
- i = 1
- while(i<=100) do
- mon.setCursorPos(i,38)
- mon.write(" ")
- i = i + 1
- end
- --affichage
- mon.setBackgroundColor(colors.black)
- if(score1==nil) then score1 = 0 end
- mon.setCursorPos(40,3)
- if(score1<10) and (score1>=0) then mon.write("0"..score1) else mon.write(""..score1) end
- if(score2==nil) then score2 = 0 end
- mon.setCursorPos(59,3)
- if(score2<10) and (score2>=0) then mon.write("0"..score2) else mon.write(""..score2) end
- return y1, y2, score1, score2
- end
- --initialisation
- --start of the game
- --function initBall(dx,dy)
- --pos[1] = 50
- --pos[2] = 19
- --pos[1] = pos[1]+ dx
- --pos[2] = pos[2]+ dy *(39/100)
- --end
- function processBall() -- args dx,dy,pos,y1,y2
- if(pos[2]>=37 or pos[2]<=2) then dy = -dy end -- high / low limit detected
- if(pos[1]<=6) then
- noAngle = 0
- if(math.abs(pos[2])==y1 or math.abs(pos[2])==y1+1) then
- dx = -dx
- angle = 0
- elseif(math.abs(pos[2])==y1-1 or math.abs(pos[2])==y1+2)then
- angle = 45
- elseif(math.abs(pos[2])==y1-2 or math.abs(pos[2])==y1+3)then
- angle = 60
- end
- if(dy==0) then
- dx = -dx
- noAngle = 1
- elseif(dy<0 and noAngle == 0) then
- dx = math.ceil((math.cos(math.sqrt(dx))*(math.cos(math.rad(angle)))-math.sin(math.sqrt(dx))*math.sin(math.rad(angle)))^2)
- dy = math.ceil(-((math.sin(math.sqrt(dy))*(math.cos(math.rad(angle)))+math.sin(math.sqrt(dy))*math.cos(math.rad(angle)))^2))
- elseif(dy>0 and noAngle == 0) then
- dx = math.ceil((((math.cos(math.sqrt(dx))))*((math.cos(math.rad(angle))))-(math.rad(math.sqrt(dx)))*math.sin(math.rad(angle)))^2)
- dy = math.ceil(-((math.sin(math.sqrt(dy)))*(math.cos(math.rad(angle)))+(math.sin(math.sqrt(dy)))*math.cos(math.rad(angle)))^2)
- end
- elseif(pos[1]>=95)then
- noAngle = 0
- if(math.abs(pos[2])==y2 or math.abs(pos[2])==y2+1) then
- dx = -dx
- angle = 0
- elseif(math.abs(pos[2])==y2-1 or math.abs(pos[2])==y2+2)then
- angle = 45
- elseif(math.abs(pos[2])==y2-2 or math.abs(pos[2])==y2+3) then
- angle = 60
- end
- if(dy==0) then
- dx = -dx
- noAngle = 1
- elseif(dy<0 and noAngle == 0) then
- dx = math.floor(((math.cos(math.sqrt(dx)))*(math.cos(math.rad(angle)))-(math.sin(math.sqrt(dx)))*math.sin(math.rad(angle)))^2)
- dy = math.floor(-((math.sin(math.sqrt(dy)))*((math.cos(math.rad(angle))))+(math.sin(math.sqrt(dy)))*math.cos(math.rad(angle)))^2)
- elseif(dy>0 and noAngle == 0) then
- dx = math.floor(((math.cos(math.sqrt(dx)))*((math.cos(math.rad(angle))))-(math.sin(math.sqrt(dx)))*math.sin(math.rad(angle)))^2)
- dy = math.floor(-(math.sin((math.sqrt(dy)))*((math.cos(math.rad(angle))))+(math.sin(math.sqrt(dy)))*math.cos(math.rad(angle)))^2)
- end
- end
- if(pos[1]<2) then --score
- pos[1] = 50
- pos[2] = 19
- score2 = score2 + 1
- dx = 1
- dy = 0
- y1 = 19
- y2 = 19
- end
- if (pos[1]>=98) then
- pos[1] = 50
- pos[2] = 19
- score1 = score1+1
- dx = -1
- dy = 0
- y1 = 19
- y2 = 19
- end
- pos[1] = pos[1] + dx
- pos[2] = pos[2] + dy
- print("pos1 : ".. dx)
- print("pos2 : ".. pos[2])
- --angle = 60
- --dx = (math.cos(math.sqrt(dx))*(math.cos(math.rad(angle)))-math.sin(math.rad(math.sqrt(dx)))*math.sin(math.rad(angle)))^2
- --dy = -(math.sin(math.sqrt(dy))*(math.cos(math.rad(angle)))+math.sin(math.rad(math.sqrt(dy)))*math.cos(math.rad(angle)))^2
- mon.setTextColor(colors.yellow)
- mon.setCursorPos(pos[1],pos[2])
- mon.write("()")
- mon.setTextColor(colors.white)
- end
- --touch updater()
- function touch()
- while true do
- event, side, xPos, yPos = os.pullEvent("monitor_touch")
- if xPos<50 then
- if yPos<y1 then
- if y1>=6 then
- y1 = y1-1
- end
- else
- if y1<=32 then
- y1 = y1+1
- end
- end
- elseif xPos>50 then
- if yPos<y2 then
- if y2>=6 then
- y2 = y2-1
- end
- else
- if y2<=32 then
- y2 = y2+1
- end
- end
- end
- end
- end
- --main call
- --main loop
- function main_loop()
- ite3 = 0
- while true do
- --test
- y1,y2,score1,score2 = updater(y1,y2,score1,score2)
- processBall() -- args dx,dy,pos,y1,y2
- if ite3%10 == 0 then
- print("y1 = "..y1)
- print("y2 = "..y2)
- print(" ")
- end
- os.sleep(0.1)
- end
- end
- parallel.waitForAny(main_loop,touch)
- --copyright appollon41 v1.0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement