-- GhastTearz' modified version of -- brickbreaker by TheZipCreator -- Global Constants term.redirect(peripheral.wrap("top")) local width, height = term.getSize() local paddleWidth = 6 local keyDelay = 0.1 local delay = 0.15 local brickRows = 3 local backgroundColor = colors.black local textColor = colors.white local paddleColor = colors.white local scoreLineColor = colors.lightGray local brickColor = colors.magenta local ballColor = colors.white -- Global variables local paddle local score local bricks local ballx local bally local ballvx local ballvy local gameOver function restart() paddle = math.floor((width - paddleWidth) / 2) score = 0 ballx = math.floor( width / 2) bally = height - 8 ballvx = 1 ballvy = 1 gameOver = false -- Fill in the brick array, which is a -- 2D array that records if the brick still -- exists at each location. bricks = {} for x = 1, width do bricks[x] = {} for y = 2, brickRows + 1 do bricks[x][y] = true end end end -- Draws the initial screen, then from there -- things are changed character by character. function initialDraw() term.setBackgroundColor(backgroundColor) term.clear() --Draw paddle term.setBackgroundColor(paddleColor) for i=0, paddleWidth do term.setCursorPos(paddle+i, height) term.write(" ") end --draw score line term.setBackgroundColor(scoreLineColor) for i = 1, width do term.setCursorPos(i, 1) term.write(" ") end term.setTextColor(textColor) term.setCursorPos(1,1) term.write(score) --draw the bricks. Since it's the initial draw --we don't have to check if the bricks exist term.setBackgroundColor(brickColor) for x = 1, width do for y = 2, brickRows + 1 do term.setCursorPos(x,y) term.write(" ") end end -- draw the ball term.setBackgroundColor(ballColor) term.setCursorPos(math.floor(ballx), math.floor(bally)) term.write(" ") end function drawScore() term.setBackgroundColor(backgroundColor) term.setTextColor(textColor) term.clear() centertext("Final Score: "..score, height/2) centertext("Press the top of the screen to restart", (height/2)+1) end function moveBall() -- First we need to check to see if the -- ball hits a brick. We have 2 cases, either the -- brick is directly above the ball, or the brick -- is directly in the path of the ball (for -- diagonal collisions) local breakBrickX, breakBrickY -- brick above ball if bricks[ballx][bally - 1] == true then breakBrickX = ballx breakBrickY = bally - 1 bounce(1) -- brick in path of ball elseif bricks[ballx+ballvx][bally+ballvy] == true then breakBrickX = ballx + ballvx breakBrickY = bally + ballvy bounce(1) bounce(2) end if breakBrickX ~= nil and breakBrickY ~= nil then bricks[breakBrickX][breakBrickY] = false -- draw a blank space over the broken brick term.setBackgroundColor(backgroundColor) term.setCursorPos(breakBrickX, breakBrickY) term.write(" ") -- print new score. Since score only gets -- bigger, we don't need to worry about -- redrawing the whole score line score = score + 1 term.setBackgroundColor(scoreLineColor) term.setTextColor(textColor) term.setCursorPos(1,1) term.write(score) end local oldx = ballx local oldy = bally ballx = ballx + ballvx bally = bally + ballvy -- ball hits the paddle if bally == height - 1 and ballx >= paddle and ballx <= paddle + paddleWidth then bounce(1) end -- ball hits top wall if bally == 2 then bounce(1) end -- ball hits right wall if ballx >= width then bounce(2) end -- ball hits left wall if ballx <= 1 then bounce(2) end --Now draw the new position of the ball term.setBackgroundColor(backgroundColor) term.setCursorPos(oldx, oldy) term.write(" ") term.setBackgroundColor(ballColor) term.setCursorPos(math.floor(ballx),math.floor(bally)) term.write(" ") end function bounce(dir) if dir == 1 then ballvy = -ballvy end if dir == 2 then ballvx = -ballvx end end -- direction of -1 is left and 1 is right -- drawing is done by removing a character from -- one end of the paddle and drawing a new -- character at the other end. function movePaddle( direction ) local removeX, addX -- calculate the new paddle position and also -- find the positions of the chars that need to -- be added and removed if direction == 1 and paddle + paddleWidth < width then removeX = paddle addX = paddle + paddleWidth + 1 paddle = paddle + 1 elseif direction == -1 and paddle > 1 then removeX = paddle + paddleWidth addX = paddle - 1 paddle = paddle - 1 else return end -- Draw the new paddle term.setBackgroundColor(backgroundColor) term.setCursorPos(removeX, height) term.write(" ") term.setBackgroundColor(paddleColor) term.setCursorPos(addX, height) term.write(" ") end function centertext(text, height) term.setCursorPos((width/2)-(string.len(text)/2), height) term.write(text) end -- Main Game Loop restart() initialDraw() -- every delay seconds a timer goes off which -- moves the ball local timerID = os.startTimer(delay) while true do event, value, x, y = os.pullEvent() if event == "monitor_touch" then if y >= 50 then restart() initialDraw() end if y >= 100 then term.setBackgroundColor(colors.black) term.setTextColor(colors.white) term.clear() term.setCursorPos(1,1) return end if x >= 10 or value == keys.d then movePaddle(1) end if x <= 10 or value == keys.a then movePaddle(-1) end end if event == "timer" and timerID == value then moveBall() timerID = os.startTimer(delay) end if bally > height then drawScore() end end