fork download
  1. gr = love.graphics
  2. win = love.window
  3. lk = love.keyboard
  4. require("button")
  5. require("t")
  6.  
  7. function love.load()
  8. local loading = {}
  9. loading = serialize.load("records.lua")
  10. font = gr.newFont("NotoSans.ttf",14)
  11. mini = gr.newFont("NotoSans.ttf",10)
  12. big = gr.newFont("NotoSans.ttf",30)
  13. 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
  14. kube = {x = 96, y = 196, colx = 8, coly = 8}
  15. speed = 1
  16. timer = 0
  17. umber = {}
  18. records = {{"Standart", 0},{"Impulse", 0},{"Invers", 0},{"Unreal", 0},{"XY diagonal", 0},{"Perpendiculars", 0}}
  19. if loading and #loading > 0 then
  20. for i = 1, #loading do
  21. records[i][2] = loading[i]
  22. end
  23. end
  24. score = 0
  25. 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<")}
  26. 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<")}
  27. back = utton:create(200-font:getWidth(" >Back< ")/2,300," >Back< ")
  28. r = utton:create(200-font:getWidth(" >Restart< ")/2,250," >Restart< ")
  29. end
  30.  
  31. local function restart()
  32. kube = {x = 96, y = 196, colx = 8, coly = 8, speed = 350}
  33. speed = 1
  34. timer = 0
  35. umber = {}
  36. score = 0
  37. toch = nil
  38. end
  39.  
  40. function love.update(dt)
  41. math.randomseed(math.random(os.time(), os.time()*100))
  42. if toch then
  43. r:update(dt)
  44. back:update(dt)
  45. end
  46. if not flag then
  47. for i = 1, #mb do
  48. mb[i]:update(dt)
  49. end
  50. elseif flag == 1 then
  51. back:update(dt)
  52. elseif flag == 2 then
  53. for i = 1, #tgb do
  54. tgb[i]:update(dt)
  55. end
  56. elseif flag == 3 then
  57. if toch then
  58. if records[1][2] < score then
  59. records[1][2] = score
  60. end
  61. end
  62. timer = timer+10*dt
  63. if #umber > 0 and not toch then
  64. for i = 1, #umber do
  65. if umber[i] and umber[i][2] > 400 then
  66. table.remove(umber,i)
  67. end
  68. if umber[i] then
  69. umber[i][2] = umber[i][2] + umber[i][5]*dt*speed
  70. end
  71. end
  72. end
  73. if timer >= 4/(speed/2) and not toch then
  74. timer = 0
  75. speed = speed + 0.01
  76. score = score + 1
  77. table.insert(umber, {math.random(0,99), 0, 4, 4, 50})--x,y,w,h,speed
  78. end
  79. if lk.isDown("d") and not toch then
  80. kube.x = kube.x+ 400*dt
  81. end
  82. if lk.isDown("a") and not toch then
  83. kube.x = kube.x- 400*dt
  84. end
  85. if kube.x >= 191 then
  86. kube.x = 191
  87. elseif kube.x <= 8 then
  88. kube.x = 8
  89. end
  90. elseif flag == 4 then
  91. if toch then
  92. if records[2][2] < score then
  93. records[2][2] = score
  94. end
  95. end
  96. timer = timer+10*dt
  97. if #umber > 0 and not toch then
  98. for i = 1, #umber do
  99. if umber[i] and umber[i][2] > 400 then
  100. table.remove(umber,i)
  101. end
  102. if umber[i] then
  103. umber[i][2] = umber[i][2] + umber[i][5]*dt*speed
  104. end
  105. end
  106. end
  107. if timer >= 15/(speed/2) and not toch then
  108. timer = 0
  109. for x = 0, 5 do
  110. table.insert(umber, {math.random(0,99), 0, 4, 4, 50})--x,y,w,h,speed
  111. speed = speed + 0.01
  112. score = score + 1
  113. end
  114. end
  115. if lk.isDown("d") and not toch then
  116. kube.x = kube.x+ 400*dt
  117. end
  118. if lk.isDown("a") and not toch then
  119. kube.x = kube.x- 400*dt
  120. end
  121. if kube.x >= 191 then
  122. kube.x = 191
  123. elseif kube.x <= 8 then
  124. kube.x = 8
  125. end
  126. elseif flag == 5 then
  127. if toch then
  128. if records[3][2] < score then
  129. records[3][2] = score
  130. end
  131. end
  132. timer = timer+10*dt
  133. if #umber > 0 and not toch then
  134. for i = 1, #umber do
  135. if umber[i] and (umber[i][2] > 400 or umber[i][2] < -10) then
  136. table.remove(umber,i)
  137. end
  138. if umber[i] then
  139. if umber[i][6] == "d" then
  140. umber[i][2] = umber[i][2] + umber[i][5]*dt*speed
  141. else
  142. umber[i][2] = umber[i][2] - umber[i][5]*dt*speed
  143. end
  144. end
  145. end
  146. end
  147. if timer >= 8/(speed/2) and not toch then
  148. timer = 0
  149. table.insert(umber, {math.random(0,99), 0, 4, 4, 50, "d"})--x,y,w,h,speed
  150. table.insert(umber, {math.random(0,99), 400, 4, 4, 50, "u"})
  151. speed = speed + 0.02
  152. score = score + 2
  153. end
  154. if lk.isDown("d") and not toch then
  155. kube.x = kube.x+ 400*dt
  156. end
  157. if lk.isDown("a") and not toch then
  158. kube.x = kube.x- 400*dt
  159. end
  160. if kube.x >= 191 then
  161. kube.x = 191
  162. elseif kube.x <= 8 then
  163. kube.x = 8
  164. end
  165. elseif flag == 6 then
  166. if toch then
  167. if records[4][2] < score then
  168. records[4][2] = score
  169. end
  170. end
  171. timer = timer+10*dt
  172. if #umber > 0 and not toch then
  173. for i = 1, #umber do
  174. if umber[i] and umber[i][2] > 400 then
  175. table.remove(umber,i)
  176. end
  177. if umber[i] then
  178. umber[i][2] = umber[i][2] + umber[i][5]*dt*speed
  179. end
  180. end
  181. end
  182. if timer >= 1/(speed/2) and not toch then
  183. timer = 0
  184. speed = speed + 0.01
  185. score = score + 1
  186. table.insert(umber, {math.random(0,99), 0, 4, 4, 50})--x,y,w,h,speed
  187. end
  188. if lk.isDown("d") and not toch then
  189. kube.x = kube.x+ 400*dt
  190. end
  191. if lk.isDown("a") and not toch then
  192. kube.x = kube.x- 400*dt
  193. end
  194. if kube.x >= 191 then
  195. kube.x = 191
  196. elseif kube.x <= 8 then
  197. kube.x = 8
  198. end
  199. elseif flag == 7 then
  200. if toch then
  201. if records[5][2] < score then
  202. records[5][2] = score
  203. end
  204. end
  205. timer = timer+10*dt
  206. if #umber > 0 and not toch then
  207. for i = 1, #umber do
  208. if umber[i] and (umber[i][2] > 400 or umber[i][2] < -10) then
  209. table.remove(umber,i)
  210. end
  211. if umber[i] then
  212. if umber[i][6] == "d" then
  213. umber[i][2] = umber[i][2] + umber[i][5]*dt*speed
  214. umber[i][1] = umber[i][1] + umber[i][5]*dt*speed/5
  215. elseif umber[i][6] == "u" then
  216. umber[i][2] = umber[i][2] - umber[i][5]*dt*speed
  217. umber[i][1] = umber[i][1] - umber[i][5]*dt*speed/5
  218. end
  219. end
  220. end
  221. end
  222. if timer >= 8/(speed/2) and not toch then
  223. timer = 0
  224. table.insert(umber, {math.random(0,99), 0, 4, 4, 50, "d"})--x,y,w,h,speed
  225. table.insert(umber, {math.random(0,99), 400, 4, 4, 50, "u"})
  226. speed = speed + 0.02
  227. score = score + 2
  228. end
  229. if lk.isDown("d") and not toch then
  230. kube.x = kube.x+ 400*dt
  231. end
  232. if lk.isDown("a") and not toch then
  233. kube.x = kube.x- 400*dt
  234. end
  235. if kube.x >= 191 then
  236. kube.x = 191
  237. elseif kube.x <= 8 then
  238. kube.x = 8
  239. end
  240. elseif flag == 8 then
  241. if toch then
  242. if records[6][2] < score then
  243. records[6][2] = score
  244. end
  245. end
  246. timer = timer+10*dt
  247. if #umber > 0 and not toch then
  248. for i = 1, #umber do
  249. if umber[i] and (umber[i][2] > 400 or umber[i][2] < -10 or umber[i][1] > 420 or umber[i][1] < -10) then
  250. table.remove(umber,i)
  251. end
  252. if umber[i] then
  253. if umber[i][6] == "d" then
  254. umber[i][2] = umber[i][2] + umber[i][5]*dt*speed
  255. elseif umber[i][6] == "u" then
  256. umber[i][2] = umber[i][2] - umber[i][5]*dt*speed
  257. elseif umber[i][6] == "l" then
  258. umber[i][1] = umber[i][1] + umber[i][5]*dt*speed
  259. elseif umber[i][6] == "r" then
  260. umber[i][1] = umber[i][1] - umber[i][5]*dt*speed
  261. end
  262. end
  263. end
  264. end
  265. if timer >= 16/(speed/2) and not toch then
  266. timer = 0
  267. table.insert(umber, {math.random(0,99), 0, 4, 4, 50, "d"})--x,y,w,h,speed
  268. table.insert(umber, {math.random(0,99), 400, 4, 4, 50, "u"})
  269. table.insert(umber, {0, math.random(0,99)*4, 4, 4, 25, "l"})
  270. table.insert(umber, {100, math.random(0,99)*4, 4, 4, 25, "r"})
  271. speed = speed + 0.04
  272. score = score + 4
  273. end
  274. if lk.isDown("d") and not toch then
  275. kube.x = kube.x+ 400*dt
  276. end
  277. if lk.isDown("a") and not toch then
  278. kube.x = kube.x- 400*dt
  279. end
  280. if lk.isDown("w") and not toch then
  281. kube.y = kube.y- 400*dt
  282. end
  283. if lk.isDown("s") and not toch then
  284. kube.y = kube.y+ 400*dt
  285. end
  286. if kube.x >= 191 then
  287. kube.x = 191
  288. elseif kube.x <= 8 then
  289. kube.x = 8
  290. end
  291. if kube.y <= 0 then
  292. kube.y = 0
  293. elseif kube.y >= 384 then
  294. kube.y = 384
  295. end
  296. end
  297. end
  298.  
  299.  
  300.  
  301. function love.draw()
  302. gr.setFont(font)
  303. if not flag then
  304. for i = 1, #mb do
  305. mb[i]:draw()
  306. end
  307. elseif flag == 1 then
  308. back:draw()
  309. for i = 1, #records do
  310. gr.setColor(255,255,255)
  311. 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)
  312. end
  313. elseif flag == 2 then
  314. gr.setColor(0,255,0)
  315. gr.print("Select the type of game",win.getWidth()/2 - font:getWidth("Select the type of game")/2)
  316. gr.setColor(255,255,255)
  317. for i = 1, #tgb do
  318. tgb[i]:draw()
  319. end
  320. elseif flag >= 3 then
  321. if #umber > 0 then
  322. gr.setColor(255,0,0)
  323. for i = 1, #umber do
  324. 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
  325. toch = true
  326. end
  327. gr.rectangle("fill",umber[i][1]*4,umber[i][2],umber[i][3],umber[i][4])
  328. end
  329. end
  330. if toch then
  331. gr.setColor(100,0,0)
  332. else
  333. gr.setColor(255,255,255)
  334. end
  335. gr.rectangle("fill",math.floor(kube.x-8),math.floor(kube.y),16,16)
  336. gr.rectangle("fill",math.floor(400-kube.x-8),math.floor(kube.y),16,16)
  337. gr.setColor(255,255,255)
  338. gr.line(200,0,200,400)
  339. if not toch then
  340. gr.print("Score: "..score.." Speed:"..string.format(" %.1f",speed),0,380)
  341. else
  342. gr.setFont(big)
  343. gr.print("Score: "..score.."\nRecord: "..records[flag-2][2],100,145)
  344. end
  345. end
  346. if toch then
  347. gr.setColor(0,0,0)
  348. gr.rectangle("fill",100,240,200,100)
  349. r:draw()
  350. back:draw()
  351. end
  352. gr.setColor(255,255,255)
  353. gr.setFont(mini)
  354. gr.print("By Sfabrikan")
  355. end
  356.  
  357.  
  358.  
  359.  
  360.  
  361. function love.mousepressed(x, y, button) --это клик мыши
  362. if flag == 2 then
  363. for i = 1, #tgb do
  364. if tgb[i]:mpress(x,y,button) then
  365. if i == 1 then
  366. flag = 3
  367. elseif i == 2 then
  368. flag = 4
  369. elseif i == 3 then
  370. flag = 5
  371. elseif i == 4 then
  372. flag = 6
  373. elseif i == 5 then
  374. flag = 7
  375. elseif i == 6 then
  376. flag = 8
  377. end
  378. end
  379. end
  380. elseif flag == 1 then
  381. if back:mpress(x,y,button) then
  382. flag = nil
  383. end
  384. elseif not flag then
  385. for i = 1, #mb do
  386. if mb[i]:mpress(x,y,button) then
  387. if i == 1 then
  388. flag = 2
  389. elseif i == 2 then
  390. flag = 1
  391. elseif i == 3 then
  392. love.event.quit()
  393. end
  394. end
  395. end
  396. end
  397. if toch then
  398. if r:mpress(x,y,button) then
  399. restart()
  400. elseif back:mpress(x,y,button) then
  401. restart()
  402. flag = nil
  403. end
  404. end
  405. end
  406.  
  407. function love.keypressed(key)
  408. if key == "escape" and flag then
  409. if flag > 2 and toch then
  410. restart()
  411. flag = nil
  412. elseif flag <= 2 then
  413. flag = nil
  414. end
  415. elseif key == "r" and flag and flag > 2 and toch then
  416. toch = nil
  417. kube = {x = 96, y = 196, colx = 8, coly = 8}
  418. speed = 1
  419. timer = 0
  420. score = 0
  421. umber = {}
  422. end
  423. end
  424.  
  425. function love.textinput (t)
  426.  
  427. end
  428.  
  429. function love.quit()
  430. local s = {}
  431. for i = 1, #records do
  432. table.insert(s, records[i][2])
  433. end
  434. serialize.save(s, "records.lua")
  435. end
  436.  
  437. --button
  438.  
  439. utton = {}
  440.  
  441. function utton:create(x,y,text) --Собсвтенно положение и текст внутри кнопки
  442. local t = {}
  443. t.toch = false --Завел этот булерн для косания
  444. t.x = x
  445. t.y = y
  446. t.text = text
  447. self.__index = self
  448. return setmetatable(t,self)
  449. end
  450.  
  451. function utton:update(dt) --обновляем
  452. self.toch = false --карочи касание фалс
  453. local x = love.mouse.getX() --Воруем положение мышки х
  454. local y = love.mouse.getY() --Воруем положение мышки y
  455. if x > self.x
  456. and x < self.x + font:getWidth(self.text) --К положению кнопки + ширина текста self.text
  457. and y < self.y + font:getHeight()
  458. and y > self.y then --вот такое вот ограменное условие!
  459. self.toch = true --Если условие выполнено, до все гуд тру
  460. end
  461. end
  462.  
  463. function utton:draw() --рисуем
  464. love.graphics.setColor(255,255,255)
  465. if self.toch then --если true то
  466. love.graphics.setColor(255,0,0)
  467. end
  468. love.graphics.setFont(font)
  469. love.graphics.rectangle("line",self.x-2,self.y-2,font:getWidth(self.text)+4,font:getHeight()+4)
  470. love.graphics.print(self.text,self.x,self.y)
  471. end
  472.  
  473. function utton:mpress(x,y,button)
  474. if self.toch then
  475. return true
  476. end
  477. return false
  478. end
  479.  
  480. --[[
  481. **Modified for LOVE**
  482. Save Table to File
  483. Load Table from File
  484. v 1.0
  485.  
  486. Lua 5.2 compatible
  487.  
  488. Only Saves Tables, Numbers, Strings, and Booleans
  489. Insides Table References are saved
  490. Does not save Userdata, Metatables, Functions and indices of these
  491. ----------------------------------------------------
  492. successful,errmsg = save( table,filename )
  493.  
  494. ----------------------------------------------------
  495. table = load( filename )
  496.  
  497. ----------------------------------------------------
  498.  
  499. Licensed under the same terms as Lua itself.
  500. ]]--
  501. -- declare local variables
  502. --// exportstring( string )
  503. --// returns a "Lua" portable version of the string
  504. local function exportstring( s )
  505. return string.format("%q", s)
  506. end
  507.  
  508. local function createSubDir(path)
  509. local dir,name,ext = path:match('^(.-)([^\\/]-)%.?([^\\/%.]*)$')
  510. if #name == 0 then name = ext; ext = '' end
  511. local dirpath = ''
  512. for subfolder in dir:gmatch('([^\\/]-[\\/])') do
  513. dirpath = dirpath..subfolder
  514. if love.filesystem.isFile(dirpath) then
  515. return 'Cannot save table, '..dirpath..' is a file'
  516. end
  517. if not love.filesystem.exists(dirpath) then
  518. love.filesystem.mkdir(dirpath)
  519. end
  520. end
  521. end
  522.  
  523. local insert = function(t,v)
  524. if t.len then
  525. t.len = t.len+1
  526. t[t.len] = v
  527. return
  528. end
  529. table.insert(t,v)
  530. end
  531.  
  532. serialize = {}
  533.  
  534. --// The Save Function
  535. function serialize.save( tbl,filename )
  536. local charS,charE = "\t","\n"
  537.  
  538. local err = createSubDir(filename)
  539. if err then return false,err end
  540.  
  541. local file = love.filesystem.newFile(filename)
  542. file:open('w')
  543.  
  544. -- initiate variables for save procedure
  545. local tables,lookup = { tbl; len = 1 },{ [tbl] = 1 }
  546. file:write( "return {"..charE )
  547.  
  548. for idx,t in ipairs( tables ) do
  549. file:write( "-- Table: {"..idx.."}"..charE )
  550. file:write( "{"..charE )
  551. local thandled = {}
  552.  
  553. for i,v in ipairs( t ) do
  554. thandled[i] = true
  555. local stype = type( v )
  556. -- only handle value
  557. if stype == "table" then
  558. if not lookup[v] then
  559. insert( tables, v )
  560. lookup[v] = #tables
  561. end
  562. file:write( charS.."{"..lookup[v].."},"..charE )
  563. elseif stype == "string" then
  564. file:write( charS..exportstring( v )..","..charE )
  565. elseif stype == "number" or stype == 'boolean' then
  566. file:write( charS..tostring( v )..","..charE )
  567. end
  568. end
  569.  
  570. for i,v in pairs( t ) do
  571. -- escape handled values
  572. if (not thandled[i]) then
  573.  
  574. local str = ""
  575. local stype = type( i )
  576. -- handle index
  577. -- if stype == "table" then
  578. -- if not lookup[i] then
  579. -- insert( tables,i )
  580. -- lookup[i] = #tables
  581. -- end
  582. -- str = charS.."[{"..lookup[i].."}]="
  583. if stype == "string" then
  584. -- elseif stype == "string" then
  585. str = charS.."["..exportstring( i ).."]="
  586. elseif stype == "number" or stype == 'boolean' then
  587. str = charS.."["..tostring( i ).."]="
  588. end
  589.  
  590. if str ~= "" then
  591. stype = type( v )
  592. -- handle value
  593. if stype == "table" then
  594. if not lookup[v] then
  595. insert( tables,v )
  596. lookup[v] = #tables
  597. end
  598. file:write( str.."{"..lookup[v].."},"..charE )
  599. elseif stype == "string" then
  600. file:write( str..exportstring( v )..","..charE )
  601. elseif stype == "number" or stype == 'boolean' then
  602. file:write( str..tostring( v )..","..charE )
  603. end
  604. end
  605. end
  606. end
  607. file:write( "},"..charE )
  608. end
  609. file:write( "}" )
  610. file:close()
  611.  
  612. return true
  613. end
  614.  
  615. --// The Load Function
  616. function serialize.load( filename )
  617. if not love.filesystem.exists(filename) then return end
  618. local tables = love.filesystem.load(filename)()
  619. for idx = 1,#tables do
  620. local tolinki = {}
  621. for i,v in pairs( tables[idx] ) do
  622. if type( v ) == "table" then
  623. tables[idx][i] = tables[v[1]]
  624. end
  625. if type( i ) == "table" and tables[i[1]] then
  626. insert( tolinki,{ i,tables[i[1]] } )
  627. end
  628. end
  629. -- link indices
  630. for _,v in ipairs( tolinki ) do
  631. tables[idx][v[2]],tables[idx][v[1]] = tables[idx][v[1]],nil
  632. end
  633. end
  634. return tables[1]
  635. end
  636.  
  637. -- ChillCode
Runtime error #stdin #stdout #stderr 0s 14112KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
lua5.3: prog.lua:1: attempt to index a nil value (global 'love')
stack traceback:
	prog.lua:1: in main chunk
	[C]: in ?