Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("OS/Api/draw")
- w, h = term.getSize()
- gameOver = false
- refreshRate = 0.2
- gTimer = os.startTimer(refreshRate)
- gameColor = colors.lime
- textColor = colors.black
- lines = 8
- lives = 3
- bat = {
- xpos = math.floor(w/2);
- ypos = h - 1;
- width = 6;
- update = function(self, key)
- if key == keys.a then
- if self.xpos > 1 then
- self.xpos = self.xpos - 1
- end
- elseif key == keys.d then
- if self.xpos + self.width < w + 1 then
- self.xpos = self.xpos + 1
- end
- elseif key == keys.q then
- gameOver = true
- end
- end;
- draw = function(self)
- term.setCursorPos(bat.xpos, h - 1)
- term.setBackgroundColor(colors.lime)
- write(string.rep("|", bat.width))
- end;
- }
- ball = {
- xpos = math.floor(w/2);
- ypos = h - 2;
- xvel = 1;
- yvel = 1;
- update = function(self)
- if self.ypos == h - 2 then
- if self.xpos >= bat.xpos and self.xpos < bat.xpos + bat.width then
- self.yvel = self.yvel * -1
- end
- end
- if self.xpos <= 2 then
- self.xvel = self.xvel * -1
- end
- if self.xpos >= w then
- self.xvel = self.xvel * -1
- end
- if self.ypos <= 1 then
- self.yvel = self.yvel * -1
- end
- if self.ypos >= h then
- gameOver = true
- end
- self.xpos = self.xpos + self.xvel
- self.ypos = self.ypos + self.yvel
- updateGrid()
- end;
- draw = function(self)
- term.setCursorPos(ball.xpos, ball.ypos)
- write(" ")
- end;
- }
- function prepare()
- bCount = 0
- bGrid = {}
- for i = 3, (w - 2), 2 do
- bGrid[i] = {}
- for j = 4, lines do
- col = 2 ^ math.random(1, 14)
- bGrid[i][j] = {
- xpos = i;
- ypos = j;
- colour = col;
- broken = false;
- }
- bCount = bCount + 1
- -- print(bGrid[i][j].xpos.." : "..bGrid[i][j].ypos.." : "..bGrid[i][j].color)
- -- sleep(0.5)
- end
- end
- end
- function updateGrid()
- for i = 3, (w - 2), 2 do
- for j = 4, lines do
- hit = false
- if not bGrid[i][j].broken then
- if ball.xpos == bGrid[i][j].xpos - 1 or ball.xpos == bGrid[i][j].xpos + 2 then
- if ball.ypos == bGrid[i][j].ypos then
- ball.xvel = ball.xvel * -1
- hit = true
- end
- end
- if ball.ypos == bGrid[i][j].ypos - 1 or ball.ypos == bGrid[i][j].ypos + 1 then
- if ball.xpos == bGrid[i][j].xpos or ball.xpos == bGrid[i][j].xpos + 1 then
- ball.yvel = ball.yvel * -1
- hit = true
- end
- end
- if hit then
- bGrid[i][j].broken = true
- bCount = bCount - 1
- score = score + 10
- --term.setCursorPos(1, y)
- --write("HIT!")
- --ball:update()
- end
- end
- end
- end
- end
- function drawGame(debug)
- term.setBackgroundColor(colors.black)
- term.setTextColor(textColor)
- term.clear()
- -- print(bGrid[4][7].xpos)
- term.setBackgroundColor(gameColor)
- bat.draw()
- for i = 3, (w - 2), 2 do
- for j = 4, lines do
- if not bGrid[i][j].broken then
- term.setCursorPos(bGrid[i][j].xpos, bGrid[i][j].ypos)
- term.setBackgroundColor(bGrid[i][j].colour)
- write(" ")
- end
- end
- end
- term.setBackgroundColor(gameColor)
- ball.draw()
- draw.centerText(1, w, h, colors.black, colors.lime, "Lives: "..lives.." Score: "..score..) --" HighScore: "..highScore)
- end
- function gameUpdate()
- event, key = os.pullEvent()
- if event == "key" then
- bat:update(key)
- elseif event == "timer" then
- ball:update()
- gTimer = os.startTimer(refreshRate)
- end
- --checkScores()
- end
- function reset()
- bat.xpos = math.floor(w/2)
- bat.ypos = h - 1
- ball.xpos = math.floor(w/2)
- ball.ypos = h - 2
- ball.xvel = 1
- ball.yvel = 1
- drawGame()
- event, key = os.pullEvent("key")
- if key == keys.d then
- ball.xvel = 1
- else
- ball.xvel = -1
- end
- end
- function loadScores()
- if fs.exists("OS/Games/Scores/blockOut") then
- data = fs.open("OS/Games/Scores/blockOut", "r")
- sTab = textutils.unserialize(data.readLine())
- data.close()
- else
- sTab = {}
- for i = 1, 10 do
- sTab[i] = {}
- sTab[i][1] = "None"
- sTab[i][2] = "0"
- end
- end
- highScore = sTab[1][2]
- lowScore = sTab[10][2]
- end
- function checkScores()
- --loadScores()
- if score > lowScore then
- for i = 1, 10 do
- if score > tonumber(sTab[i][2]) then
- oldName = sTab[i][1]
- oldScore = sTab[i][2]
- sTab[i][1] = pName
- sTab[i][2] = score
- for j = i + 1, #sTab do
- if oldScore == 0 then
- else
- nameChain = sTab[j][1]
- scoreChain = sTab[j][2]
- sTab[j][1] = oldName
- sTab[j][2] = oldScore
- oldName = nameChain
- oldScore = scoreChain
- oldName = sTab[i][1]
- oldScore = sTab[i][2]
- end
- end
- lowScore = sTab[10][2]
- return
- end
- end
- end
- highScore = sTab[1][2]
- hs = textutils.serialize(sTab)
- dataWrite = fs.open("OS/Games/Scores/blockOut", "w")
- dataWrite.write(hs)
- dataWrite.close()
- end
- function scoreDump()
- --loadScores()
- term.setBackgroundColor(colors.lime)
- term.clear()
- for i = 1, 10 do
- draw.centerText(1, w, i + 4, colors.black, colors.lime, i..". "..sTab[i][1]..". "..sTab[i][2])
- end
- draw.centerText(1, w, 17, colors.black, colors.lime, "Would you like to play again?")
- event, key = os.pullEvent()
- if event == "key" then
- if key == keys.n then
- exit = true
- scoreDisp = false
- elseif keys == keys.y then
- score = 0
- lives = 3
- scoreDisp = false
- end
- end
- end
- score = 0
- exit = false
- draw.inputBox("Enter name:")
- input = read()
- pName = tostring(input)
- repeat
- --loadScores()
- --checkScores()
- prepare()
- reset()
- while lives > 0 and bCount > 0 do
- gTimer = os.startTimer(refreshRate)
- while not gameOver and bCount > 0 do
- gameUpdate()
- drawGame()
- end
- if bCount == 0 then
- prepare()
- lives = lives + 1
- if bat.width > 3 then
- bat.width = bat.width - 1
- else
- refreshRate = refreshRate - 0.1
- end
- else
- lives = lives - 1
- end
- gameOver = false
- reset()
- end
- scoreDisp = true
- while scoreDisp do
- scoreDump()
- end
- until exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement