Advertisement
theoriginalbit

CCPong

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