Advertisement
appollon41

Pong

Apr 26th, 2022 (edited)
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.49 KB | None | 0 0
  1. mon = peripheral.wrap("top")
  2. mon.setBackgroundColor(colors.black)
  3. mon.setTextScale(0.5)
  4. mon.setCursorPos(1,1)
  5. mon.clear()
  6.  
  7. --mon.write(x.." "..y)
  8.  
  9. --variables
  10. x,y = mon.getSize() --5x2 screen: x = 100, y = 38 | non scalable on this alpha
  11.  
  12. score1 = 0 --P1's score
  13. score2 = 0 --P2's score
  14.  
  15. dx = 1
  16. dy = 0
  17.  
  18. y1 = 19
  19. y2 = 19
  20.  
  21. pos = {} --pos of the ball
  22. pos[1] = 50
  23. pos[2] = 19
  24.  
  25. ite1 = 1
  26. ite2 = 1
  27.  
  28. multip = 1
  29. --functions
  30. function updater(y1,y2,score1,score2)
  31. mon.clear()
  32.  local i = -2
  33.  local j = 0
  34.  
  35.  while(i<=3) do
  36.   mon.setCursorPos(3,y1+i)
  37.   mon.setBackgroundColor(colors.white)
  38.   mon.write(" ")
  39.   mon.setCursorPos(4,y1+i)
  40.   mon.write(" ")
  41.   i = i + 1
  42.   mon.setBackgroundColor(colors.black)
  43.  end
  44.  
  45.  i = -2
  46.  
  47.  while(i<=3) do
  48.   mon.setCursorPos(97,y2+i)
  49.   mon.setBackgroundColor(colors.white)
  50.   mon.write(" ")
  51.   mon.setCursorPos(98,y2+i)
  52.   mon.write(" ")
  53.   i = i + 1
  54.   mon.setBackgroundColor(colors.black)
  55.  end
  56.  
  57.  i = 3
  58.  
  59.  mon.setCursorPos(50,i)
  60.  
  61.  while (i<=38) do
  62.   if(j==0) then
  63.    mon.setCursorPos(50,i)
  64.    mon.setBackgroundColor(colors.white)
  65.    mon.write(" ")
  66.    j = 1
  67.   else
  68.    mon.setCursorPos(50,i)
  69.    mon.setBackgroundColor(colors.black)
  70.    j = 0
  71.   end
  72.   i = i+1
  73.  end
  74.  mon.setBackgroundColor(colors.gray)
  75.  
  76.  i = 1
  77.  
  78.  
  79.  while(i<=100) do
  80.   mon.setCursorPos(i,1)
  81.   mon.write(" ")
  82.   i = i + 1
  83.  end
  84.  
  85.  i = 1
  86.  
  87.  while(i<=100) do
  88.   mon.setCursorPos(i,38)
  89.   mon.write(" ")
  90.   i = i + 1
  91.  end
  92.  
  93.  --affichage
  94.  
  95.  mon.setBackgroundColor(colors.black)
  96.  
  97.  if(score1==nil) then score1 = 0 end
  98.   mon.setCursorPos(40,3)
  99.   if(score1<10) and (score1>=0) then mon.write("0"..score1) else mon.write(""..score1) end
  100.  if(score2==nil) then score2 = 0 end
  101.   mon.setCursorPos(59,3)
  102.   if(score2<10) and (score2>=0) then mon.write("0"..score2) else mon.write(""..score2) end
  103.  
  104.  return y1, y2, score1, score2
  105.  
  106. end
  107.  
  108. --initialisation
  109.  
  110.  
  111.  
  112. --start of the game
  113.  
  114. --function initBall(dx,dy)
  115. --pos[1] = 50
  116. --pos[2] = 19
  117. --pos[1] = pos[1]+ dx
  118. --pos[2] = pos[2]+ dy *(39/100)
  119. --end
  120.  
  121. function processBall() -- args dx,dy,pos,y1,y2
  122.  
  123.  
  124.  if(pos[2]>=37 or pos[2]<=2) then dy = -dy end -- high / low limit detected
  125.  
  126.  
  127. if(pos[1]<=6) then
  128.  noAngle = 0
  129.  
  130.  if(math.abs(pos[2])==y1 or math.abs(pos[2])==y1+1) then
  131.   dx = -dx
  132.   angle = 0
  133.  elseif(math.abs(pos[2])==y1-1 or math.abs(pos[2])==y1+2)then
  134.   angle = 45
  135.  elseif(math.abs(pos[2])==y1-2 or math.abs(pos[2])==y1+3)then
  136.   angle = 60
  137.  end
  138.  
  139.  if(dy==0) then
  140.   dx = -dx
  141.   noAngle = 1
  142.  elseif(dy<0 and noAngle == 0) then
  143.   dx = math.ceil((math.cos(math.sqrt(dx))*(math.cos(math.rad(angle)))-math.sin(math.sqrt(dx))*math.sin(math.rad(angle)))^2)
  144.   dy = math.ceil(-((math.sin(math.sqrt(dy))*(math.cos(math.rad(angle)))+math.sin(math.sqrt(dy))*math.cos(math.rad(angle)))^2))
  145.  elseif(dy>0 and noAngle == 0) then
  146.   dx = math.ceil((((math.cos(math.sqrt(dx))))*((math.cos(math.rad(angle))))-(math.rad(math.sqrt(dx)))*math.sin(math.rad(angle)))^2)
  147.   dy = math.ceil(-((math.sin(math.sqrt(dy)))*(math.cos(math.rad(angle)))+(math.sin(math.sqrt(dy)))*math.cos(math.rad(angle)))^2)
  148.  end
  149.  
  150.  
  151.  elseif(pos[1]>=95)then
  152.   noAngle = 0
  153.  
  154.   if(math.abs(pos[2])==y2 or math.abs(pos[2])==y2+1) then
  155.    dx = -dx
  156.    angle = 0
  157.   elseif(math.abs(pos[2])==y2-1 or math.abs(pos[2])==y2+2)then
  158.    angle = 45
  159.   elseif(math.abs(pos[2])==y2-2 or math.abs(pos[2])==y2+3) then
  160.   angle = 60
  161.   end
  162.  
  163.   if(dy==0) then
  164.    dx = -dx
  165.    noAngle = 1
  166.   elseif(dy<0 and noAngle == 0) then
  167.    dx = math.floor(((math.cos(math.sqrt(dx)))*(math.cos(math.rad(angle)))-(math.sin(math.sqrt(dx)))*math.sin(math.rad(angle)))^2)
  168.    dy = math.floor(-((math.sin(math.sqrt(dy)))*((math.cos(math.rad(angle))))+(math.sin(math.sqrt(dy)))*math.cos(math.rad(angle)))^2)
  169.   elseif(dy>0 and noAngle == 0) then
  170.    dx = math.floor(((math.cos(math.sqrt(dx)))*((math.cos(math.rad(angle))))-(math.sin(math.sqrt(dx)))*math.sin(math.rad(angle)))^2)
  171.    dy = math.floor(-(math.sin((math.sqrt(dy)))*((math.cos(math.rad(angle))))+(math.sin(math.sqrt(dy)))*math.cos(math.rad(angle)))^2)
  172.   end
  173. end
  174.  
  175. if(pos[1]<2) then --score
  176.     pos[1] = 50
  177.     pos[2] = 19
  178.     score2 = score2 + 1
  179.     dx = 1
  180.     dy = 0
  181.     y1 = 19
  182.     y2 = 19
  183.    end
  184.    
  185. if (pos[1]>=98) then
  186.     pos[1] = 50
  187.     pos[2] = 19
  188.     score1 = score1+1
  189.     dx = -1
  190.     dy = 0
  191.     y1 = 19
  192.     y2 = 19
  193. end
  194.  
  195.  
  196.  
  197.    
  198.  
  199.  pos[1] = pos[1] + dx
  200.  pos[2] = pos[2] + dy
  201.  print("pos1 : ".. dx)
  202.  print("pos2 : ".. pos[2])
  203.  
  204.  --angle = 60
  205.  --dx = (math.cos(math.sqrt(dx))*(math.cos(math.rad(angle)))-math.sin(math.rad(math.sqrt(dx)))*math.sin(math.rad(angle)))^2
  206.  --dy = -(math.sin(math.sqrt(dy))*(math.cos(math.rad(angle)))+math.sin(math.rad(math.sqrt(dy)))*math.cos(math.rad(angle)))^2
  207.  
  208.  
  209.  
  210.  mon.setTextColor(colors.yellow)
  211.  mon.setCursorPos(pos[1],pos[2])
  212.  mon.write("()")
  213.  mon.setTextColor(colors.white)
  214. end
  215.  
  216.  
  217.  
  218. --touch updater()
  219.  
  220. function touch()
  221.  
  222. while true do
  223.  
  224. event, side, xPos, yPos = os.pullEvent("monitor_touch")
  225.  
  226. if xPos<50 then
  227.  if yPos<y1 then
  228.  
  229.   if y1>=6 then
  230.    y1 = y1-1
  231.   end
  232.  else
  233.   if y1<=32 then
  234.    y1 = y1+1
  235.   end
  236.   end
  237.  elseif xPos>50 then
  238.   if yPos<y2 then
  239.   if y2>=6 then
  240.    y2 = y2-1
  241.   end  
  242.   else
  243.   if y2<=32 then
  244.    y2 = y2+1
  245.   end  
  246.   end
  247.  
  248. end
  249.  
  250. end
  251.  
  252.  
  253. end
  254. --main call
  255.  
  256. --main loop
  257. function main_loop()
  258. ite3 = 0
  259.  while true do
  260. --test
  261. y1,y2,score1,score2 = updater(y1,y2,score1,score2)
  262.  
  263. processBall() -- args dx,dy,pos,y1,y2
  264.  
  265. if ite3%10 == 0 then
  266. print("y1 = "..y1)
  267. print("y2 = "..y2)
  268. print(" ")
  269. end
  270.  os.sleep(0.1)
  271.  end
  272. end
  273.  
  274. parallel.waitForAny(main_loop,touch)
  275.  
  276. --copyright appollon41 v1.0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement