Advertisement
Guest User

OCPong lua

a guest
Feb 19th, 2016
3,458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.94 KB | None | 0 0
  1. local term = require "term"
  2. local event = require "event"
  3. local running = false
  4. local p1Name = ""
  5. local p2Name = ""
  6. local timer = 0
  7. local state = 0
  8.  
  9.  
  10. local p1e=0
  11. local p1y=6
  12. local score1=0
  13.  
  14. local p2e=0
  15. local p2y=6
  16. local score2=0
  17.  
  18. local bx=24
  19. local by=9
  20. local bh=-1
  21. local bv=-1
  22.  
  23.  
  24. function unknownEvent()
  25.  
  26. end
  27.  
  28. local myEventHandlers = setmetatable({}, { __index = function() return unknownEvent end })
  29.  
  30. function finish()
  31. running=false
  32. end
  33.  
  34. function init()
  35. term.setCursorBlink(false)
  36. term.setCursor(1,1)
  37. print "Player 1:"
  38. term.setCursor(1,2)
  39. print (p1Name)
  40. term.setCursor(35,1)
  41. print "Player 2:"
  42. term.setCursor(35,2)
  43. print (p2Name)
  44.  
  45. term.setCursor(25,2)
  46. print(score1)
  47. term.setCursor(27,2)
  48. print(score2)
  49. term.setCursor(1,3)
  50. print "█████████████████████████████████████████████████"
  51. term.setCursor(1,15)
  52. print "█████████████████████████████████████████████████"
  53.  
  54. term.setCursor(2,6)
  55. print "█"
  56. term.setCursor(2,7)
  57. print "█"
  58. term.setCursor(2,8)
  59. print "█"
  60. term.setCursor(2,9)
  61. print "█"
  62.  
  63. term.setCursor(48,6)
  64. print "█"
  65. term.setCursor(48,7)
  66. print "█"
  67. term.setCursor(48,8)
  68. print "█"
  69. term.setCursor(48,9)
  70. print "█"
  71.  
  72. local running = true
  73. end
  74.  
  75.  
  76.  
  77. function handleEvent(eventID, ...)
  78. if (eventID) then
  79. myEventHandlers[eventID](...)
  80. end
  81. end
  82.  
  83. function myEventHandlers.key_down(adress, char, code, playerName)
  84. if (code == 57) then
  85. event.cancel(timer)
  86. finish()
  87. end
  88.  
  89. if (playerName == p1Name) then
  90. p1e=code
  91. end
  92. if (playerName == p2Name) then
  93. p2e=code
  94. end
  95.  
  96. end
  97.  
  98. function myEventHandlers.key_up(adress, char, code, playerName)
  99. if (running == false) then
  100. if (p1Name == "") then
  101. p1Name = playerName
  102. term.setCursor(9,13)
  103. print (p1Name)
  104. state=1
  105. else if (p2Name=="") then
  106. p2Name = playerName
  107. term.setCursor(34,13)
  108. print (p2Name)
  109. term.setCursor(20,14)
  110. print "Press ENTER"
  111. term.setCursor(18,10)
  112. print " "
  113. state=2
  114. end
  115. end
  116.  
  117. if (state==2) and (code==28) then
  118. running=true
  119. end
  120.  
  121. else
  122.  
  123. if (playerName == p1Name) and (code == p1e) then
  124. p1e=0
  125. end
  126. if (playerName == p2Name) and (code == p2e) then
  127. p2e=0
  128. end
  129.  
  130. end
  131.  
  132.  
  133. end
  134.  
  135. function loop()
  136.  
  137. if (p1e == 200) and (p1y > 4) then
  138. p1y=p1y-1
  139. term.setCursor(2,p1y+4)
  140. print " "
  141. term.setCursor(2,p1y)
  142. print "█"
  143.  
  144.  
  145. else if (p1e == 208) and (p1y < 11) then
  146. p1y=p1y+1
  147. term.setCursor(2,p1y+3)
  148. print "█"
  149. term.setCursor(2,p1y-1)
  150. print " "
  151. end
  152. end
  153.  
  154. if (p2e == 200) and (p2y > 4) then
  155. p2y=p2y-1
  156. term.setCursor(48,p2y+4)
  157. print " "
  158. term.setCursor(48,p2y)
  159. print "█"
  160.  
  161.  
  162. else if (p2e == 208) and (p2y < 11) then
  163. p2y=p2y+1
  164. term.setCursor(48,p2y+3)
  165. print "█"
  166. term.setCursor(48,p2y-1)
  167. print " "
  168. end
  169. end
  170.  
  171. if (bv == -1) and (by == 4) then
  172. bv=1
  173. elseif (bv == 1) and (by==14) then
  174. bv=-1
  175. end
  176.  
  177. if (bx == 3) then
  178. if (p1y<=(by+bv)) and ((p1y+3)>=(by+bv)) then
  179. bh=1
  180. end
  181. elseif (bx==47) then
  182. if (p2y<=(by+bv)) and ((p2y+3)>=(by+bv)) then
  183. bh=-1
  184. end
  185. end
  186.  
  187. if (bx <= 1) then
  188. term.setCursor(bx,by)
  189. print " "
  190. bx=24
  191. by=9
  192. score2=score2+1
  193. term.setCursor(25,2)
  194. print(score2)
  195. if (score2 == 10) then
  196. term.setCursor(19,9)
  197. print (p2Name .. " wins!")
  198. event.cancel(timer)
  199. timer=event(2,finish)
  200. end
  201. elseif (bx>49) then
  202. term.setCursor(bx,by)
  203. print " "
  204. bx=24
  205. by=9
  206. score1=score1+1
  207. term.setCursor(27,2)
  208. print(score1)
  209. if (score1 == 10) then
  210. term.setCursor(19,9)
  211. print (p1Name .. " wins!")
  212. event.cancel(timer)
  213. timer=event(2,finish)
  214. end
  215. end
  216.  
  217. term.setCursor(bx,by)
  218. print " "
  219. bx=bx+bh
  220. by=by+bv
  221. term.setCursor(bx,by)
  222. print "●"
  223. end
  224.  
  225. term.clear()
  226. term.setCursor(6,3)
  227. print "█████████ ████████ ███ ██ ████████"
  228. term.setCursor(6,4)
  229. print "███ ██ ██ ██ ████ ██ ██"
  230. term.setCursor(6,5)
  231. print "███ ██ ██ ██ ██ ██ ██ ██"
  232. term.setCursor(6,6)
  233. print "█████████ ██ ██ ██ ████ ██ ████"
  234. term.setCursor(6,7)
  235. print "███ ██ ██ ██ ███ ██ ██"
  236. term.setCursor(6,8)
  237. print "███ ████████ ██ ██ ████████"
  238. term.setCursor(18,10)
  239. print "Press any key"
  240. term.setCursor(9,12)
  241. print "Player 1 Player 2"
  242.  
  243. os.sleep(0.1)
  244. while not running do
  245. handleEvent(event.pull())
  246. end
  247.  
  248. term.clear()
  249. init()
  250. timer=event.timer(0.1,loop,math.huge)
  251.  
  252. while running do
  253. handleEvent(event.pull())
  254. end
  255.  
  256.  
  257. event.ignore("key_down",init)
  258. term.clear()
  259. term.setCursor(1,1)
  260. os.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement