-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
93 lines (72 loc) · 2.29 KB
/
main.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
MORTO = false
VIVO = true
local screenWidth = love.graphics.getWidth() -- 800
local screenHeight = love.graphics.getHeight() -- 600
function love.load()
cellSize = 20
cellDrawSize = cellSize - 1
grid = {}
gridXCount = screenWidth / cellSize
gridYCount = screenHeight / cellSize
for x = 1, gridXCount + 1 do
grid[x] = {}
for y = 1, gridYCount + 1 do
grid[x][y] = MORTO
end
end
end
function love.update()
mouseXLocation = math.floor(love.mouse.getX() / cellSize) + 1
mouseYLocation = math.floor(love.mouse.getY() / cellSize) + 1
if love.mouse.isDown(1) then
grid[mouseXLocation][mouseYLocation] = VIVO
end
if love.mouse.isDown(2) then
grid[mouseXLocation][mouseYLocation] = MORTO
end
end
function love.draw()
love.graphics.setColor(1,1,1)
for y = 1, screenHeight / cellSize do
for x = 1, screenWidth / cellSize do
if grid[x][y] == VIVO then
love.graphics.setColor(1,0,1)
elseif x == mouseXLocation and y == mouseYLocation then
love.graphics.setColor(0,.75,1)
else
love.graphics.setColor(1,1,1)
end
love.graphics.rectangle(
'fill',
(x - 1) * (cellSize),
(y - 1) * (cellSize),
cellDrawSize,
cellDrawSize
)
end
end
love.graphics.setColor(0,0,0)
love.graphics.print("O mouse esta em: " .. mouseXLocation .. ", " .. mouseYLocation)
end
function love.keypressed()
nextGrid = {}
nextGridXCount = screenWidth / cellSize
nextGridYCount = screenHeight / cellSize
for x = 1, nextGridXCount + 1 do
nextGrid[x] = {}
for y = 1, nextGridYCount + 1 do
local neighbourCount = 0
for dx = -1, 1 do
for dy = -1, 1 do
if not (dy == 0 and dx == 0) and
grid[x + dx] and
grid[x + dx][y + dy] == VIVO then
neighbourCount = neighbourCount + 1
end
end
end
nextGrid[x][y] = neighbourCount == 3 or (grid[x][y] and neighbourCount == 2)
end
end
grid = nextGrid
end