Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local w,h = term.getSize()
- local running = true
- local refreshRate = 0.15
- local gtID = os.startTimer(refreshRate)
- wall = {
- bricks = { };
- initialize = function(self)
- local wallcolours = {
- [3] = colours.lime;
- [4] = colours.yellow;
- [5] = colours.orange;
- [6] = colours.red;
- }
- for i=3,6 do
- self.bricks[i] = { }
- for x=1,w do
- self.bricks[i][x] = wallcolours[i]
- end
- end
- end;
- update = function(self) end;
- draw = function(self)
- for yval,v in pairs(self.bricks) do
- for xval,color in pairs(v) do
- term.setCursorPos(xval,yval)
- term.setBackgroundColour(color)
- term.write(" ")
- end
- end
- end;
- }
- ball = {
- xpos = math.floor(w/2);
- ypos = math.floor(h/2);
- xvel = 1;
- yvel = -1;
- update = function(self)
- self.xpos = self.xpos + self.xvel
- self.ypos = self.ypos + self.yvel
- if self.ypos == bat.ypos - 1 and
- self.xpos >= bat.xpos and self.xpos <= bat.xpos + bat.width - 1 then
- self.yvel = self.yvel * -1
- end
- if self.xpos <= 1 and self.xvel == -1 then
- self.xvel = self.xvel * -1
- elseif self.xpos >= w and self.xvel == 1 then
- self.xvel = self.xvel * -1
- end
- if self.ypos <= 1 and self.yvel == -1 then
- self.yvel = self.yvel * -1
- elseif self.ypos >= h and self.yvel == 1 then
- running = false
- end
- --Vertical collisions
- if wall.bricks[self.ypos + self.yvel] and
- wall.bricks[self.ypos + self.yvel][self.xpos] ~= nil then
- for i=-1,1 do
- wall.bricks[self.ypos + self.yvel][self.xpos + i] = nil
- end
- self.yvel = self.yvel * -1
- end
- --Horizontal collisions
- if wall.bricks[self.ypos] and
- wall.bricks[self.ypos][self.xpos + self.xvel] ~= nil then
- for i=-1,1 do
- if wall.bricks[self.ypos + i] then
- wall.bricks[self.ypos + i][self.xpos + self.xvel] = nil
- end
- end
- self.xvel = self.xvel * -1
- end
- --Diagonal collisions
- if wall.bricks[self.ypos + self.yvel] and
- wall.bricks[self.ypos + self.yvel][self.xpos + self.xvel] ~= nil then
- wall.bricks[self.ypos + self.yvel][self.xpos + self.xvel] = nil
- self.xvel = self.xvel * -1
- self.yvel = self.yvel * -1
- end
- end;
- draw = function(self)
- term.setCursorPos(self.xpos, self.ypos)
- term.setBackgroundColour(colours.white)
- term.write(" ")
- end;
- }
- bat = {
- xpos = math.floor(w/2);
- ypos = h-1;
- width = 5;
- update = function(self, key)
- if key == keys.left and self.xpos > 1 then
- self.xpos = self.xpos-1
- elseif key == keys.right and self.xpos < w - self.width + 1 then
- self.xpos = self.xpos+1
- end
- end;
- draw = function(self)
- term.setCursorPos(self.xpos, self.ypos)
- term.setBackgroundColour(colours.white)
- term.write(string.rep(" ", self.width))
- end
- }
- function drawGame()
- term.setBackgroundColour(colours.black)
- term.clear()
- wall:draw()
- bat:draw()
- ball:draw()
- end
- function updateGame()
- local id,p1 = os.pullEvent()
- if id == "key" then
- if p1 == keys.q then running = false end
- bat:update(p1)
- elseif id == "timer" and p1 == gtID then
- ball:update()
- wall:update()
- gtID = os.startTimer(refreshRate)
- end
- end
- function gameLoop()
- while running do
- drawGame()
- updateGame()
- end
- end
- wall:initialize()
- gameLoop()
- term.setBackgroundColour(colours.black)
- shell.run("clear")
- sleep(0.01)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement