ÎÌÌÌ̈ˆˆ¬ªªª¬ˆˆˆ¬ÌÌ̬ÌÀ̬ÌÀ̬ÌÀÌÌÌìÌÀÀî̬ÀÌÀ¬ÀÀÀ¬ÀÀÀ¬ÀÀÎÌÌÌ̈ˆˆ¬ªªª¬ˆˆˆ¬ÌÌ̬ÌÌ̬ÌÀ̬ÌÀÌÌÌìÌÀÀî̬ÀÌ̬ÀÀÀ¬ÀÀÀ¬ÀÀ¬ÌÌ̬ªªª¬ª¬ª¬ªÊ̬ªªªŒˆˆˆÌÀÌÎÌÌÎ̬̪ªÀ쬪ÀÀÀÌîÀìîÌÌîî¬ÌÌ̬ªªª¬ª¬ª¬ªÊ̬ªªªŒˆˆˆÌÀÌÎÌÌÎ̬̪ªÀ쬪ÀÀÀÌîÀìîÌÌîî >Š1pwpwpwwwwwwpppwwpppwpwpwwwwpwpwppppppwpwpppwwwwpppppwpppppwwwpwpwpwpwwpwpppppwpwpwppwpwppwpwwwppwwpwwppppwwpwpwpppwwwwwwpwwwpppppwîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîîî30333333303330333033333303300303303330333333333333033333303003330003033000333333330030300330303300¼-- title: SimonPiano -- author: aki44 -- desc: The classic sound sequence game adapted to a 5 keys piano -- script: lua CHEAT = false -- forward declarations Game = {} Splash = {} Light = {} -- global variables lights = {} scene = nil -- Splash class Splash = { update = function(self) if keyp(28) then scene = Game:new() end -- '1' key end, draw = function(self) cls() print("SIMON PIANO KEYS", 50, 60, 14, false, 2) print("PRESS KEY 1 TO START", 29, 20, 7) print("USE KEYS 1, 2, 3, 4, 5", 70, 100, 3) end } -- Game class Game = { cpu_turn = false, lost = false, sequence = {}, next = 1, player_next = 1, t0 = 0 } function Game:new() local o = {} setmetatable(o, self) self.__index = self o:generate(5) o.cpu_turn = true o.lost = false o.sequence = o.sequence or {} o.player_next = 1 o.next = 1 o.t0 = 0 return o end function Game:rnd_note() return math.random(28, 32) end function Game:generate(n) self.sequence = {} for i = 28, n+28 do table.insert(self.sequence, self:rnd_note()) end end function Game:extend(n) for i = 1, n do table.insert(self.sequence, self:rnd_note()) end end function Game:play() if self.t0 == 0 then self.t0 = time() self.next = 1 elseif time() - self.t0 > 500 then if self.next <= #self.sequence then for _, v in pairs(lights) do if v.id == self.sequence[self.next] then v:on() v:play() else v:off() end end end if self.next > #self.sequence then self.t0 = 0 self.cpu_turn = false else self.t0 = time() self.next = self.next + 1 end end end function Game:guess(n) if self.sequence[self.player_next] == n then for _, v in pairs(lights) do if v.id == n then v:on() v:play() end end if self.player_next == #self.sequence then self:extend(1) self.player_next = 1 self.cpu_turn = true else self.player_next = self.player_next + 1 end else self.t0 = 0 self.lost = true end end function Game:lose() if self.t0 == 0 then self.t0 = time() self.next = 1 elseif time() - self.t0 > 500 and self.next == 1 then sfx(0, "E-2", 60) self.t0 = time() self.next = 2 elseif time() - self.t0 > 1000 and self.next == 2 then scene = Splash end end function Game:update() for _, v in pairs(lights) do v:update() end if self.cpu_turn then self:play() elseif self.lost then self:lose() else for _, v in pairs(lights) do if keyp(v.id) then v:on() v:play() self:guess(v.id) end end end if CHEAT and keyp(7) then self.cpu_turn = true end end function Game:draw() cls() for _, v in pairs(lights) do v:draw() end print("LENGTH: " .. #self.sequence) local s="" for i = 1, #self.sequence do local aa="" if self.sequence[i]==28 then s = s .. "1" .. " " elseif self.sequence[i]==29 then s = s .. "2" .. " " elseif self.sequence[i]==30 then s = s .. "3" .. " " elseif self.sequence[i]==31 then s = s .. "4" .. " " elseif self.sequence[i]==32 then s = s .. "5" .. " " end end --shows the whole sequence --print (s,10,20) if self.cpu_turn then print("CPU TURN", 0, 8) end end -- Light class Light = {} Light.__index = Light function Light:new(x, y, off_color, on_color, note, keycode, label, id) local o = { x = x, y = y, off_color = off_color, on_color = on_color, note = note, keycode = keycode, label = label, id = id, lit = 0 } setmetatable(o, self) return o end function Light:update() if self.lit > 0 then self.lit = self.lit - 1 end end function Light:draw() rect(self.x-10, self.y, 30, 50, self.lit > 0 and self.on_color or self.off_color) rectb(self.x-10, self.y, 30,50, 1) print(self.label, self.x , self.y+20 , 1) if self.lit>0 then print(self.label, 240/2 - 5*12/2, 20, 14, false, 4) end end function Light:on() self.lit = 30 end function Light:off() self.lit = 0 end function Light:play() sfx(0, self.note, 25) end -- init function init() local t1 = 20 lights = { Light:new(240/6*1, 136/3, 4, 0, "C-3", 2, "1/C", 28), -- '1' key Light:new(240/6*2, 136/3, 5, 0, "D-3", 3, "2/D", 29), -- '2' key Light:new(240/6*3, 136/3, 2, 0, "E-3", 4, "3/E", 30), -- '3' key Light:new(240/6*4, 136/3, 9, 0, "F-4", 5, "4/F", 31), -- '4' key Light:new(240/6*5, 136/3, 11, 0, "G-4", 6, "5/G", 32) -- '5' key } scene = Splash end -- TIC hook function TIC() scene:update() scene:draw() end init() cls()