最後活躍 6 hours ago

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

main.lua 原始檔案 Playground
1local size = 100
2local offsetH, offsetW = love.graphics.getHeight() / 2 - 250, love.graphics.getWidth() / 2 - 250
3
4local 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
12function flip(x, y)
13 if grid[y] ~= nil and grid[y][x] ~= nil then
14 grid[y][x] = not grid[y][x]
15 end
16end
17
18function 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)
24end
25
26function 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
31end
32
33function 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
43end
44
45function 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)
49end