Advertisement
nitrogenfingers

breakout

Feb 5th, 2013
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. local w,h = term.getSize()
  2. local running = true
  3.  
  4. local refreshRate = 0.15
  5. local gtID = os.startTimer(refreshRate)
  6.  
  7. wall = {
  8. bricks = { };
  9.  
  10. initialize = function(self)
  11. local wallcolours = {
  12. [3] = colours.lime;
  13. [4] = colours.yellow;
  14. [5] = colours.orange;
  15. [6] = colours.red;
  16. }
  17.  
  18. for i=3,6 do
  19. self.bricks[i] = { }
  20. for x=1,w do
  21. self.bricks[i][x] = wallcolours[i]
  22. end
  23. end
  24. end;
  25. update = function(self) end;
  26. draw = function(self)
  27. for yval,v in pairs(self.bricks) do
  28. for xval,color in pairs(v) do
  29. term.setCursorPos(xval,yval)
  30. term.setBackgroundColour(color)
  31. term.write(" ")
  32. end
  33. end
  34. end;
  35. }
  36.  
  37. ball = {
  38. xpos = math.floor(w/2);
  39. ypos = math.floor(h/2);
  40. xvel = 1;
  41. yvel = -1;
  42.  
  43. update = function(self)
  44. self.xpos = self.xpos + self.xvel
  45. self.ypos = self.ypos + self.yvel
  46.  
  47. if self.ypos == bat.ypos - 1 and
  48. self.xpos >= bat.xpos and self.xpos <= bat.xpos + bat.width - 1 then
  49. self.yvel = self.yvel * -1
  50. end
  51.  
  52. if self.xpos <= 1 and self.xvel == -1 then
  53. self.xvel = self.xvel * -1
  54. elseif self.xpos >= w and self.xvel == 1 then
  55. self.xvel = self.xvel * -1
  56. end
  57. if self.ypos <= 1 and self.yvel == -1 then
  58. self.yvel = self.yvel * -1
  59. elseif self.ypos >= h and self.yvel == 1 then
  60. running = false
  61. end
  62.  
  63. --Vertical collisions
  64. if wall.bricks[self.ypos + self.yvel] and
  65. wall.bricks[self.ypos + self.yvel][self.xpos] ~= nil then
  66. for i=-1,1 do
  67. wall.bricks[self.ypos + self.yvel][self.xpos + i] = nil
  68. end
  69. self.yvel = self.yvel * -1
  70. end
  71.  
  72. --Horizontal collisions
  73. if wall.bricks[self.ypos] and
  74. wall.bricks[self.ypos][self.xpos + self.xvel] ~= nil then
  75. for i=-1,1 do
  76. if wall.bricks[self.ypos + i] then
  77. wall.bricks[self.ypos + i][self.xpos + self.xvel] = nil
  78. end
  79. end
  80. self.xvel = self.xvel * -1
  81. end
  82.  
  83. --Diagonal collisions
  84. if wall.bricks[self.ypos + self.yvel] and
  85. wall.bricks[self.ypos + self.yvel][self.xpos + self.xvel] ~= nil then
  86. wall.bricks[self.ypos + self.yvel][self.xpos + self.xvel] = nil
  87. self.xvel = self.xvel * -1
  88. self.yvel = self.yvel * -1
  89. end
  90. end;
  91.  
  92. draw = function(self)
  93. term.setCursorPos(self.xpos, self.ypos)
  94. term.setBackgroundColour(colours.white)
  95. term.write(" ")
  96. end;
  97. }
  98.  
  99. bat = {
  100. xpos = math.floor(w/2);
  101. ypos = h-1;
  102. width = 5;
  103.  
  104. update = function(self, key)
  105. if key == keys.left and self.xpos > 1 then
  106. self.xpos = self.xpos-1
  107. elseif key == keys.right and self.xpos < w - self.width + 1 then
  108. self.xpos = self.xpos+1
  109. end
  110. end;
  111.  
  112. draw = function(self)
  113. term.setCursorPos(self.xpos, self.ypos)
  114. term.setBackgroundColour(colours.white)
  115. term.write(string.rep(" ", self.width))
  116. end
  117. }
  118.  
  119. function drawGame()
  120. term.setBackgroundColour(colours.black)
  121. term.clear()
  122. wall:draw()
  123. bat:draw()
  124. ball:draw()
  125. end
  126.  
  127. function updateGame()
  128. local id,p1 = os.pullEvent()
  129.  
  130. if id == "key" then
  131. if p1 == keys.q then running = false end
  132. bat:update(p1)
  133. elseif id == "timer" and p1 == gtID then
  134. ball:update()
  135. wall:update()
  136. gtID = os.startTimer(refreshRate)
  137. end
  138. end
  139.  
  140. function gameLoop()
  141. while running do
  142. drawGame()
  143. updateGame()
  144. end
  145. end
  146.  
  147. wall:initialize()
  148. gameLoop()
  149. term.setBackgroundColour(colours.black)
  150. shell.run("clear")
  151. sleep(0.01)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement