Advertisement
Mackan90096

Untitled

Mar 28th, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.29 KB | None | 0 0
  1. --[[
  2. Author: TheOriginalBIT
  3. Version: 1.1.2
  4. Created: 5 May 2013
  5. Last Update: 5 May 2013
  6.  
  7. License:
  8.  
  9. COPYRIGHT NOTICE
  10. Copyright © 2013 Joshua Asbury a.k.a TheOriginalBIT [theoriginalbit@gmail.com]
  11.  
  12. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  13. associated documentation files (the "Software"), to deal in the Software without restriction,
  14. including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  15. copies of the Software, and to permit persons to whom the Software is furnished to do so,
  16. subject to the following conditions:
  17.  
  18. -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  19. -Visible credit is given to the original author.
  20. -The software is distributed in a non-profit way.
  21.  
  22. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  23. WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  24. COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  25. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. ]]--
  27.  
  28. local function clear(col) term.setBackgroundColor(col or colors.black) term.clear() term.setCursorPos(1,1) end
  29. -- function thanks to Mads... found here: http://www.computercraft.info/forums2/index.php?/topic/11771-print-coloured-text-easily/page__p__105389#entry105389
  30. local function writeWithFormat(...) local s = "&0" for k, v in ipairs(arg) do s = s .. v end s = s .. "&0" local fields = {} local lastcolor, lastpos = "0", 0 for pos, clr in s:gmatch"()&(%x)" do table.insert(fields, {s:sub(lastpos + 2, pos - 1), lastcolor}) lastcolor, lastpos = clr , pos end for i = 2, #fields do term.setTextColor(2 ^ (tonumber(fields[i][2], 16))) write(fields[i][1]) end end
  31. -- modification of Mads' function to get the length of the string without the color modifiers
  32. local function countFormatters(text) return #(text:gsub("()&(%x)", '')) end
  33. -- print a color formatted string in the center of the screen
  34. local function cwriteWithFormat(text, y) local sw,sh = term.getSize() local _,cy = term.getCursorPos() term.setCursorPos((sw-countFormatters(text))/2+(countFormatters(text) % 2 == 0 and 1 or 0), y or cy) writeWithFormat(text) end
  35. -- writes the text at the give location
  36.  
  37. local sw, sh = term.getSize()
  38.  
  39. local PLAYER_KEYS = {
  40. [1] = { up = 17, down = 31 },
  41. [2] = { up = 24, down = 38 }
  42. }
  43.  
  44. local PLAYER_PADDLES = {}
  45. local BALL = {}
  46.  
  47. local PADDLE_HEIGHT = 5
  48.  
  49. local keyPressed
  50. local renderRequired = true
  51.  
  52. local lastBallMove = os.clock()
  53.  
  54. local function setupBall()
  55. BALL.pos = { x = math.ceil(sw / 2), y = math.ceil(sh / 2) }
  56. BALL.dir = { x = (math.random(1,2) == 2 and 1 or -1), y = 0 }
  57. end
  58.  
  59. local function setupPaddles()
  60. PLAYER_PADDLES[1] = { x = (2) , y = math.ceil(sh / 2 - 2), lastMove = os.clock() }
  61. PLAYER_PADDLES[2] = { x = (sw - 1) , y = math.ceil(sh / 2 - 2), lastMove = os.clock() }
  62. end
  63.  
  64. local function processInput()
  65. os.startTimer(0.1) -- input timeout
  66. local event = { os.pullEvent() }
  67. if event[1] == 'key' then
  68. for i = 1, 2 do
  69. if event[2] == PLAYER_KEYS[i].up or event[2] == PLAYER_KEYS[i].down then
  70. keyPressed = event[2]
  71. break
  72. end
  73. end
  74. end
  75. end
  76.  
  77. local function moveBall(x, y)
  78. BALL.pos.x = BALL.pos.x + x
  79. BALL.pos.y = BALL.pos.y + y
  80. end
  81.  
  82. local function checkWallCollision()
  83. return (BALL.pos.y < 1 or BALL.pos.y > sh)
  84. end
  85.  
  86. local function checkPaddleCollision()
  87. local playerToCheck = BALL.dir.x == 1 and 2 or 1
  88. for i = 1, 2 do
  89. local playerX = PLAYER_PADDLES[i].x
  90. local playerMinY = PLAYER_PADDLES[i].y
  91. local playerMaxY = playerMinY + 4
  92. if (BALL.pos.x == playerX and BALL.pos.y >= playerMinY and BALL.pos.y <= playerMaxY) then
  93. return true
  94. end
  95. end
  96.  
  97. return false
  98. end
  99.  
  100. local function checkGoalCollision()
  101. return (BALL.pos.x < 1 or BALL.pos.x > sw)
  102. end
  103.  
  104. local function update()
  105. term.setCursorPos(1, 1)
  106. term.setTextColor(colors.black)
  107.  
  108. local ballMoved = false
  109. if os.clock() - lastBallMove >= 0.2 then
  110. ballMoved = true
  111. lastBallMove = os.clock()
  112. moveBall(BALL.dir.x, BALL.dir.y)
  113. if checkPaddleCollision() then
  114. BALL.dir.x = (BALL.dir.x == 1 and -1 or 1)
  115. moveBall(BALL.dir.x, 0)
  116. ballMoved = false
  117. elseif checkWallCollision() then
  118. BALL.dir.y = (BALL.dir.y == 1 and -1 or 1)
  119. moveBall((BALL.dir.x == 1 and -1 or 1), BALL.dir.y)
  120. ballMoved = false
  121. elseif checkGoalCollision() then
  122. setupBall()
  123. setupPaddles()
  124. end
  125. end
  126.  
  127. local paddleMoved = false
  128. if keyPressed then
  129. for i = 1, 2 do
  130. if os.clock() - PLAYER_PADDLES[i].lastMove >= 0.3 then
  131. PLAYER_PADDLES[i].lastMove = os.clock()
  132. local moveAmount = keyPressed == PLAYER_KEYS[i].up and -1 or (keyPressed == PLAYER_KEYS[i].down and 1 or 0)
  133. PLAYER_PADDLES[i].y = PLAYER_PADDLES[i].y + moveAmount
  134. paddleMoved = true
  135. break
  136. end
  137. end
  138. keyPressed = nil -- we have processed the key, remove it so we don't process it again
  139. end
  140.  
  141. renderRequired = (paddleMoved or ballMoved)
  142. end
  143.  
  144. local function render()
  145. if not renderRequired then return end
  146.  
  147. -- clear the screen
  148. term.setBackgroundColor(colors.black)
  149. term.clear()
  150.  
  151. -- draw the middle line
  152. term.setTextColor(colors.white)
  153. for i = 1, sh do
  154. term.setCursorPos(math.ceil(sw / 2), i)
  155. write('|')
  156. end
  157.  
  158.  
  159. -- render our ball
  160. term.setCursorPos(BALL.pos.x, BALL.pos.y)
  161. term.setBackgroundColor(colors.white)
  162. write('O')
  163.  
  164. -- render the paddles
  165. for i = 1, 2 do
  166. for y = 0, PADDLE_HEIGHT - 1 do
  167. term.setCursorPos(PLAYER_PADDLES[i].x, PLAYER_PADDLES[i].y + y)
  168. write('#')
  169. end
  170. end
  171.  
  172. renderRequired = false
  173. end
  174.  
  175. local function main(argv, argc)
  176. setupBall()
  177. setupPaddles()
  178. render()
  179.  
  180. while true do
  181. processInput()
  182. update()
  183. render()
  184. end
  185.  
  186. return true
  187. end
  188.  
  189. -- create a terminal object with a non-advanced computer safe version of setting colors
  190. local termObj = {
  191. setTextColor = function(n) if term.isColor and term.isColor() then local ok, err = pcall(term.native.setTextColor , n) if not ok then error(err, 2) end end end,
  192. setBackgroundColor = function(n) if term.isColor and term.isColor() then local ok, err = pcall(term.native.setBackgroundColor , n) if not ok then error(err, 2) end end end
  193. }
  194. -- also override the English spelling of the colour functions
  195. termObj.setTextColour = termObj.setTextColor
  196. termObj.setBackgroundColour = termObj.setBackgroundColor
  197.  
  198. -- make the terminal object refer to the native terminal for every other function
  199. termObj.__index = term.native
  200. setmetatable(termObj, termObj)
  201.  
  202. -- redirect the terminal to the new object
  203. term.redirect(termObj)
  204.  
  205. -- run the program
  206. local ok, err = pcall(main, #{...}, {...})
  207.  
  208. -- catch-all
  209. if not ok and err ~= 'Terminated' then
  210. clear()
  211. print('Error in runtime!')
  212. print(err)
  213. sleep(5)
  214. end
  215.  
  216. -- print thank you message
  217. clear()
  218. cwriteWithFormat('&4Thank you for playing CCPong v1.0', 1)
  219. cwriteWithFormat('&4By &8TheOriginal&3BIT\n', 2)
  220.  
  221. -- restore the default terminal object
  222. term.restore()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement