Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local game_state = {
- size = 10,
- field = {
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 1, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
- {0, 1, 1, 1, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
- },
- time = 0
- }
- function game_state:next()
- local neighbours = function(i, j)
- local left = (i > 1) and (i - 1) or self.size
- local right = (i < self.size) and i + 1 or 1
- local up = (j > 1) and j - 1 or self.size
- local down = (j < self.size) and j + 1 or 1
- local field = self.field
- return {
- field[left][up], field[i][up], field[right][up],
- field[left][j], field[right][j],
- field[left][down], field[i][down], field[right][down]
- }
- end
- local sum = function(arr)
- local s = 0
- for i, v in ipairs(arr) do
- s = s + v
- end
- return s
- end
- local temp = {}
- for i = 1, self.size do
- temp[i] = {}
- for j = 1, self.size do
- local n = sum(neighbours(i, j))
- if self.field[i][j] == 1 then
- temp[i][j] = (n < 2 or 3 < n) and 0 or 1
- else
- temp[i][j] = (n == 3) and 1 or 0
- end
- end
- end
- self.field = temp
- return temp
- end
- local g = love.graphics
- local dimension = 800
- local update_interval = 0.15
- function love.load()
- love.window.setMode(dimension, dimension)
- end
- function love.draw()
- local cell_size = dimension / game_state.size
- for i = 1, game_state.size do
- for j = 1, game_state.size do
- if game_state.field[i][j] == 1 then
- g.rectangle("fill",
- cell_size * (j - 1), cell_size * (i - 1),
- cell_size, cell_size)
- end
- end
- end
- end
- function love.update(dt)
- game_state.time = game_state.time + dt
- if game_state.time > update_interval then
- game_state:next()
- game_state.time = 0
- end
- end
Add Comment
Please, Sign In to add comment