Advertisement
Guest User

startup

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