Advertisement
Redxone

[Pong] Advanced - No Flickr!

Nov 26th, 2015
644
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.86 KB | None | 0 0
  1. c1 = " Easy "
  2. c2 = "Medium"
  3. c3 = " Hard "
  4. c4 = " Comp "
  5. c5 = " Quit "
  6. local w,h = term.getSize()
  7. ms = 1
  8. m = true
  9. loc = 23
  10. trim = 21
  11. ti = 0.12
  12. local tiID = os.startTimer(ti)
  13.  
  14. titleball = {
  15. xpos = math.floor(w/2);
  16. ypos = math.floor(h/2);
  17. xvel = -1;
  18. yvel = -1;
  19.  
  20. update = function(self)
  21. self.xpos = self.xpos + self.xvel
  22. self.ypos = self.ypos + self.yvel
  23.  
  24. if self.xpos <=1 and self.xvel == -1 then
  25. self.xvel = self.xvel * -1
  26. elseif self.xpos >=w and self.xvel == 1 then
  27. self.xvel = self.xvel * -1
  28. elseif self.ypos <=3 and self.yvel == -1 then
  29. self.yvel = self.yvel * -1
  30. elseif self.ypos >=h-2 and self.yvel == 1 then
  31. self.yvel = self.yvel * -1
  32. end
  33. end;
  34.  
  35. draw = function(self)
  36.   term.current().setVisible(false)
  37. term.setBackgroundColor(colors.white)
  38. term.setCursorPos(self.xpos,self.ypos)
  39. term.write(" ")
  40. term.current().setVisible(true)
  41. end
  42. }
  43.  
  44. function draw()
  45. pm()
  46. titleball:draw()
  47. end
  48.  
  49. function update()
  50. local event, button = os.pullEvent()
  51. if event == "key" then
  52. mys(button)
  53. end
  54.  
  55. if event == "timer" and button == tiID then
  56. titleball:update()
  57. tiID = os.startTimer(ti)
  58. end
  59. end
  60.  
  61. function pm()
  62.   term.current().setVisible(false)
  63. term.setTextColor(colors.yellow)
  64. term.setBackgroundColor(colors.green)
  65. term.clear()
  66. bg = paintutils.loadImage("pongworld")
  67. paintutils.drawImage(bg,1,1)
  68. term.setBackgroundColor(colors.lime)
  69. term.setCursorPos(loc,5)
  70. print(c1)
  71. term.setCursorPos(loc,6)
  72. print(c2)
  73. term.setCursorPos(loc,7)
  74. print(c3)
  75. term.setCursorPos(loc,8)
  76. print(c4)
  77. term.setCursorPos(loc,9)
  78. print(c5)
  79. if ms == 1 then
  80. term.setCursorPos(trim,5)
  81. print("[ "..c1.." ]")
  82. elseif ms == 2 then
  83. term.setCursorPos(trim,6)
  84. print("[ "..c2.." ]")
  85. elseif ms == 3 then
  86. term.setCursorPos(trim,7)
  87. print("[ "..c3.." ]")
  88. elseif ms == 4 then
  89. term.setCursorPos(trim,8)
  90. print("[ "..c4.." ]")
  91. elseif ms == 5 then
  92. term.setCursorPos(trim,9)
  93. print("[ "..c5.." ]")
  94. end
  95. term.current().setVisible(true)
  96. mys()
  97. end
  98. term.setTextColor(colors.white)
  99.  
  100. function mys(button)
  101. if button == keys.up then
  102.     if ms ~= 1 then
  103.     ms = ms - 1
  104.     pm()
  105.     end
  106. end
  107.  
  108. if button == keys.down then
  109.     if ms ~= 5 then
  110.     ms = ms + 1
  111.     pm()
  112.     end
  113. end
  114.  
  115. if button == keys.enter then
  116.     if ms == 1 then
  117. gameRate = 0.16
  118. run = true
  119. ball:update()
  120. gtID = os.startTimer(gameRate)
  121. return gameLoop()
  122. elseif ms == 2 then
  123. gameRate = 0.12
  124. run = true
  125. ball:update()
  126. gtID = os.startTimer(gameRate)
  127. return gameLoop()
  128. elseif ms == 3 then
  129. gameRate = 0.06
  130. run = true
  131. ball:update()
  132. gtID = os.startTimer(gameRate)
  133. return gameLoop()
  134. elseif ms == 4 then
  135. gameRate = 0.12
  136. cRate = 0.16
  137. run = true
  138. ball:update()
  139. gtID = os.startTimer(gameRate)
  140. comp = true
  141. coID = os.startTimer(cRate)
  142. return compLoop()
  143. elseif ms == 5 then
  144. m = false
  145. return
  146. end
  147. end
  148. end
  149.  
  150. function loop()
  151. while m do
  152. draw()
  153. update()
  154. end
  155. end
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166. -------------------------------------------------------------------
  167. run = true
  168. local score = 0
  169. p1p = 0
  170. p2p = 0
  171. wp = 10
  172. ball = {
  173. xpos = math.max(math.random(3,40),0);
  174. ypos = math.max(math.random(3,15),0);
  175. xvel = -1;
  176. yvel = -1;
  177.  
  178. update = function(self)
  179. self.xpos = self.xpos + self.xvel
  180. self.ypos = self.ypos + self.yvel
  181.  
  182. if self.ypos >= bat.ypos-1 and self.ypos <= bat.ypos + bat.height and self.xpos == bat.xpos+1 then
  183. self.xvel = self.xvel * -1
  184. end
  185.  
  186. if self.ypos >= bat2.ypos-1 and self.ypos <= bat2.ypos + bat2.height and self.xpos == bat2.xpos-1 then
  187. self.xvel = self.xvel * -1
  188. end
  189.  
  190. if self.xpos <=1 and self.xvel == -1 then
  191.  
  192. if p2p == wp-1 then
  193. textx = math.floor(w/2-8);
  194. term.setCursorPos(textx,5)
  195. term.setBackgroundColor(colors.black)
  196. term.setTextColor(colors.yellow)
  197. print "Player 2 Wins!"
  198. sleep(1)
  199. run = false
  200. m = false
  201. else
  202. p2p = p2p + 1
  203. term.setBackgroundColor(colors.red)
  204. term.clear()
  205. sleep(1)
  206. self.xpos = math.max(math.random(3,40),0);
  207. self.ypos = math.max(math.random(3,15),0);
  208. term.setBackgroundColor(colors.green)
  209. end
  210. elseif self.xpos >=w and self.xvel == 1 then
  211.  
  212. if p1p == wp-1 then
  213. p1p = p1p + 1
  214. drawGame()
  215. textx = math.floor(w/2-8);
  216. term.setCursorPos(textx,5)
  217. term.setBackgroundColor(colors.black)
  218. term.setTextColor(colors.yellow)
  219. print "Player 1 Wins!"
  220. sleep(1)
  221. run = false
  222. m = false
  223. else
  224. p1p = p1p + 1
  225. term.setBackgroundColor(colors.red)
  226. term.clear()
  227. sleep(1)
  228. self.xpos = math.max(math.random(3,40),0);
  229. self.ypos = math.max(math.random(3,15),0);
  230. term.setBackgroundColor(colors.green)
  231. end
  232. elseif self.ypos <=3 and self.yvel == -1 then
  233. self.yvel = self.yvel * -1
  234. elseif self.ypos >=h-2 and self.yvel == 1 then
  235. self.yvel = self.yvel * -1
  236. end
  237. end;
  238.  
  239. draw = function(self)
  240. term.setBackgroundColor(colors.white)
  241. term.setCursorPos(self.xpos,self.ypos)
  242. term.write(" ")
  243. end
  244. }
  245.  
  246. bat = {
  247. xpos = 1;
  248. ypos = 1;
  249. height = 4;
  250.  
  251. update = function(self,key)
  252.  if key == keys.w and self.ypos ~=3 then
  253.  self.ypos = self.ypos - 1
  254.  elseif key == keys.s and self.ypos ~= 18-self.height then
  255.  self.ypos = self.ypos + 1
  256.  
  257. end
  258. end;
  259.  
  260. updatedrag = function(self,_y)
  261.    if _y ~=3 and _y ~= 18-self.height then
  262.       self.ypos = _y
  263.    end
  264.  
  265. end;
  266.  
  267. draw = function (self)
  268. term.setBackgroundColor(colors.blue)
  269. term.setCursorPos(self.xpos,self.ypos)
  270. term.setTextColor(colors.black)
  271. term.write(" ")
  272. term.setCursorPos(self.xpos,self.ypos+1)
  273. term.write(" ")
  274. term.setCursorPos(self.xpos,self.ypos+2)
  275. term.write(" ")
  276. term.setCursorPos(self.xpos,self.ypos+3)
  277. term.write(" ")
  278. end
  279.  
  280. }
  281.  
  282. bat2 = {
  283. xpos = w;
  284. ypos = 1;
  285. height = 4;
  286.  
  287. update = function(self,key)
  288.  if key == keys.up and self.ypos ~=3 then
  289.  self.ypos = self.ypos - 1
  290.  elseif key == keys.down and self.ypos ~= 18-self.height then
  291.  self.ypos = self.ypos + 1
  292.  
  293. end
  294. end;
  295.  
  296. updatedrag = function(self,_y)
  297.    if _y ~=3 and _y ~= 18-self.height then
  298.       self.ypos = _y
  299.    end
  300. end;
  301.  
  302. draw = function (self)
  303. term.setBackgroundColor(colors.red)
  304. term.setTextColor(colors.black)
  305. term.setCursorPos(self.xpos,self.ypos)
  306. term.write(" ")
  307. term.setCursorPos(self.xpos,self.ypos+1)
  308. term.write(" ")
  309. term.setCursorPos(self.xpos,self.ypos+2)
  310. term.write(" ")
  311. term.setCursorPos(self.xpos,self.ypos+3)
  312. term.write(" ")
  313. end
  314.  
  315. }
  316.  
  317. -- if self.ypos == 3 then
  318. --self.ypos = self.ypos - 1
  319. --elseif self.ypos == 18-self.height then
  320. --self.ypos = self.ypos + 1
  321. --else
  322.  
  323. cball = {
  324. xpos = math.max(math.random(3,40),0);
  325. ypos = math.max(math.random(3,15),0);
  326. xvel = -1;
  327. yvel = -1;
  328.  
  329. update = function(self)
  330. self.xpos = self.xpos + self.xvel
  331. self.ypos = self.ypos + self.yvel
  332.  
  333. if self.ypos >= bat.ypos-1 and self.ypos <= bat.ypos + bat.height and self.xpos == bat.xpos+1 then
  334. self.xvel = self.xvel * -1
  335. end
  336.  
  337. if self.ypos >= comP.ypos-1 and self.ypos <= comP.ypos + comP.height and self.xpos == comP.xpos-1 then
  338. self.xvel = self.xvel * -1
  339. end
  340.  
  341. if self.xpos <=1 and self.xvel == -1 then
  342.  
  343. if p2p == wp-1 then
  344. textx = math.floor(w/2-8);
  345. term.setCursorPos(textx,5)
  346. term.setBackgroundColor(colors.black)
  347. term.setTextColor(colors.yellow)
  348. print "Computer Wins!"
  349. sleep(1)
  350. run = false
  351. m = false
  352. else
  353. p2p = p2p + 1
  354. term.setBackgroundColor(colors.red)
  355. term.clear()
  356. sleep(1)
  357. self.xpos = math.max(math.random(3,40),0);
  358. self.ypos = math.max(math.random(3,15),0);
  359. term.setBackgroundColor(colors.green)
  360. gtID = os.startTimer(gameRate)
  361. coID = os.startTimer(cRate)
  362. return compLoop()
  363. end
  364. elseif self.xpos >=w and self.xvel == 1 then
  365.  
  366. if p1p == wp-1 then
  367. p1p = p1p + 1
  368. drawGame()
  369. textx = math.floor(w/2-8);
  370. term.setCursorPos(textx,5)
  371. term.setBackgroundColor(colors.black)
  372. term.setTextColor(colors.yellow)
  373. print "Player 1 Wins!"
  374. sleep(1)
  375. run = false
  376. m = false
  377. else
  378. p1p = p1p + 1
  379. term.setBackgroundColor(colors.red)
  380. term.clear()
  381. sleep(1)
  382. self.xpos = math.max(math.random(3,40),0);
  383. self.ypos = math.max(math.random(3,15),0);
  384. term.setBackgroundColor(colors.green)
  385. gtID = os.startTimer(gameRate)
  386. coID = os.startTimer(cRate)
  387. return compLoop()
  388. end
  389. elseif self.ypos <=3 and self.yvel == -1 then
  390. self.yvel = self.yvel * -1
  391. elseif self.ypos >=h-2 and self.yvel == 1 then
  392. self.yvel = self.yvel * -1
  393. end
  394. end;
  395.  
  396. draw = function(self)
  397. term.setBackgroundColor(colors.white)
  398. term.setCursorPos(self.xpos,self.ypos)
  399. term.write(" ")
  400. end
  401. }
  402.  
  403. comP = {
  404. xpos = w;
  405. ypos = 1;
  406. height = 4;
  407.  
  408. update = function(self,key)
  409. lose = math.max(math.random(1,5),0)
  410.   if cball.yvel == 1 then
  411.    if lose == 4 or lose == 5 then
  412.          if self.ypos <= 3 then
  413.          self.ypos = 4
  414.          end
  415.      else
  416.      
  417.    if self.ypos <= 3 then
  418.   self.ypos = 4
  419.   else
  420.   self.ypos = self.ypos + 1
  421.   end
  422.   end
  423.    
  424.   elseif cball.yvel == -1 then
  425.     if lose == 4 or lose == 5 then
  426.        if comP.ypos >= 14 then
  427.          comP.ypos = 13
  428.          end
  429.     else
  430.    
  431.   if self.ypos >= 14 then
  432.      self.ypos = 13
  433.      else
  434.      self.ypos = self.ypos - 1
  435.      end
  436.      end
  437.    
  438.       end
  439.     end;
  440.  
  441. draw = function (self)
  442. term.setBackgroundColor(colors.red)
  443. term.setTextColor(colors.black)
  444. term.setCursorPos(self.xpos,self.ypos)
  445. term.write(" ")
  446. term.setCursorPos(self.xpos,self.ypos+1)
  447. term.write(" ")
  448. term.setCursorPos(self.xpos,self.ypos+2)
  449. term.write(" ")
  450. term.setCursorPos(self.xpos,self.ypos+3)
  451. term.write(" ")
  452. term.setBackgroundColor(colors.black)
  453. end
  454.  
  455. }
  456.  
  457.  
  458. function drawComp()
  459.   term.current().setVisible(false)
  460. term.setBackgroundColor(colors.green)
  461. term.clear()
  462. bg = paintutils.loadImage("pongworld")
  463. paintutils.drawImage(bg,1,1)
  464. term.setBackgroundColor(colors.white)
  465. term.setTextColor(colors.black)
  466. term.setCursorPos(22,4)
  467. print(""..p1p)
  468. term.setCursorPos(25,4)
  469. print(""..p2p)
  470. cball:draw()
  471. bat:draw()
  472. comP:draw()
  473. term.current().setVisible(true)
  474. end
  475.  
  476.  
  477. function drawGame()
  478.   term.current().setVisible(false)
  479. term.setBackgroundColor(colors.green)
  480. term.clear()
  481. bg = paintutils.loadImage("pongworld")
  482. paintutils.drawImage(bg,1,1)
  483. term.setBackgroundColor(colors.white)
  484. term.setTextColor(colors.black)
  485. term.setCursorPos(22,4)
  486. print(""..p1p)
  487. term.setCursorPos(25,4)
  488. print(""..p2p)
  489. ball:draw()
  490. bat:draw()
  491. bat2:draw()
  492. term.current().setVisible(true)
  493. end
  494.  
  495. function gameUpdate()
  496. local event, button, _,my = os.pullEvent()
  497.  
  498. if event == "timer" and button == gtID then
  499. ball:update()
  500. gtID = os.startTimer(gameRate)
  501. end
  502. if event == "key" then
  503. bat:update(button)
  504.   bat2:update(button)
  505.    if button == keys.backspace then run = false  m = false end
  506. end
  507.  
  508. end
  509.  
  510. function compUpdate()
  511. local event, button, _,_y = os.pullEvent()
  512. if event == "timer" and button == gtID then
  513. cball:update()
  514. gtID = os.startTimer(gameRate)
  515. end
  516.  
  517. if event == "mouse_drag"  then
  518.    bat:updatedrag(_y)
  519. end
  520.  
  521.  
  522. if event == "timer" and button == coID then
  523.  if comP.ypos >= 14 then
  524.          comP.ypos = 13
  525.          end
  526.          
  527. comP:update()
  528. coID = os.startTimer(cRate)
  529. end
  530.  
  531. if event == "key" then
  532. bat:update(button)
  533.    if button == keys.backspace then run = false  m = false end
  534. end
  535. end
  536.  
  537.  
  538. function compLoop()
  539. while run == true do
  540. m = false
  541. drawComp()
  542. compUpdate()
  543. end
  544. while not run do
  545. break
  546. end
  547. end
  548.  
  549. function gameLoop()
  550. while run == true do
  551. m = false
  552. drawGame()
  553. gameUpdate()
  554. end
  555. while not run do
  556. break
  557. end
  558. end
  559. loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement