gr = love.graphics win = love.window lk = love.keyboard require("button") require("t") function love.load() local loading = {} loading = serialize.load("records.lua") font = gr.newFont("NotoSans.ttf",14) mini = gr.newFont("NotoSans.ttf",10) big = gr.newFont("NotoSans.ttf",30) flag = nil --Flag = nil -menu, Flag = 1 -records, Flag = 2 -type game, Flag = 3 -type game standart, Flag = 4 -type game impulse, Flag = 5 -type game Invers, Flag = 6 -type game Unreal kube = {x = 96, y = 196, colx = 8, coly = 8} speed = 1 timer = 0 umber = {} records = {{"Standart", 0},{"Impulse", 0},{"Invers", 0},{"Unreal", 0},{"XY diagonal", 0},{"Perpendiculars", 0}} if loading and #loading > 0 then for i = 1, #loading do records[i][2] = loading[i] end end score = 0 mb = {utton:create(200-font:getWidth(">Play<")/2,125,">Play<"),utton:create(200-font:getWidth(">Records<")/2,160,">Records<"),utton:create(200-font:getWidth(">Exit<")/2,195,">Exit<")} tgb = {utton:create(200-font:getWidth(">Standart<")/2,60,">Standart<"),utton:create(200-font:getWidth(">Impulse<")/2,90,">Impulse<"),utton:create(200-font:getWidth(">Invers<")/2,120,">Invers<"),utton:create(200-font:getWidth(">Unreal<")/2,150,">Unreal<"),utton:create(200-font:getWidth(">XY diagonal<")/2,180,">XY diagonal<"),utton:create(200-font:getWidth(">Perpendiculars<")/2,210,">Perpendiculars<")} back = utton:create(200-font:getWidth(" >Back< ")/2,300," >Back< ") r = utton:create(200-font:getWidth(" >Restart< ")/2,250," >Restart< ") end local function restart() kube = {x = 96, y = 196, colx = 8, coly = 8, speed = 350} speed = 1 timer = 0 umber = {} score = 0 toch = nil end function love.update(dt) math.randomseed(math.random(os.time(), os.time()*100)) if toch then r:update(dt) back:update(dt) end if not flag then for i = 1, #mb do mb[i]:update(dt) end elseif flag == 1 then back:update(dt) elseif flag == 2 then for i = 1, #tgb do tgb[i]:update(dt) end elseif flag == 3 then if toch then if records[1][2] < score then records[1][2] = score end end timer = timer+10*dt if #umber > 0 and not toch then for i = 1, #umber do if umber[i] and umber[i][2] > 400 then table.remove(umber,i) end if umber[i] then umber[i][2] = umber[i][2] + umber[i][5]*dt*speed end end end if timer >= 4/(speed/2) and not toch then timer = 0 speed = speed + 0.01 score = score + 1 table.insert(umber, {math.random(0,99), 0, 4, 4, 50})--x,y,w,h,speed end if lk.isDown("d") and not toch then kube.x = kube.x+ 400*dt end if lk.isDown("a") and not toch then kube.x = kube.x- 400*dt end if kube.x >= 191 then kube.x = 191 elseif kube.x <= 8 then kube.x = 8 end elseif flag == 4 then if toch then if records[2][2] < score then records[2][2] = score end end timer = timer+10*dt if #umber > 0 and not toch then for i = 1, #umber do if umber[i] and umber[i][2] > 400 then table.remove(umber,i) end if umber[i] then umber[i][2] = umber[i][2] + umber[i][5]*dt*speed end end end if timer >= 15/(speed/2) and not toch then timer = 0 for x = 0, 5 do table.insert(umber, {math.random(0,99), 0, 4, 4, 50})--x,y,w,h,speed speed = speed + 0.01 score = score + 1 end end if lk.isDown("d") and not toch then kube.x = kube.x+ 400*dt end if lk.isDown("a") and not toch then kube.x = kube.x- 400*dt end if kube.x >= 191 then kube.x = 191 elseif kube.x <= 8 then kube.x = 8 end elseif flag == 5 then if toch then if records[3][2] < score then records[3][2] = score end end timer = timer+10*dt if #umber > 0 and not toch then for i = 1, #umber do if umber[i] and (umber[i][2] > 400 or umber[i][2] < -10) then table.remove(umber,i) end if umber[i] then if umber[i][6] == "d" then umber[i][2] = umber[i][2] + umber[i][5]*dt*speed else umber[i][2] = umber[i][2] - umber[i][5]*dt*speed end end end end if timer >= 8/(speed/2) and not toch then timer = 0 table.insert(umber, {math.random(0,99), 0, 4, 4, 50, "d"})--x,y,w,h,speed table.insert(umber, {math.random(0,99), 400, 4, 4, 50, "u"}) speed = speed + 0.02 score = score + 2 end if lk.isDown("d") and not toch then kube.x = kube.x+ 400*dt end if lk.isDown("a") and not toch then kube.x = kube.x- 400*dt end if kube.x >= 191 then kube.x = 191 elseif kube.x <= 8 then kube.x = 8 end elseif flag == 6 then if toch then if records[4][2] < score then records[4][2] = score end end timer = timer+10*dt if #umber > 0 and not toch then for i = 1, #umber do if umber[i] and umber[i][2] > 400 then table.remove(umber,i) end if umber[i] then umber[i][2] = umber[i][2] + umber[i][5]*dt*speed end end end if timer >= 1/(speed/2) and not toch then timer = 0 speed = speed + 0.01 score = score + 1 table.insert(umber, {math.random(0,99), 0, 4, 4, 50})--x,y,w,h,speed end if lk.isDown("d") and not toch then kube.x = kube.x+ 400*dt end if lk.isDown("a") and not toch then kube.x = kube.x- 400*dt end if kube.x >= 191 then kube.x = 191 elseif kube.x <= 8 then kube.x = 8 end elseif flag == 7 then if toch then if records[5][2] < score then records[5][2] = score end end timer = timer+10*dt if #umber > 0 and not toch then for i = 1, #umber do if umber[i] and (umber[i][2] > 400 or umber[i][2] < -10) then table.remove(umber,i) end if umber[i] then if umber[i][6] == "d" then umber[i][2] = umber[i][2] + umber[i][5]*dt*speed umber[i][1] = umber[i][1] + umber[i][5]*dt*speed/5 elseif umber[i][6] == "u" then umber[i][2] = umber[i][2] - umber[i][5]*dt*speed umber[i][1] = umber[i][1] - umber[i][5]*dt*speed/5 end end end end if timer >= 8/(speed/2) and not toch then timer = 0 table.insert(umber, {math.random(0,99), 0, 4, 4, 50, "d"})--x,y,w,h,speed table.insert(umber, {math.random(0,99), 400, 4, 4, 50, "u"}) speed = speed + 0.02 score = score + 2 end if lk.isDown("d") and not toch then kube.x = kube.x+ 400*dt end if lk.isDown("a") and not toch then kube.x = kube.x- 400*dt end if kube.x >= 191 then kube.x = 191 elseif kube.x <= 8 then kube.x = 8 end elseif flag == 8 then if toch then if records[6][2] < score then records[6][2] = score end end timer = timer+10*dt if #umber > 0 and not toch then for i = 1, #umber do if umber[i] and (umber[i][2] > 400 or umber[i][2] < -10 or umber[i][1] > 420 or umber[i][1] < -10) then table.remove(umber,i) end if umber[i] then if umber[i][6] == "d" then umber[i][2] = umber[i][2] + umber[i][5]*dt*speed elseif umber[i][6] == "u" then umber[i][2] = umber[i][2] - umber[i][5]*dt*speed elseif umber[i][6] == "l" then umber[i][1] = umber[i][1] + umber[i][5]*dt*speed elseif umber[i][6] == "r" then umber[i][1] = umber[i][1] - umber[i][5]*dt*speed end end end end if timer >= 16/(speed/2) and not toch then timer = 0 table.insert(umber, {math.random(0,99), 0, 4, 4, 50, "d"})--x,y,w,h,speed table.insert(umber, {math.random(0,99), 400, 4, 4, 50, "u"}) table.insert(umber, {0, math.random(0,99)*4, 4, 4, 25, "l"}) table.insert(umber, {100, math.random(0,99)*4, 4, 4, 25, "r"}) speed = speed + 0.04 score = score + 4 end if lk.isDown("d") and not toch then kube.x = kube.x+ 400*dt end if lk.isDown("a") and not toch then kube.x = kube.x- 400*dt end if lk.isDown("w") and not toch then kube.y = kube.y- 400*dt end if lk.isDown("s") and not toch then kube.y = kube.y+ 400*dt end if kube.x >= 191 then kube.x = 191 elseif kube.x <= 8 then kube.x = 8 end if kube.y <= 0 then kube.y = 0 elseif kube.y >= 384 then kube.y = 384 end end end function love.draw() gr.setFont(font) if not flag then for i = 1, #mb do mb[i]:draw() end elseif flag == 1 then back:draw() for i = 1, #records do gr.setColor(255,255,255) gr.print(records[i][1].." ---------- score: "..records[i][2],win.getWidth()/2 - font:getWidth(records[i][1].." ---------- score: "..records[i][2])/2, i*20+70) end elseif flag == 2 then gr.setColor(0,255,0) gr.print("Select the type of game",win.getWidth()/2 - font:getWidth("Select the type of game")/2) gr.setColor(255,255,255) for i = 1, #tgb do tgb[i]:draw() end elseif flag >= 3 then if #umber > 0 then gr.setColor(255,0,0) for i = 1, #umber do if (umber[i][1]*4 >= math.floor(kube.x-12) and umber[i][1]*4 <= math.floor(kube.x+8) and umber[i][2] >= kube.y-4 and umber[i][2] <= kube.y+16) or (umber[i][1]*4 >= math.floor(400-kube.x-12) and umber[i][1]*4 <= math.floor(400-kube.x+8) and umber[i][2] >= kube.y-4 and umber[i][2] <= kube.y+16)then toch = true end gr.rectangle("fill",umber[i][1]*4,umber[i][2],umber[i][3],umber[i][4]) end end if toch then gr.setColor(100,0,0) else gr.setColor(255,255,255) end gr.rectangle("fill",math.floor(kube.x-8),math.floor(kube.y),16,16) gr.rectangle("fill",math.floor(400-kube.x-8),math.floor(kube.y),16,16) gr.setColor(255,255,255) gr.line(200,0,200,400) if not toch then gr.print("Score: "..score.." Speed:"..string.format(" %.1f",speed),0,380) else gr.setFont(big) gr.print("Score: "..score.."\nRecord: "..records[flag-2][2],100,145) end end if toch then gr.setColor(0,0,0) gr.rectangle("fill",100,240,200,100) r:draw() back:draw() end gr.setColor(255,255,255) gr.setFont(mini) gr.print("By Sfabrikan") end function love.mousepressed(x, y, button) --это клик мыши if flag == 2 then for i = 1, #tgb do if tgb[i]:mpress(x,y,button) then if i == 1 then flag = 3 elseif i == 2 then flag = 4 elseif i == 3 then flag = 5 elseif i == 4 then flag = 6 elseif i == 5 then flag = 7 elseif i == 6 then flag = 8 end end end elseif flag == 1 then if back:mpress(x,y,button) then flag = nil end elseif not flag then for i = 1, #mb do if mb[i]:mpress(x,y,button) then if i == 1 then flag = 2 elseif i == 2 then flag = 1 elseif i == 3 then love.event.quit() end end end end if toch then if r:mpress(x,y,button) then restart() elseif back:mpress(x,y,button) then restart() flag = nil end end end function love.keypressed(key) if key == "escape" and flag then if flag > 2 and toch then restart() flag = nil elseif flag <= 2 then flag = nil end elseif key == "r" and flag and flag > 2 and toch then toch = nil kube = {x = 96, y = 196, colx = 8, coly = 8} speed = 1 timer = 0 score = 0 umber = {} end end function love.textinput (t) end function love.quit() local s = {} for i = 1, #records do table.insert(s, records[i][2]) end serialize.save(s, "records.lua") end --button utton = {} function utton:create(x,y,text) --Собсвтенно положение и текст внутри кнопки local t = {} t.toch = false --Завел этот булерн для косания t.x = x t.y = y t.text = text self.__index = self return setmetatable(t,self) end function utton:update(dt) --обновляем self.toch = false --карочи касание фалс local x = love.mouse.getX() --Воруем положение мышки х local y = love.mouse.getY() --Воруем положение мышки y if x > self.x and x < self.x + font:getWidth(self.text) --К положению кнопки + ширина текста self.text and y < self.y + font:getHeight() and y > self.y then --вот такое вот ограменное условие! self.toch = true --Если условие выполнено, до все гуд тру end end function utton:draw() --рисуем love.graphics.setColor(255,255,255) if self.toch then --если true то love.graphics.setColor(255,0,0) end love.graphics.setFont(font) love.graphics.rectangle("line",self.x-2,self.y-2,font:getWidth(self.text)+4,font:getHeight()+4) love.graphics.print(self.text,self.x,self.y) end function utton:mpress(x,y,button) if self.toch then return true end return false end --[[ **Modified for LOVE** Save Table to File Load Table from File v 1.0 Lua 5.2 compatible Only Saves Tables, Numbers, Strings, and Booleans Insides Table References are saved Does not save Userdata, Metatables, Functions and indices of these ---------------------------------------------------- successful,errmsg = save( table,filename ) ---------------------------------------------------- table = load( filename ) ---------------------------------------------------- Licensed under the same terms as Lua itself. ]]-- -- declare local variables --// exportstring( string ) --// returns a "Lua" portable version of the string local function exportstring( s ) return string.format("%q", s) end local function createSubDir(path) local dir,name,ext = path:match('^(.-)([^\\/]-)%.?([^\\/%.]*)$') if #name == 0 then name = ext; ext = '' end local dirpath = '' for subfolder in dir:gmatch('([^\\/]-[\\/])') do dirpath = dirpath..subfolder if love.filesystem.isFile(dirpath) then return 'Cannot save table, '..dirpath..' is a file' end if not love.filesystem.exists(dirpath) then love.filesystem.mkdir(dirpath) end end end local insert = function(t,v) if t.len then t.len = t.len+1 t[t.len] = v return end table.insert(t,v) end serialize = {} --// The Save Function function serialize.save( tbl,filename ) local charS,charE = "\t","\n" local err = createSubDir(filename) if err then return false,err end local file = love.filesystem.newFile(filename) file:open('w') -- initiate variables for save procedure local tables,lookup = { tbl; len = 1 },{ [tbl] = 1 } file:write( "return {"..charE ) for idx,t in ipairs( tables ) do file:write( "-- Table: {"..idx.."}"..charE ) file:write( "{"..charE ) local thandled = {} for i,v in ipairs( t ) do thandled[i] = true local stype = type( v ) -- only handle value if stype == "table" then if not lookup[v] then insert( tables, v ) lookup[v] = #tables end file:write( charS.."{"..lookup[v].."},"..charE ) elseif stype == "string" then file:write( charS..exportstring( v )..","..charE ) elseif stype == "number" or stype == 'boolean' then file:write( charS..tostring( v )..","..charE ) end end for i,v in pairs( t ) do -- escape handled values if (not thandled[i]) then local str = "" local stype = type( i ) -- handle index -- if stype == "table" then -- if not lookup[i] then -- insert( tables,i ) -- lookup[i] = #tables -- end -- str = charS.."[{"..lookup[i].."}]=" if stype == "string" then -- elseif stype == "string" then str = charS.."["..exportstring( i ).."]=" elseif stype == "number" or stype == 'boolean' then str = charS.."["..tostring( i ).."]=" end if str ~= "" then stype = type( v ) -- handle value if stype == "table" then if not lookup[v] then insert( tables,v ) lookup[v] = #tables end file:write( str.."{"..lookup[v].."},"..charE ) elseif stype == "string" then file:write( str..exportstring( v )..","..charE ) elseif stype == "number" or stype == 'boolean' then file:write( str..tostring( v )..","..charE ) end end end end file:write( "},"..charE ) end file:write( "}" ) file:close() return true end --// The Load Function function serialize.load( filename ) if not love.filesystem.exists(filename) then return end local tables = love.filesystem.load(filename)() for idx = 1,#tables do local tolinki = {} for i,v in pairs( tables[idx] ) do if type( v ) == "table" then tables[idx][i] = tables[v[1]] end if type( i ) == "table" and tables[i[1]] then insert( tolinki,{ i,tables[i[1]] } ) end end -- link indices for _,v in ipairs( tolinki ) do tables[idx][v[2]],tables[idx][v[1]] = tables[idx][v[1]],nil end end return tables[1] end -- ChillCode