Advertisement
GravityCube

SpaceShip

Dec 26th, 2018
771
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 19.25 KB | None | 0 0
  1. --[[
  2.    
  3.             --*--*--*--*--*--*--*--*--*--*--*--*--*--*--   
  4.             --*             SpaceShip Game           *--
  5.             --*     https://pastebin.com/rW9x5QvX    *--
  6.             --*    by: GravityCube and Archmaestro   *--
  7.             --*--*--*--*--*--*--*--*--*--*--*--*--*--*--
  8.  
  9.            
  10.     Changelog:
  11.         1.0.0 First release
  12.        
  13. --]]
  14. levels = {}
  15.  
  16. levels[1] = {
  17.     ["enemies"] = {
  18.         [1]={
  19.             ["entity_type"] = "enemy_1",
  20.             ["spawn_rate"] = 2.5,
  21.             ["x_velocity"] = 1.5,
  22.             ["lives"] = 1,
  23.             ["points"] = 50,
  24.         }
  25.     }
  26. }
  27. levels[2] = {
  28.     ["enemies"] = {  
  29.         [1]={
  30.             ["entity_type"] = "enemy_1",
  31.             ["spawn_rate"] = 1.5,
  32.             ["x_velocity"] = 1.5,
  33.             ["lives"] = 1,
  34.             ["points"] = 50,
  35.         },
  36.         [2]={
  37.             ["entity_type"] = "enemy_2",
  38.             ["entity_type"] = "enemy_2",
  39.             ["spawn_rate"] = 1,
  40.             ["x_velocity"] = 1.7,
  41.             ["lives"] = 2,
  42.             ["points"] = 150,
  43.             ["bullets"] = {
  44.                 [1]={
  45.                     ["position"]={["x"]=-3,["y"]=0},
  46.                     ["speed"]=-2.5,
  47.                     ["rate"]=1,
  48.                 }
  49.             }
  50.         }
  51.     }
  52. }
  53. levels[3] = {
  54.     ["enemies"] = {
  55.         [1]={
  56.             ["entity_type"] = "enemy_2",
  57.             ["spawn_rate"] = 1,
  58.             ["x_velocity"] = 1.6,
  59.             ["lives"] = 2,
  60.             ["points"] = 150,
  61.             ["bullets"] = {
  62.                 [1]={
  63.                     ["position"]={["x"]=-3,["y"]=0},
  64.                     ["speed"]=-2.5,
  65.                     ["rate"]=1,
  66.                 }
  67.             }
  68.         },
  69.         [2]={
  70.             ["entity_type"] = "enemy_3",
  71.             ["spawn_rate"] = 1,
  72.             ["x_velocity"] = 1.8,
  73.             ["lives"] = 1,
  74.             ["points"] = 200,
  75.             ["bullets"] = {
  76.                 [1]={
  77.                     ["position"]={["x"]=-3,["y"]=2},
  78.                     ["speed"]=-2.5,
  79.                     ["rate"]=1,
  80.                 },
  81.                 [2]={
  82.                     ["position"]={["x"]=-3,["y"]=-2},
  83.                     ["speed"]=-2.5,
  84.                     ["rate"]=1,
  85.                 }
  86.             }
  87.         }
  88.     }
  89. }
  90. levels[4] = {
  91.     ["enemies"] = {
  92.         [1]={
  93.             ["entity_type"] = "enemy_1",
  94.             ["spawn_rate"] = 2,
  95.             ["x_velocity"] = 1.5,
  96.             ["lives"] = 1,
  97.             ["points"] = 50,
  98.             ["bullets"] = {
  99.                 [1]={
  100.                     ["position"]={["x"]=-3,["y"]=0},
  101.                     ["speed"]=-2.5,
  102.                     ["rate"]=1,
  103.                 }
  104.             }
  105.         },
  106.         [2]={
  107.             ["entity_type"] = "enemy_3",
  108.             ["spawn_rate"] = 1,
  109.             ["x_velocity"] = 1.9,
  110.             ["lives"] = 1,
  111.             ["points"] = 200,
  112.             ["bullets"] = {
  113.                 [1]={
  114.                     ["position"]={["x"]=-3,["y"]=2},
  115.                     ["speed"]=-2.5,
  116.                     ["rate"]=1,
  117.                 },
  118.                 [2]={
  119.                     ["position"]={["x"]=-3,["y"]=-2},
  120.                     ["speed"]=-2.5,
  121.                     ["rate"]=1,
  122.                 }
  123.             }
  124.         }
  125.     }
  126. }
  127. levels[0] = {
  128.     ["enemies"] = {
  129.         [1]={
  130.             ["entity_type"] = "boss_1",
  131.             ["spawn_rate"] = 0,
  132.             ["x_velocity"] = 20,
  133.             ["lives"] = 100,
  134.             ["points"] = 50000,
  135.             ["position"] = {["x"]=0.5, ["y"] = 0.5}
  136.            
  137.         }
  138.     }
  139. }
  140. --------------------------------------------------------
  141. -->                    Variables                     <--
  142. --------------------------------------------------------
  143. os.loadAPI("gcapi")
  144. os.loadAPI("mLink")
  145. os.loadAPI("ocs/apis/sensor")
  146. os.loadAPI("gameModels")
  147. os.loadAPI("bigMessages")
  148. os.loadAPI("CLKeysAPI")
  149.  
  150. local score = 0
  151. local coins = 0
  152. local level = 1
  153. local entities = {}
  154.  
  155. local args = {...}
  156. local player = (args[1] and args[1] ~= '' and args[1]) or 'pim'
  157. local pixelsUsed = {} -- {[x]={[y]={entityIDs}}}
  158. local clock = 0
  159.  
  160. local pim = peripheral.find("pim")
  161.  
  162. local mon = peripheral.wrap("top")
  163. mon.setTextScale(0.5)
  164. local max_x, max_y = mon.getSize()
  165.  
  166. local mon2 = nil
  167.  
  168. for _,pName in pairs(peripheral.getNames()) do
  169.     if peripheral.getType(pName) == "monitor" then
  170.         if pName ~= "top" then
  171.             mon2 = peripheral.wrap(pName)
  172.         end
  173.     end
  174. end
  175.  
  176. local s = nil
  177. for _,pName in pairs(peripheral.getNames()) do
  178.     if peripheral.getType(pName) == "sensor" then
  179.         s = sensor.wrap(pName)
  180.     end
  181. end
  182. --------------------------------------------------------
  183. -->                  Player Cursor                   <--
  184. --------------------------------------------------------
  185. local isBlocking = false
  186. function getCursorPlayer(player)
  187.     local targetDetails = s.getTargetDetails(player)
  188.     if targetDetails then
  189.    
  190.         local pos = targetDetails["Position"]
  191.         local yaw = targetDetails["Yaw"]
  192.         local pitch = targetDetails["Pitch"]
  193.         local x,y = mLink.getCursorPos(pos.X, pos.Y, pos.Z, yaw, pitch)
  194.        
  195.         local ib = targetDetails["IsBlocking"]
  196.         if ib ~= isBlocking then
  197.             os.queueEvent("blocking_event", player, x, y, ib)
  198.         end
  199.         isBlocking = ib
  200.        
  201.         return x, y
  202.     end
  203.     return -1, -1
  204. end
  205.  
  206. function insideBoundaries(x,y)
  207.     if x > max_x or y > max_y or x < 1 or y < 1 then
  208.         return false
  209.     end
  210.     return true
  211. end
  212. function checkPlayer(player)
  213.     if getCurrentPlayer() == player then
  214.         return true
  215.     end
  216.     return false
  217. end
  218.  
  219. function getCurrentPlayer()
  220.     return pim.getInventoryName()
  221. end
  222. --------------------------------------------------------
  223. -->                       Entities                      <--
  224. --------------------------------------------------------
  225. Entity = {}
  226. Entity.__index = Entity
  227. function Entity.create(entity_type)
  228.     local entity = {}
  229.     setmetatable(entity,Entity)
  230.     entity.team = 2
  231.     entity.velocity = {["x"]=0, ["y"]=0}
  232.     entity.position = {["x"]=1, ["y"]=math.floor(max_y/2)}
  233.     entity.state = "hidden"
  234.     entity.type = entity_type
  235.     entity.lives = 1
  236.     entity.god_time = 6
  237.     entity.deathEvents = {}
  238.     entity.crashEvents = {}
  239.     entity.pixels = gameModels.getModel(entity_type)
  240.    
  241.     return entity
  242. end
  243. function Entity:getPos()
  244.     local pos = self.position
  245.     return pos.x, pos.y
  246. end
  247. function Entity:setPos(x,y)
  248.     self.position.x = x
  249.     self.position.y = y
  250. end
  251. function Entity:deathEvent()
  252.     self.state = "dead"
  253.     if self.points then
  254.         score = score + self.points
  255.     end
  256.     for _,event in pairs(self.deathEvents) do
  257.         event(self)
  258.     end
  259. end
  260. function Entity:update()
  261.    
  262.     local sState = string.split(self.state .. "_", "_")
  263.     if sState[1] == "god" then
  264.         local t = tonumber(sState[2]) - 1
  265.         self:setState("god_" .. t)
  266.         if t == 0 then
  267.             self:setState("active")
  268.         end
  269.     end
  270.    
  271.     if self.bullets or self.bullet then
  272.         self:shoot()
  273.     end
  274.    
  275.     if self.state == "active" or sState[1] == "god" then
  276.         self:updateMovement()
  277.     end
  278. end
  279. function Entity:crashEvent()
  280.     self.lives = self.lives - 1
  281.     self.state = "god_" .. tostring(self.god_time)
  282.    
  283.     for _,event in pairs(self.crashEvents) do
  284.         event(self)
  285.     end
  286.     if self.lives == 0 then
  287.         self:deathEvent()
  288.     end
  289. end
  290. function Entity:updateMovement()
  291.     local xo, yo = self:getPos()
  292.     local vx, vy = self:getVelocity()
  293.     local nx = xo + vx
  294.     local ny = yo + vy
  295.    
  296.     --Collision with wall (x)
  297.     if ny > max_y or ny < 1 then
  298.         self:setVelocity(vx,-vy)
  299.         ny = yo - vy
  300.     end
  301.    
  302.     if not insideBoundaries(nx,ny) then
  303.         self:deathEvent()
  304.     end
  305.     self:setPos(xo+vx, yo+vy)
  306. end
  307. function Entity:getVelocity()
  308.     local vel = self.velocity
  309.     return vel.x, vel.y
  310. end
  311. function Entity:setVelocity(x,y)
  312.     self.velocity.x = x
  313.     self.velocity.y = y
  314. end
  315. function Entity:setState(state)
  316.     self.state = state
  317. end
  318.  
  319. local noPixels = {}
  320. function Entity:getPixels()
  321.     local sState = string.split(self.state .. "_", "_")
  322.     if sState[1] == "god" then
  323.         if tonumber(sState[2]) % 2 == 0 then
  324.             return gameModels.models.blackDot
  325.         end
  326.     end
  327.    
  328.     local pixels = self.pixels
  329.     if pixels then
  330.         return pixels
  331.     end
  332.    
  333.     return noPixels
  334. end
  335.  
  336. function updateEntities()
  337.     dead_entities = {}
  338.     for i,entity in pairs(entities) do
  339.         entity:update()
  340.         if entity.state == "dead" and entity.type ~= "player"  then
  341.             table.insert(dead_entities, i)
  342.         end
  343.     end
  344.     for _,i in pairs(dead_entities) do
  345.         entities[i] = nil
  346.     end
  347.    
  348. end
  349. function checkCollisions()
  350.     for x,y_list in pairs(pixelsUsed) do
  351.         for y, id_list in pairs(y_list) do
  352.             if #id_list > 1 and not sameTeam(id_list) then
  353.                 for _,i in pairs(id_list) do
  354.                     local entity = entities[i]
  355.                     if entity then
  356.                         entity.state = "crashed"
  357.                     end
  358.                 end
  359.             end
  360.         end
  361.     end
  362.     for _,entity in pairs(entities) do
  363.         if entity.state == "crashed" then
  364.             entity:crashEvent()
  365.         end
  366.     end
  367. end
  368. function sameTeam(id_list)
  369.     local teams = {}
  370.     for _,i in pairs(id_list) do
  371.         local entity = entities[i]
  372.         if entity then
  373.             local team = entity.team
  374.             if not table.contains(teams, team) then
  375.                 table.insert(teams,team)
  376.             end
  377.         end
  378.     end
  379.     if #teams > 1 then
  380.         return false
  381.     end
  382.     return true
  383. end
  384. --------------------------------------------------------
  385. -->                Player SpaceShip                  <--
  386. --------------------------------------------------------
  387. SpaceShip = setmetatable({}, {__index = Entity})
  388. SpaceShip.__index = SpaceShip
  389.  
  390. function SpaceShip.create()
  391.     local self = setmetatable(Entity.create("player"), SpaceShip)
  392.     self.state = "active"
  393.     self.lives = 5
  394.     self.god_time = 10
  395.     self.team = 1
  396.     self.max_velocity = 2.4
  397.    
  398.     self.bullet = {}
  399.     self.bullet.type = "bullet"
  400.     self.bullet.speed = 2.5
  401.     self.bullet.rate =  5 -- x per sec
  402.     return self
  403. end
  404. function SpaceShip:updateMovement()
  405.     local x, y = getCursorPlayer(player)
  406.     if not insideBoundaries(x,y) then return end
  407.     --[[
  408.     local max_velocity = self.max_velocity
  409.     local xo, yo = self:getPos()
  410.     local max_velocity2 = max_velocity^2
  411.     local dx2 = (x-xo)^2
  412.     local dy2 = (y-yo)^2
  413.     if dx2 + dy2 > max_velocity2 then
  414.         local p = (max_velocity2/(dx2 + dy2) )^0.5
  415.         x = ((x-xo) * p) + xo
  416.         y = ((y-yo) * p) + yo
  417.     end--]]
  418.     self:setPos(x,y)
  419. end
  420.  
  421. function SpaceShip:shoot()
  422.     if not (math.floor(clock % (20/self.bullet.rate)) == 0) then return end
  423.     local x,y = self:getPos()
  424.     local vx = self.bullet.speed
  425.     local bullet = Bullet.create(x+5,y,vx,1, true)
  426.    
  427.     local crashEvent = function ()
  428.         killSound()
  429.     end
  430.     table.insert(bullet.crashEvents, crashEvent)
  431.     table.insert(entities, bullet)
  432. end
  433.  
  434. function SpaceShip:setBulletSpeed(speed)
  435.     self.bullet.speed = speed
  436. end
  437. function SpaceShip:setBulletType(bullet_type)
  438.     self.bullet.type = bullet_type
  439. end
  440. --------------------------------------------------------
  441. -->                 Bullet Entity                    <--
  442. --------------------------------------------------------
  443. Bullet = setmetatable({}, {__index = Entity})
  444. Bullet.__index = Bullet
  445.  
  446. function Bullet.create(x, y, vx, team, fromPlayer)
  447.     local entity_type = "bullet"
  448.     if fromPlayer then
  449.         entity_type = "player_bullet"
  450.     end
  451.     local self = setmetatable(Entity.create(entity_type), Bullet)
  452.     self.state = "active"
  453.     self.team = team
  454.    
  455.     self.velocity.x = vx
  456.     self.position.x = x
  457.     self.position.y = y
  458.     return self
  459. end
  460. --------------------------------------------------------
  461. -->                    Sound                      <--
  462. --------------------------------------------------------
  463. local nb = peripheral.find("music")
  464. function killSound()
  465.     nb.playSound("random.pop",1,1)
  466. end
  467. function playSound(sound_name)
  468.     nb.playSound(sound_name,1,1)
  469. end
  470. --------------------------------------------------------
  471. -->                    ScoreBar                      <--
  472. --------------------------------------------------------
  473. function updateScoreBar(ss)
  474.     local mon = mon2
  475.     if not mon then return end
  476.     mon2.setTextScale(1.8)
  477.     mon.setBackgroundColor(colors.black)
  478.     mon.clear()
  479.     local max_x, max_y = mon.getSize()
  480.     local lives = ss.lives
  481.  
  482.    
  483.     local levelText = 'Level ' .. tostring(level)
  484.     mon.setCursorPos(gcapi.getCenter(max_x, levelText),1)
  485.     mon.write(levelText)
  486.    
  487.     local scoreText = 'Score ' .. tostring(score)
  488.     mon.setCursorPos(gcapi.getCenter(max_x, scoreText),2)
  489.     mon.write(scoreText)
  490.    
  491.     mon.setCursorPos(1,3)
  492.     mon.write('Lives ')
  493.     for i=1, lives do
  494.         mon.setBackgroundColor(colors.black)
  495.         mon.write(' ')
  496.         mon.setBackgroundColor(colors.red)
  497.         mon.write(' ')
  498.     end
  499. end
  500.  
  501. --------------------------------------------------------
  502. -->                   BigButtons                     <--
  503. --------------------------------------------------------
  504. local buttons = {}
  505. for x=1, max_x do
  506.     buttons[x] = {}
  507. end
  508.  
  509. Button = {}
  510. Button.__index = Button
  511.  
  512. function Button.create(corner1, corner2, func)
  513.     local button = {}
  514.     setmetatable(button,Button)
  515.     button.func = func
  516.     for x=corner1.x, corner2.x do
  517.         for y=corner1.y, corner2.y do
  518.             buttons[x][y] = button
  519.         end
  520.     end
  521.     return button
  522. end
  523. function buttonListener(tEvent)
  524.     if tEvent[1] == "blocking_event" then
  525.         local player = tEvent[2]
  526.         local x = tEvent[3]
  527.         local y = tEvent[4]
  528.         local pushed = tEvent[5]
  529.         if not pushed then
  530.             return
  531.         end
  532.        
  533.         local button = buttons[x][y]
  534.         if button then
  535.             playSound("random.click")
  536.             button.func()
  537.         end
  538.     end
  539. end
  540. --------------------------------------------------------
  541. -->                   ScoreBoard                     <--
  542. --------------------------------------------------------
  543. local scoreboard = {}
  544.  
  545. function loadScoreboard()
  546.     prizes = gcapi.getListFromFile("/disk/scoreboard")
  547. end
  548.  
  549. function saveScoreboard()
  550.     gcapi.saveListToFile("/disk/scoreboard", prizes)
  551. end
  552.  
  553. function addNewScore(nick, score)
  554.     loadScoreboard()
  555.     if not scoreboard[nick] or scoreboard[nick] < score then
  556.         scoreboard[nick] = score
  557.         saveScoreboard()
  558.         return true
  559.     end
  560.     return false
  561. end
  562. function scoreMenu()
  563.     local time = os.clock() + 10
  564.     while true do
  565.         local seconds = math.floor(time - os.clock())
  566.         if seconds < 0 then break end
  567.         cleanScreen()
  568.         bigMessages.displayMessage(mon, "Save your score using", colors.red, nil, gcapi.getCenter(max_y, 5)-4)
  569.         bigMessages.displayMessage(mon, "!save <nickname>", colors.red, nil, gcapi.getCenter(max_y, 5)+2)
  570.         bigMessages.displayMessage(mon, "You have " .. seconds .. " seconds.", colors.red, nil, gcapi.getCenter(max_y, 5)+8)
  571.         tEvent = {os.pullEvent()}
  572.         if tEvent[1] == "chatEvent" then
  573.             local player = tEvent[2]
  574.             if checkPlayer(player) then
  575.                 local co = tEvent[3]
  576.                 local command = gcapi.split(co .. " ", " ")
  577.                 if command[1] == "save" then  
  578.                     local nick = command[2]
  579.                     if command[2] and command[2] ~= '' then
  580.                         addNewScore(nick, score)
  581.                         break
  582.                     else
  583.                         displayError("You need to specify a nickname")
  584.                         sleep(2)
  585.                     end
  586.                 end
  587.                
  588.                 if command[1] == "skip" then break end
  589.             end
  590.         end
  591.     end
  592. end
  593. --------------------------------------------------------
  594. -->                     Monitor                      <--
  595. --------------------------------------------------------
  596. mon.setTextColor(colors.black)
  597. function cleanScreen()
  598.     mon.setBackgroundColor(colors.white)
  599.     mon.clear()
  600. end
  601. function printMenu()
  602.     cleanScreen()
  603.    
  604.     bigMessages.displayMessage(mon, "Space Invaders", colors.magenta, nil, gcapi.getCenter(max_y, 5)-2)
  605.     if player ~= "pim" then
  606.         bigMessages.displayMessage(mon, "Start with !play", colors.cyan, nil, gcapi.getCenter(max_y, 5)+4)
  607.     else
  608.         bigMessages.displayMessage(mon, "Stand in the PIM", colors.cyan, nil, gcapi.getCenter(max_y, 5)+4)
  609.     end
  610.    
  611.     mon.setBackgroundColor(colors.white)
  612.     local infoMessage = "NOTE: By using this machine and writing !play you acknowledge that your keys will be deducted from your inventory"
  613.     mon.setTextColor(colors.black)
  614.     mon.setCursorPos(gcapi.getCenter(max_x, infoMessage), max_y-1)
  615.     mon.write(infoMessage)
  616.    
  617. end
  618. function displayError(err)
  619.     cleanScreen()
  620.     bigMessages.displayMessage(mon, err, colors.red)
  621. end
  622. function updateGameScreen()
  623.     cleanScreen()
  624.     pixelsUsed = {}
  625.     for i, entity in pairs(entities) do
  626.        
  627.         local xo, yo = entity:getPos()
  628.         for a,pixel in pairs(entity:getPixels()) do
  629.             local x = math.floor(pixel.x + xo)
  630.             local y = math.floor(pixel.y + yo)
  631.             mon.setBackgroundColor(pixel.color)
  632.             mon.setCursorPos(x, y)
  633.             mon.write(" ")
  634.             if (entity.state == "active") then
  635.                 addToPixel(x, y, i)
  636.             end
  637.         end
  638.     end
  639. end
  640. function addToPixel(x, y, i)
  641.     if not pixelsUsed[x]then
  642.         pixelsUsed[x] = {}
  643.     end
  644.     id_list = pixelsUsed[x][y]
  645.     if not id_list then
  646.         pixelsUsed[x][y] = {i}
  647.         return
  648.     end
  649.     table.insert(id_list, i)
  650. end
  651. --------------------------------------------------------
  652. -->                    Game functions                     <--
  653. --------------------------------------------------------
  654. function spawnEnemies(level)
  655.     for _,enemy_settings in pairs(levels[level].enemies) do
  656.         if (math.floor(clock % (20/enemy_settings.spawn_rate)) == 0) then
  657.             local vx = -enemy_settings.x_velocity
  658.             local vy = (gcapi.getRandom(4, 9)/10)
  659.             if gcapi.getRandom(1,2) == 1 then
  660.                 vy = -vy
  661.             end
  662.             local enemy = Entity.create(enemy_settings.entity_type)
  663.            
  664.             local x = max_x
  665.             local y = gcapi.getRandom(1,max_y)
  666.             local position = enemy_settings.position
  667.             if position then
  668.                 if position.x then
  669.                     x = max_x*position.x
  670.                 end
  671.                 if position.y then
  672.                     y = max_y*position.y
  673.                 end
  674.             end
  675.            
  676.            
  677.             enemy:setPos(x, y)
  678.             enemy:setVelocity(vx, vy)
  679.             enemy.lives = enemy_settings.lives
  680.             enemy:setState("active")
  681.             enemy.points = enemy_settings.points
  682.            
  683.             local bullets = enemy_settings.bullets
  684.             if bullets then
  685.                 enemy.bullets = bullets
  686.                 enemy.shoot = function (self)
  687.                     for _,bullet in pairs(self.bullets) do
  688.                         if (math.floor(clock % (20/bullet.rate)) == 0) then
  689.                             local x,y = self:getPos()
  690.                             local pos = bullet.position
  691.                             local vx = bullet.speed
  692.                             local bullet = Bullet.create(x+pos.x,y+pos.y,vx,2)
  693.                            
  694.                             table.insert(entities, bullet)
  695.                         end
  696.                     end
  697.                 end
  698.             end
  699.            
  700.             table.insert(entities, enemy)
  701.         end
  702.     end
  703. end
  704. function startGame()
  705.     parallel.waitForAny(startGameThread, gameListener)
  706. end
  707. function startGameThread()
  708.     mon.setBackgroundColor(colors.white)
  709.     mon.clear()
  710.    
  711.     entities = {}
  712.     score = 0
  713.     local spaceShip = SpaceShip:create("player")
  714.     spaceShip:setState("active")
  715.     table.insert(entities, spaceShip)
  716.    
  717.     level = 1
  718.     local levelChangeScore = 0
  719.     while spaceShip.state ~= "dead" do
  720.         if player ~= "pim" then
  721.             if score - levelChangeScore > 2000 and levels[level+1] then
  722.                 level = level + 1
  723.                 levelChangeScore = score
  724.                 playSound("random.levelup")
  725.             end
  726.             spawnEnemies(level)
  727.             updateEntities()
  728.             updateGameScreen()
  729.             checkCollisions()
  730.             updateScoreBar(spaceShip)
  731.             sleep(0.05)
  732.             clock = clock + 1
  733.         else
  734.             bigMessages.displayMessage(mon, "Pause", colors.magenta)
  735.             sleep(1)
  736.         end
  737.     end
  738.     bigMessages.displayMessage(mon, "You are dead!", colors.red)
  739.     sleep(2.5)
  740. end
  741.  
  742. function gameListener()
  743.     while true do
  744.         tEvent = {os.pullEvent()}
  745.         if tEvent[1] == "player_on" then
  746.             player = pim.getInventoryName()
  747.         elseif tEvent[1] == "player_off" then
  748.             player = pim.getInventoryName()
  749.         elseif tEvent[1] == "blocking_event" then
  750.             local player = tEvent[2]
  751.             local x = tEvent[3]
  752.             local y = tEvent[4]
  753.             local state = tEvent[5]
  754.            
  755.            
  756.         end
  757.     end
  758. end
  759.  
  760. --------------------------------------------------------
  761. -->                    Menu functions                     <--
  762. --------------------------------------------------------
  763. function insertCoin()
  764.     local qty, err = CLKeysAPI.moveKeysToSystem(1)
  765.     if qty == 1 then
  766.         return true
  767.     else
  768.         return false, err
  769.     end
  770. end
  771.    
  772. function menuListener()
  773.     sleep(1)
  774.     printMenu()
  775.     player = pim.getInventoryName()
  776.     while true do
  777.         tEvent = {os.pullEvent()}
  778.         if tEvent[1] == "player_on" then
  779.             player = pim.getInventoryName()
  780.             if not CLKeysAPI then
  781.                 print(player .. " started a game")
  782.                 startGame()
  783.             end
  784.             printMenu()
  785.         elseif tEvent[1] == "player_off" then
  786.             player = pim.getInventoryName()
  787.             printMenu()
  788.         elseif tEvent[1] == "chatEvent" then
  789.             local player = tEvent[2]
  790.             co = tEvent[3]
  791.             if checkPlayer(player) then
  792.                 if co == "play" then
  793.                     if insertCoin() then
  794.                         print(player .. " started a game")
  795.                         startGame()
  796.                         scoreMenu()
  797.                         printMenu()
  798.                     else
  799.                         displayError("You dont have enough keys!")
  800.                         print(player .. " doesnt have enough keys")
  801.                         sleep(4)
  802.                         printMenu()
  803.                     end
  804.                 end
  805.             end
  806.         end
  807.     end
  808. end
  809.  
  810. parallel.waitForAny(menuListener, gcapi.startChatEventQueue)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement