最終更新 7 hours ago

A small lights out game written for LOVE. It doesn't check win states tho.

capitalex's Avatar capitalex revised this gist 7 hours ago. Go to revision

1 file changed, 49 insertions

main.lua(file created)

@@ -0,0 +1,49 @@
1 + local size = 100
2 + local offsetH, offsetW = love.graphics.getHeight() / 2 - 250, love.graphics.getWidth() / 2 - 250
3 +
4 + local grid = {
5 + {false, false, false, false, false},
6 + {false, false, false, false, false},
7 + {false, false, false, false, false},
8 + {false, false, false, false, false},
9 + {false, false, false, false, false},
10 + }
11 +
12 + function flip(x, y)
13 + if grid[y] ~= nil and grid[y][x] ~= nil then
14 + grid[y][x] = not grid[y][x]
15 + end
16 + end
17 +
18 + function toggle(x, y)
19 + flip(x, y + 1, grid)
20 + flip(x + 1, y, grid)
21 + flip(x, y, grid)
22 + flip(x - 1, y, grid)
23 + flip(x, y - 1, grid)
24 + end
25 +
26 + function love.load()
27 + math.randomseed(os.time())
28 + for i = 1, 20 do
29 + toggle(math.random(1, 5), math.random(1, 5))
30 + end
31 + end
32 +
33 + function love.draw()
34 + for y = 1, 5 do
35 + for x = 1, 5 do
36 + if grid[y][x] then
37 + love.graphics.rectangle("fill", (x - 1) * size + offsetW, (y - 1) * size + offsetH, size, size)
38 + else
39 + love.graphics.rectangle("line", (x - 1) * size + offsetW, (y - 1) * size + offsetH, size, size)
40 + end
41 + end
42 + end
43 + end
44 +
45 + function love.mousepressed(x, y, button)
46 + local gridx = math.floor((x - offsetW) / size) + 1
47 + local gridy = math.floor((y - offsetH) / size) + 1
48 + toggle(gridx, gridy)
49 + end
Newer Older