Advertisement
TIMAS_Bro

pong

Oct 16th, 2023 (edited)
698
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local file = fs.open("lgame/lgame","r")
  2. if file then
  3.     file.close()
  4. os.loadAPI('/lg/lg')
  5. lg.setup('term')
  6. os.loadAPI("uap")
  7. os.loadAPI("/lgame/lgame")
  8. lgame.setup("term")
  9. lg.fillScreen(colors.black)
  10. local im1 = fs.open("/pong/ball.nfp","r")
  11. if im1 then
  12.   im1.close()
  13. else
  14.   shell.run("pastebin","get","cnJQa66z","/pong/wall.nfp")
  15.   shell.run("pastebin","get","sJBDTsfR","/pong/ball.nfp")
  16.   lg.drawText(1,1,colors.white,colors.black,"Downloading sprites...")
  17.   os.sleep(1)
  18.   lg.fillScreen(colors.black)
  19.  
  20. end
  21. lgame.lggSetDefBG(colors.black)
  22. local lefty = 1
  23. lgame.drawSprite('left',2,2,'/pong/wall.nfp')
  24. local spwx = math.random (10, lgame.getSize().x-2)
  25. local spwy = math.random (3, lgame.getSize().y-1)
  26. lgame.drawSprite("ball",spwx,spwy,"/pong/ball.nfp")
  27. local px = spwx
  28. local py = spwy
  29. local dir = math.random (1, 4)
  30. colided = false
  31. local ly = 1
  32. local speed = 0.2
  33. local c = 0
  34. local d = false
  35. os.sleep(0.1)
  36. local function dt(text)
  37.   lgame.fill(1,lgame.getSize().x,lgame.getSize().y,lgame.getSize().y,colors.black)
  38.   lgame.drawText(1,lgame.getSize().y,colors.white,colors.black,text)
  39. end
  40. dt("3")
  41. os.sleep(1)
  42. dt("2")
  43. os.sleep(1)
  44. dt("1")
  45. os.sleep(1)
  46. --lgame.drawText(1,10,colors.white,colors.black,"SIZE: "..lgame.getSize().x.." "..lgame.getSize().y)
  47. function click()
  48.   while true do
  49.   local event, key = os.pullEvent("key")
  50.   if key == keys.w then
  51.     if ly ~= 1 then
  52.     ly = ly - 1
  53.     end
  54.   elseif key == keys.s then
  55.     if lgame.getSpriteColider("left").y2 ~= lgame.getSize().y then
  56.    ly = ly + 1
  57.     end
  58.   elseif key == keys.e then
  59.     os.reboot()
  60.   end
  61.   lgame.moveSprite("left",2,ly)
  62.   end
  63. end
  64. function game()
  65. local i = 1
  66. while true do
  67.     d = false
  68.     --local bal = lgame.getSpriteColider("ball").x
  69.     if lgame.getSpriteColider('ball').x == 1 then
  70.     lg.fillScreen(colors.black)
  71.     lg.drawText(9,9,colors.white,colors.black, 'You lose! R: '..c)
  72.     break
  73.     end
  74.    
  75.   i = i + 1
  76.   if not (px == lgame.getSize().x + 1 and py == lgame.getSize().y+1) then
  77.   if px <= lgame.getSize().x and px > 0 then
  78.     if py <= lgame.getSize().y and py > 0 then
  79.       if lgame.getSpriteColider("ball").x == lgame.getSize().x and dir == 1 then
  80.         dir = 3
  81.       elseif lgame.getSpriteColider("ball").x == lgame.getSize().x and dir == 2 then
  82.         dir = 4
  83.       elseif lgame.getSpriteColider("ball").x == 1 and dir == 4 then
  84.         dir = 2
  85.       elseif lgame.getSpriteColider("ball").x == 1 and dir == 3 then
  86.         dir = 4
  87.       elseif lgame.getSpriteColider("ball").y == lgame.getSize().y and dir == 1 then
  88.         dir = 2  
  89.       elseif lgame.getSpriteColider("ball").y == lgame.getSize().y and dir == 3 then
  90.         dir = 4
  91.       elseif lgame.getSpriteColider("ball").y == 1 and dir == 2 then
  92.         dir = 1
  93.       elseif lgame.getSpriteColider("ball").y == 1 and dir == 4 then
  94.         dir = 3                                    
  95.       end
  96.     elseif py == 0 then
  97.       d = true
  98.       if (dir == 4) then
  99.         dir = 3
  100.       else
  101.       dir = 1
  102.       end
  103.     elseif py == lgame.getSize().y+1 then
  104.       d = true
  105.       if (dir == 3) then
  106.         dir = 4
  107.       else
  108.         dir = 2
  109.       end
  110.     end
  111.   elseif px < 1 then
  112.     d = true
  113.     if (dir == 4) then
  114.       dir = 2
  115.     else
  116.       dir = 1
  117.     end
  118.   elseif px == lgame.getSize().x+1 then
  119.     d = true
  120.     if (dir == 1) then
  121.     dir = 3
  122.     else
  123.     dir = 4
  124.     end
  125.   end
  126.   if dir == 1 then
  127.   px = px + 1
  128.   py = py + 1
  129.   elseif dir == 2 then
  130.   px = px + 1
  131.   py = py - 1
  132.   elseif dir == 3 then
  133.   px = px - 1
  134.   py = py + 1
  135.   elseif dir == 4 then
  136.   px = px - 1
  137.   py = py - 1
  138.   end
  139.   if not d  then
  140.   lgame.moveSprite("ball",px,py)
  141.   end
  142.   os.sleep(speed)
  143.   dt("Score: "..c.." | EXIT - E")
  144.   else
  145.     break
  146.   end
  147.   if colided == false and lgame.isColiding("ball","left") then
  148.     lgame.moveSprite("ball",lgame.getSpriteColider("ball").x+2,lgame.getSpriteColider("ball").y)
  149.     speed = speed - 0.01
  150.     colided = true
  151.     c = c + 1
  152.     if dir == 4 then
  153.       dir = 2
  154.     elseif dir == 3 then
  155.       dir = 1
  156.     end
  157.   else
  158.     colided = false
  159.   end
  160. end
  161. end
  162. parallel.waitForAny(click,game)
  163. lg.drawBT('end',1,4,19,19,colors.white,colors.black,'exit')
  164. while true do
  165.  local event, button, x, y = os.pullEvent( "mouse_click" )
  166.    if (lg.isButton(x,y) == "end") then
  167. os.reboot()
  168. end
  169. end
  170. lg.exit()
  171. lgame.exit()    
  172. else
  173.     term.clear()
  174.     term.setCursorPos(1,2)
  175.     term.write("Install lgame from updater!")
  176.     uap.upd()
  177. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement