Advertisement
pedrosgali

PedrOS blockOut

Nov 21st, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.74 KB | None | 0 0
  1. os.loadAPI("OS/Api/draw")
  2. w, h = term.getSize()
  3. gameOver = false
  4. refreshRate = 0.2
  5. gTimer = os.startTimer(refreshRate)
  6. gameColor = colors.lime
  7. textColor = colors.black
  8. lines = 8
  9. lives = 3
  10.  
  11. bat = {
  12.     xpos = math.floor(w/2);
  13.     ypos = h - 1;
  14.     width = 6;
  15.     update = function(self, key)
  16.                  if key == keys.a then
  17.                      if self.xpos > 1 then
  18.                          self.xpos = self.xpos - 1
  19.                      end
  20.                  elseif key == keys.d then
  21.                      if self.xpos + self.width < w + 1 then
  22.                          self.xpos = self.xpos + 1
  23.                      end
  24.                  elseif key == keys.q then
  25.                      gameOver = true
  26.                  end
  27.              end;
  28.     draw = function(self)
  29.                term.setCursorPos(bat.xpos, h - 1)
  30.                term.setBackgroundColor(colors.lime)
  31.                write(string.rep("|", bat.width))
  32.            end;
  33. }
  34.  
  35. ball = {
  36.     xpos = math.floor(w/2);
  37.     ypos = h - 2;
  38.     xvel = 1;
  39.     yvel = 1;
  40.     update = function(self)
  41.                  if self.ypos == h - 2 then
  42.                      if self.xpos >= bat.xpos and self.xpos < bat.xpos + bat.width then
  43.                          self.yvel = self.yvel * -1
  44.                      end
  45.                  end
  46.                  if self.xpos <= 2 then
  47.                      self.xvel = self.xvel * -1
  48.                  end
  49.                  if self.xpos >= w then
  50.                      self.xvel = self.xvel * -1
  51.                  end
  52.                  if self.ypos <= 1 then
  53.                      self.yvel = self.yvel * -1
  54.                  end
  55.                  if self.ypos >= h then
  56.                      gameOver = true
  57.                  end
  58.                  self.xpos = self.xpos + self.xvel
  59.                  self.ypos = self.ypos + self.yvel
  60.                  updateGrid()
  61.              end;
  62.     draw = function(self)
  63.                term.setCursorPos(ball.xpos, ball.ypos)
  64.                write(" ")
  65.            end;
  66. }
  67.  
  68. function prepare()
  69.     bCount = 0
  70.     bGrid = {}
  71.     for i = 3, (w - 2), 2 do
  72.         bGrid[i] = {}
  73.         for j = 4, lines do
  74.             col = 2 ^ math.random(1, 14)
  75.             bGrid[i][j] = {
  76.                 xpos = i;
  77.                 ypos = j;
  78.                 colour = col;
  79.                 broken = false;
  80.                 }
  81.             bCount = bCount + 1
  82. --          print(bGrid[i][j].xpos.." : "..bGrid[i][j].ypos.." : "..bGrid[i][j].color)
  83. --          sleep(0.5)
  84.         end
  85.     end
  86. end
  87.  
  88. function updateGrid()
  89.     for i = 3, (w - 2), 2 do
  90.         for j = 4, lines do
  91.             hit = false
  92.             if not bGrid[i][j].broken then
  93.                 if ball.xpos == bGrid[i][j].xpos - 1 or ball.xpos == bGrid[i][j].xpos + 2 then
  94.                     if ball.ypos == bGrid[i][j].ypos then
  95.                         ball.xvel = ball.xvel * -1
  96.                         hit = true
  97.                     end
  98.                 end
  99.                 if ball.ypos == bGrid[i][j].ypos - 1 or ball.ypos == bGrid[i][j].ypos + 1 then
  100.                     if ball.xpos == bGrid[i][j].xpos or ball.xpos == bGrid[i][j].xpos + 1 then
  101.                         ball.yvel = ball.yvel * -1
  102.                         hit = true
  103.                     end
  104.                 end
  105.                 if hit then
  106.                     bGrid[i][j].broken = true
  107.                     bCount = bCount - 1
  108.                     score = score + 10
  109.                     --term.setCursorPos(1, y)
  110.                     --write("HIT!")
  111.                     --ball:update()
  112.                 end
  113.             end
  114.         end
  115.     end
  116. end
  117.  
  118. function drawGame(debug)
  119.     term.setBackgroundColor(colors.black)
  120.     term.setTextColor(textColor)
  121.     term.clear()
  122. --    print(bGrid[4][7].xpos)
  123.     term.setBackgroundColor(gameColor)
  124.     bat.draw()
  125.     for i = 3, (w - 2), 2 do
  126.         for j = 4, lines do
  127.             if not bGrid[i][j].broken then
  128.                  term.setCursorPos(bGrid[i][j].xpos, bGrid[i][j].ypos)
  129.                  term.setBackgroundColor(bGrid[i][j].colour)
  130.                  write("  ")
  131.             end
  132.         end
  133.     end
  134.     term.setBackgroundColor(gameColor)
  135.     ball.draw()
  136.     draw.centerText(1, w, h, colors.black, colors.lime, "Lives: "..lives.." Score: "..score..) --" HighScore: "..highScore)
  137. end
  138.  
  139. function gameUpdate()
  140.     event, key = os.pullEvent()
  141.     if event == "key" then
  142.         bat:update(key)
  143.     elseif event == "timer" then
  144.         ball:update()
  145.         gTimer = os.startTimer(refreshRate)
  146.     end
  147.     --checkScores()
  148. end
  149.  
  150. function reset()
  151.     bat.xpos = math.floor(w/2)
  152.     bat.ypos = h - 1
  153.     ball.xpos = math.floor(w/2)
  154.     ball.ypos = h - 2
  155.     ball.xvel = 1
  156.     ball.yvel = 1
  157.     drawGame()
  158.     event, key = os.pullEvent("key")
  159.     if key == keys.d then
  160.         ball.xvel = 1
  161.     else
  162.         ball.xvel = -1
  163.     end
  164. end
  165.  
  166. function loadScores()
  167.     if fs.exists("OS/Games/Scores/blockOut") then
  168.         data = fs.open("OS/Games/Scores/blockOut", "r")
  169.         sTab = textutils.unserialize(data.readLine())
  170.         data.close()
  171.     else
  172.         sTab = {}
  173.         for i = 1, 10 do
  174.             sTab[i] = {}
  175.             sTab[i][1] = "None"
  176.             sTab[i][2] = "0"
  177.         end
  178.     end
  179.     highScore = sTab[1][2]
  180.     lowScore = sTab[10][2]
  181. end
  182.  
  183. function checkScores()
  184.     --loadScores()
  185.     if score > lowScore then
  186.         for i = 1, 10 do
  187.             if score > tonumber(sTab[i][2]) then
  188.                 oldName = sTab[i][1]
  189.                 oldScore = sTab[i][2]
  190.                 sTab[i][1] = pName
  191.                 sTab[i][2] = score
  192.                 for j = i + 1, #sTab do
  193.                              if oldScore == 0 then
  194.                                 else
  195.                         nameChain = sTab[j][1]
  196.                         scoreChain = sTab[j][2]
  197.                         sTab[j][1] = oldName
  198.                         sTab[j][2] = oldScore
  199.                         oldName = nameChain
  200.                         oldScore = scoreChain
  201.                                        oldName = sTab[i][1]
  202.                         oldScore = sTab[i][2]
  203.                                 end
  204.                 end
  205.                 lowScore = sTab[10][2]
  206.                 return
  207.             end
  208.         end
  209.     end
  210.     highScore = sTab[1][2]
  211.     hs = textutils.serialize(sTab)
  212.     dataWrite = fs.open("OS/Games/Scores/blockOut", "w")
  213.     dataWrite.write(hs)
  214.     dataWrite.close()
  215. end
  216.  
  217. function scoreDump()
  218.     --loadScores()
  219.     term.setBackgroundColor(colors.lime)
  220.     term.clear()
  221.     for i = 1, 10 do
  222.         draw.centerText(1, w, i + 4, colors.black, colors.lime, i..". "..sTab[i][1]..". "..sTab[i][2])
  223.     end
  224.     draw.centerText(1, w, 17, colors.black, colors.lime, "Would you like to play again?")
  225.     event, key = os.pullEvent()
  226.     if event == "key" then
  227.         if key == keys.n then
  228.             exit = true
  229.             scoreDisp = false
  230.         elseif keys == keys.y then
  231.             score = 0
  232.             lives = 3
  233.             scoreDisp = false
  234.         end
  235.     end
  236. end
  237.  
  238. score = 0
  239. exit = false
  240. draw.inputBox("Enter name:")
  241. input = read()
  242. pName = tostring(input)
  243. repeat
  244.     --loadScores()
  245.     --checkScores()
  246.     prepare()
  247.     reset()
  248.     while lives > 0 and bCount > 0 do
  249.         gTimer = os.startTimer(refreshRate)
  250.         while not gameOver and bCount > 0 do
  251.             gameUpdate()
  252.             drawGame()
  253.         end
  254.         if bCount == 0 then
  255.             prepare()
  256.             lives = lives + 1
  257.             if bat.width > 3 then
  258.                 bat.width = bat.width - 1
  259.             else
  260.                 refreshRate = refreshRate - 0.1
  261.             end
  262.         else
  263.             lives = lives - 1
  264.         end
  265.         gameOver = false
  266.         reset()
  267.     end
  268.     scoreDisp = true
  269.     while scoreDisp do
  270.         scoreDump()
  271.     end
  272. until exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement