Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Author: TheOriginalBIT
- Version: 1.1.2
- Created: 5 May 2013
- Last Update: 5 May 2013
- License:
- COPYRIGHT NOTICE
- Copyright © 2013 Joshua Asbury a.k.a TheOriginalBIT [theoriginalbit@gmail.com]
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
- associated documentation files (the "Software"), to deal in the Software without restriction,
- including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
- copies of the Software, and to permit persons to whom the Software is furnished to do so,
- subject to the following conditions:
- -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
- -Visible credit is given to the original author.
- -The software is distributed in a non-profit way.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
- WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
- COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
- ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- ]]--
- local function clear(col) term.setBackgroundColor(col or colors.black) term.clear() term.setCursorPos(1,1) end
- -- function thanks to Mads... found here: http://www.computercraft.info/forums2/index.php?/topic/11771-print-coloured-text-easily/page__p__105389#entry105389
- 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
- -- modification of Mads' function to get the length of the string without the color modifiers
- local function countFormatters(text) return #(text:gsub("()&(%x)", '')) end
- -- print a color formatted string in the center of the screen
- 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
- -- writes the text at the give location
- local sw, sh = term.getSize()
- local PLAYER_KEYS = {
- [1] = { up = 17, down = 31 },
- [2] = { up = 24, down = 38 }
- }
- local PLAYER_PADDLES = {}
- local BALL = {}
- local PADDLE_HEIGHT = 5
- local keyPressed
- local renderRequired = true
- local lastBallMove = os.clock()
- local function setupBall()
- BALL.pos = { x = math.ceil(sw / 2), y = math.ceil(sh / 2) }
- BALL.dir = { x = (math.random(1,2) == 2 and 1 or -1), y = 0 }
- end
- local function setupPaddles()
- PLAYER_PADDLES[1] = { x = (2) , y = math.ceil(sh / 2 - 2), lastMove = os.clock() }
- PLAYER_PADDLES[2] = { x = (sw - 1) , y = math.ceil(sh / 2 - 2), lastMove = os.clock() }
- end
- local function processInput()
- os.startTimer(0.1) -- input timeout
- local event = { os.pullEvent() }
- if event[1] == 'key' then
- for i = 1, 2 do
- if event[2] == PLAYER_KEYS[i].up or event[2] == PLAYER_KEYS[i].down then
- keyPressed = event[2]
- break
- end
- end
- end
- end
- local function moveBall(x, y)
- BALL.pos.x = BALL.pos.x + x
- BALL.pos.y = BALL.pos.y + y
- end
- local function checkWallCollision()
- return (BALL.pos.y < 1 or BALL.pos.y > sh)
- end
- local function checkPaddleCollision()
- local playerToCheck = BALL.dir.x == 1 and 2 or 1
- for i = 1, 2 do
- local playerX = PLAYER_PADDLES[i].x
- local playerMinY = PLAYER_PADDLES[i].y
- local playerMaxY = playerMinY + 4
- if (BALL.pos.x == playerX and BALL.pos.y >= playerMinY and BALL.pos.y <= playerMaxY) then
- return true
- end
- end
- return false
- end
- local function checkGoalCollision()
- return (BALL.pos.x < 1 or BALL.pos.x > sw)
- end
- local function update()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.black)
- local ballMoved = false
- if os.clock() - lastBallMove >= 0.2 then
- ballMoved = true
- lastBallMove = os.clock()
- moveBall(BALL.dir.x, BALL.dir.y)
- if checkPaddleCollision() then
- BALL.dir.x = (BALL.dir.x == 1 and -1 or 1)
- moveBall(BALL.dir.x, 0)
- ballMoved = false
- elseif checkWallCollision() then
- BALL.dir.y = (BALL.dir.y == 1 and -1 or 1)
- moveBall((BALL.dir.x == 1 and -1 or 1), BALL.dir.y)
- ballMoved = false
- elseif checkGoalCollision() then
- setupBall()
- setupPaddles()
- end
- end
- local paddleMoved = false
- if keyPressed then
- for i = 1, 2 do
- if os.clock() - PLAYER_PADDLES[i].lastMove >= 0.3 then
- PLAYER_PADDLES[i].lastMove = os.clock()
- local moveAmount = keyPressed == PLAYER_KEYS[i].up and -1 or (keyPressed == PLAYER_KEYS[i].down and 1 or 0)
- PLAYER_PADDLES[i].y = PLAYER_PADDLES[i].y + moveAmount
- paddleMoved = true
- break
- end
- end
- keyPressed = nil -- we have processed the key, remove it so we don't process it again
- end
- renderRequired = (paddleMoved or ballMoved)
- end
- local function render()
- if not renderRequired then return end
- -- clear the screen
- term.setBackgroundColor(colors.black)
- term.clear()
- -- draw the middle line
- term.setTextColor(colors.white)
- for i = 1, sh do
- term.setCursorPos(math.ceil(sw / 2), i)
- write('|')
- end
- -- render our ball
- term.setCursorPos(BALL.pos.x, BALL.pos.y)
- term.setBackgroundColor(colors.white)
- write('O')
- -- render the paddles
- for i = 1, 2 do
- for y = 0, PADDLE_HEIGHT - 1 do
- term.setCursorPos(PLAYER_PADDLES[i].x, PLAYER_PADDLES[i].y + y)
- write('#')
- end
- end
- renderRequired = false
- end
- local function main(argv, argc)
- setupBall()
- setupPaddles()
- render()
- while true do
- processInput()
- update()
- render()
- end
- return true
- end
- -- create a terminal object with a non-advanced computer safe version of setting colors
- local termObj = {
- 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,
- 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
- }
- -- also override the English spelling of the colour functions
- termObj.setTextColour = termObj.setTextColor
- termObj.setBackgroundColour = termObj.setBackgroundColor
- -- make the terminal object refer to the native terminal for every other function
- termObj.__index = term.native
- setmetatable(termObj, termObj)
- -- redirect the terminal to the new object
- term.redirect(termObj)
- -- run the program
- local ok, err = pcall(main, #{...}, {...})
- -- catch-all
- if not ok and err ~= 'Terminated' then
- clear()
- print('Error in runtime!')
- print(err)
- sleep(5)
- end
- -- print thank you message
- clear()
- cwriteWithFormat('&4Thank you for playing CCPong v1.0', 1)
- cwriteWithFormat('&4By &8TheOriginal&3BIT\n', 2)
- -- restore the default terminal object
- term.restore()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement