Guest User

bricks

a guest
May 8th, 2018
687
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.13 KB | None | 0 0
  1. -- GhastTearz' modified version of
  2. -- brickbreaker by TheZipCreator
  3.  
  4. -- Global Constants
  5. local width, height = term.getSize()
  6. local paddleWidth = 6
  7. local keyDelay = 0.1
  8. local delay = 0.15
  9. local brickRows = 3
  10.  
  11. local backgroundColor = colors.black
  12. local textColor       = colors.white
  13. local paddleColor     = colors.white
  14. local scoreLineColor  = colors.lightGray
  15. local brickColor      = colors.magenta
  16. local ballColor       = colors.white
  17.  
  18. -- Global variables
  19. local paddle
  20. local score
  21. local bricks
  22. local ballx
  23. local bally
  24. local ballvx
  25. local ballvy
  26. local gameOver
  27.  
  28. function restart()
  29.   paddle = math.floor((width - paddleWidth) / 2)
  30.  
  31.   score  = 0
  32.  
  33.   ballx  = math.floor( width / 2)
  34.   bally  = height - 8
  35.  
  36.   ballvx = 1
  37.   ballvy = 1
  38.  
  39.   gameOver = false
  40.  
  41.   -- Fill in the brick array, which is a
  42.   -- 2D array that records if the brick still
  43.   -- exists at each location.
  44.   bricks = {}
  45.   for x = 1, width do
  46.     bricks[x] = {}
  47.     for y = 2, brickRows + 1 do
  48.       bricks[x][y] = true
  49.     end
  50.   end
  51.      
  52. end
  53.  
  54.  
  55.  
  56. -- Draws the initial screen, then from there
  57. -- things are changed character by character.
  58. function initialDraw()
  59.   term.setBackgroundColor(backgroundColor)
  60.   term.clear()
  61.  
  62.   --Draw paddle
  63.   term.setBackgroundColor(paddleColor)
  64.   for i=0, paddleWidth  do
  65.     term.setCursorPos(paddle+i, height)
  66.     term.write(" ")
  67.   end
  68.  
  69.   --draw score line
  70.   term.setBackgroundColor(scoreLineColor)
  71.   for i = 1, width do
  72.     term.setCursorPos(i, 1)
  73.     term.write(" ")
  74.   end
  75.   term.setTextColor(textColor)
  76.   term.setCursorPos(1,1)
  77.   term.write(score)
  78.  
  79.   --draw the bricks. Since it's the initial draw
  80.   --we don't have to check if the bricks exist
  81.   term.setBackgroundColor(brickColor)
  82.   for x = 1, width do
  83.     for y = 2, brickRows + 1 do
  84.       term.setCursorPos(x,y)
  85.       term.write(" ")
  86.     end
  87.   end
  88.  
  89.   -- draw the ball  
  90.   term.setBackgroundColor(ballColor)
  91.   term.setCursorPos(math.floor(ballx), math.floor(bally))
  92.   term.write(" ")
  93.  
  94. end
  95.  
  96.  
  97. function drawScore()
  98.   term.setBackgroundColor(backgroundColor)
  99.   term.setTextColor(textColor)
  100.   term.clear()
  101.   centertext("Final Score: "..score, height/2)
  102.   centertext("Press R to restart", (height/2)+1)
  103. end
  104.  
  105.  
  106. function moveBall()
  107.   -- First we need to check to see if the
  108.   -- ball hits a brick. We have 2 cases, either the
  109.   -- brick is directly above the ball, or the brick
  110.   -- is directly in the path of the ball (for
  111.   -- diagonal collisions)  
  112.  
  113.   local breakBrickX, breakBrickY
  114.  
  115.   -- brick above ball
  116.   if bricks[ballx][bally - 1] == true then
  117.     breakBrickX = ballx
  118.     breakBrickY = bally - 1
  119.     bounce(1)
  120.   -- brick in path of ball
  121.   elseif bricks[ballx+ballvx][bally+ballvy] == true then
  122.     breakBrickX = ballx + ballvx
  123.     breakBrickY = bally + ballvy
  124.     bounce(1)
  125.     bounce(2)
  126.   end
  127.  
  128.   if breakBrickX ~= nil and breakBrickY ~= nil then
  129.     bricks[breakBrickX][breakBrickY] = false
  130.    
  131.     -- draw a blank space over the broken brick
  132.     term.setBackgroundColor(backgroundColor)
  133.     term.setCursorPos(breakBrickX, breakBrickY)
  134.     term.write(" ")
  135.    
  136.     -- print new score. Since score only gets
  137.     -- bigger, we don't need to worry about
  138.     -- redrawing the whole score line
  139.     score = score + 1
  140.     term.setBackgroundColor(scoreLineColor)
  141.     term.setTextColor(textColor)
  142.     term.setCursorPos(1,1)
  143.     term.write(score)
  144.   end
  145.  
  146.   local oldx = ballx
  147.   local oldy = bally
  148.   ballx = ballx + ballvx
  149.   bally = bally + ballvy
  150.  
  151.  
  152.   -- ball hits the paddle
  153.   if bally == height - 1
  154.       and ballx >= paddle
  155.       and ballx <= paddle + paddleWidth then
  156.     bounce(1)
  157.   end
  158.  
  159.   -- ball hits top wall
  160.   if bally == 2 then
  161.     bounce(1)
  162.   end
  163.  
  164.   -- ball hits right wall
  165.   if ballx >= width then
  166.     bounce(2)
  167.   end
  168.  
  169.   -- ball hits left wall
  170.   if ballx <= 1 then
  171.     bounce(2)
  172.   end
  173.  
  174.   --Now draw the new position of the ball
  175.   term.setBackgroundColor(backgroundColor)
  176.   term.setCursorPos(oldx, oldy)
  177.   term.write(" ")
  178.   term.setBackgroundColor(ballColor)
  179.   term.setCursorPos(math.floor(ballx),math.floor(bally))
  180.   term.write(" ")
  181. end
  182.  
  183.  
  184.  
  185. function bounce(dir)
  186.   if dir == 1 then
  187.     ballvy = -ballvy
  188.   end
  189.   if dir == 2 then
  190.     ballvx = -ballvx
  191.   end
  192. end
  193.  
  194.  
  195. -- direction of -1 is left and 1 is right
  196. -- drawing is done by removing a character from
  197. -- one end of the paddle and drawing a new
  198. -- character at the other end.
  199. function movePaddle( direction )
  200.  
  201.   local removeX, addX
  202.  
  203.   -- calculate the new paddle position and also
  204.   -- find the positions of the chars that need to
  205.   -- be added and removed  
  206.   if direction == 1
  207.       and paddle + paddleWidth <  width then
  208.     removeX = paddle
  209.     addX    = paddle + paddleWidth + 1
  210.     paddle  = paddle + 1
  211.        
  212.   elseif direction == -1
  213.       and paddle > 1 then
  214.     removeX = paddle + paddleWidth
  215.     addX    = paddle - 1
  216.     paddle  = paddle - 1
  217.   else
  218.     return
  219.   end
  220.  
  221.   -- Draw the new paddle
  222.   term.setBackgroundColor(backgroundColor)
  223.   term.setCursorPos(removeX, height)
  224.   term.write(" ")
  225.  
  226.   term.setBackgroundColor(paddleColor)
  227.   term.setCursorPos(addX, height)
  228.   term.write(" ")
  229. end
  230.  
  231.  
  232. function centertext(text, height)
  233.   term.setCursorPos((width/2)-(string.len(text)/2), height)
  234.   term.write(text)  
  235. end
  236.  
  237.  
  238.  
  239. -- Main Game Loop
  240. restart()
  241. initialDraw()
  242.  
  243. -- every delay seconds a timer goes off which
  244. -- moves the ball
  245. local timerID = os.startTimer(delay)
  246.  
  247. while true do
  248.  
  249.   event, value = os.pullEvent()
  250.  
  251.   if event == "key" then
  252.    
  253.     if value == keys.r then
  254.       restart()
  255.       initialDraw()
  256.     end
  257.    
  258.     if value == keys.q then
  259.       term.setBackgroundColor(colors.black)
  260.       term.setTextColor(colors.white)
  261.       term.clear()
  262.       term.setCursorPos(1,1)
  263.       return
  264.     end
  265.    
  266.     if value == keys.right
  267.         or value == keys.d then
  268.       movePaddle(1)
  269.     end
  270.    
  271.     if value == keys.left
  272.         or value == keys.a then
  273.       movePaddle(-1)
  274.     end
  275.   end
  276.  
  277.   if event == "timer" and timerID == value then
  278.     moveBall()
  279.     timerID = os.startTimer(delay)
  280.   end
  281.  
  282.   if bally > height then
  283.     drawScore()
  284.   end
  285.  
  286. end
Advertisement
Add Comment
Please, Sign In to add comment