Xenthera

Game Of Life

Oct 6th, 2013
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. function love.load()
  2. lg = love.graphics
  3. lg.setBackgroundColor(150,150,150)
  4.  
  5. width,height = lg.getWidth(),lg.getHeight()
  6. pause = true
  7.  
  8. size = 12
  9.  
  10.  
  11. numberx = math.floor(width / (size + 1))
  12. numbery = math.floor(height / (size + 1))
  13.  
  14. startx = width % (size + 1) / 2
  15. starty = height % (size + 1) / 2
  16.  
  17. world = {}
  18. for i = 1, numberx do
  19. world[i] = {}
  20. for j = 1, numbery do
  21. world[i][j] = false
  22. end
  23. end
  24. x = 0
  25. y = 0
  26.  
  27. paused = true
  28.  
  29. masterClock = 0
  30. step = 0
  31.  
  32. --Menu (Not implemented)
  33.  
  34. menuBar = {}
  35. menuBar.x = 1
  36. menuBar.y = -30
  37. menuBar.height = 30
  38. menuBar.speed = 4
  39.  
  40.  
  41. buttons = {}
  42. buttons[1] = {}
  43. buttons[1].x = 10
  44. buttons[1].y = 10
  45. buttons[1].width = 100
  46. buttons[1].height = 100
  47.  
  48.  
  49.  
  50.  
  51.  
  52. backgroundC = 150
  53.  
  54.  
  55. end
  56.  
  57. function love.update( dt )
  58. masterClock = masterClock + dt
  59. if love.mouse.isDown("l") then
  60. mousepressed("l")
  61. elseif love.mouse.isDown("r") then
  62. mousepressed("r")
  63. end
  64.  
  65. if not paused then
  66. if backgroundC > 150 then
  67. backgroundC = backgroundC - 0.5
  68. end
  69. else
  70. if backgroundC < 160 then
  71. backgroundC = backgroundC + 1
  72. end
  73. end
  74.  
  75.  
  76.  
  77. end
  78.  
  79. function love.draw()
  80. if masterClock - step >= 0.05 then
  81. step = masterClock
  82. if not paused then evolve() end
  83. end
  84. sinP = math.abs(math.sin(masterClock * 10) * 255)
  85. cosC = math.abs(math.cos(masterClock * 2) * 55)
  86. lg.setColor(100,100,100)
  87. n = 0
  88. for i = 1, #world do
  89. for j = 1, #world[i] do
  90.  
  91. if world[i][j] == true then
  92. if paused then
  93. lg.setColor(200 + cosC,200 + cosC, 55 + -cosC)
  94. else
  95. lg.setColor(0,255,100)
  96. end
  97. else
  98. if paused then
  99.  
  100. lg.setColor(backgroundC,backgroundC,backgroundC)
  101.  
  102. else
  103.  
  104. lg.setColor(backgroundC,backgroundC,backgroundC)
  105.  
  106. end
  107.  
  108. end
  109.  
  110. drawSquare(((i - 1) * size) + i + startx, ((j - 1) * size) + j + starty, size)
  111.  
  112. if world[i][j] == true then n = n + 1 end
  113.  
  114.  
  115. end
  116. end
  117.  
  118. lg.setCaption("Conway's Game Of Life - ".."FPS: "..love.timer.getFPS().." Pixels Active: "..n)
  119.  
  120.  
  121.  
  122.  
  123.  
  124. if paused then
  125. lg.setColor(0,0,0,180)
  126. lg.rectangle("fill",width / 2 - 147 / 2,10 + 3 ,147,20)
  127. lg.setColor(255,255,255,sinP)
  128. lg.print("< Paused >",width / 2 + 8 - 147/2,10 + 4 + 2 )
  129. end
  130.  
  131.  
  132. end
  133.  
  134. function buttonCheck(x,y)
  135.  
  136. end
  137.  
  138. function drawSquare(x,y,size)
  139. lg.rectangle("fill",x,y,size,size)
  140.  
  141. end
  142.  
  143. function mousepressed(button)
  144. x = love.mouse.getX()
  145. y = love.mouse.getY()
  146.  
  147. for i = 1, numberx do
  148. for j = 1, numbery do
  149. if x > i + ((i - 1) * size) + startx and x < i + ((i - 1) * size) + startx + size + 2 then
  150. if y > j + ((j - 1) * size) + starty and y < j + ((j - 1) * size) + starty + size + 2 then
  151. if button == "l" then world[i][j] = true elseif button == "r" then world[i][j] = false end
  152. end
  153. end
  154.  
  155. end
  156. end
  157.  
  158. end
  159.  
  160. function love.keypressed( key )
  161. if key == "p" then paused = not paused end
  162. end
  163.  
  164. function pixel(x,y)
  165. if math.floor(x) < numberx and math.floor(y) < numbery and math.floor(x) > 0 and math.floor(y) > 0 then
  166. world[math.floor(x) ][math.floor(y) ] = true
  167. end
  168. end
  169.  
  170. function line(x1,y1,x2,y2)
  171. dx = math.abs(x2 - x1)
  172. dy = math.abs(y2 - y1)
  173.  
  174. if x1 < x2 then sx = 1 else sx = -1 end
  175. if y1 < y2 then sy = 1 else sy = -1 end
  176.  
  177. err = dx - dy
  178.  
  179. while true do
  180. pixel(x1,y1)
  181. if x1 == x2 and y1 == y2 then break end
  182. e2 = 2 * err
  183. if e2 > -dy then
  184. err = err - dy
  185. x1 = x1 + sx
  186. end
  187. if x1 == x2 and y1 == y2 then
  188. pixel(x1,y1)
  189. break
  190. end
  191. if e2 < dx then
  192. err = err + dx
  193. y1 = y1 + sy
  194. end
  195. end
  196. end
  197.  
  198. function clear()
  199. for i = 1, numberx do
  200. for j = 1, numbery do
  201. world[i][j] = false
  202. end
  203. end
  204. end
  205.  
  206. function circle(x,y,r)
  207.  
  208. multiplyPoint = r * (math.pi * 2)
  209. for i = 1, multiplyPoint + 1 do
  210. angle = i * (2 * math.pi) / (multiplyPoint)
  211. cosx = x + (math.cos(angle) * r)
  212. siny = y + (math.sin(angle) * r)
  213.  
  214. pixel(cosx + 1, siny + 1)
  215. end
  216.  
  217. end
  218.  
  219. function evolve()
  220. tempWorld = {}
  221. for i = 1, numberx do
  222. tempWorld[i] = {}
  223. for j = 1, numbery do
  224.  
  225. count = getNeighbors(i,j)
  226.  
  227. if world[i][j] == true then
  228. if count < 2 then
  229. tempWorld[i][j] = false
  230. elseif count > 3 then
  231. tempWorld[i][j] = false
  232. else
  233. tempWorld[i][j] = true
  234. end
  235.  
  236. elseif world[i][j] == false then
  237. if count == 3 then
  238. tempWorld[i][j] = true
  239. else
  240. tempWorld[i][j] = false
  241. end
  242.  
  243.  
  244. end
  245. end
  246. end
  247.  
  248. world = tempWorld
  249. tempWorld = {}
  250.  
  251. end
  252.  
  253. function getNeighbors(x,y)
  254. count = 0
  255.  
  256. if(x > 1 and y > 1) and (x < numberx and y < numberx) then
  257. if(world[x-1][y-1] == true)then count = count + 1 end
  258. if(world[x][y-1] == true)then count = count + 1 end
  259. if(world[x+1][y-1] == true)then count = count + 1 end
  260. if(world[x-1][y] == true)then count = count + 1 end
  261.  
  262. if(world[x+1][y] == true)then count = count + 1 end
  263. if(world[x-1][y+1] == true)then count = count + 1 end
  264. if(world[x][y+1] == true)then count = count + 1 end
  265. if(world[x+1][y+1] == true)then count = count + 1 end
  266. end
  267. return count
  268.  
  269. end
Advertisement
Add Comment
Please, Sign In to add comment