This repository has been archived by the owner on Dec 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.lua
91 lines (82 loc) · 1.72 KB
/
game.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
local clearScreen = true
local autoPlay = false
local height = 240
local width = 400
local threshold = 0.5
local function checkCellNeighbours(x, y)
local left = (x - 1) % width
local right = (x + 1) % width
local up = (y - 1) % height
local down = (y + 1) % height
local aliveAmount = world[left + up * width] +
world[x + up * width] +
world[right + up * width] +
world[left + y * width] +
world[right + y * width] +
world[left + down * width] +
world[x + down * width] +
world[right + down * width]
if aliveAmount == 2 then
return world[x + y * width]
end
if aliveAmount == 3 then
return 1
else
return 0
end
end
local function nextStep()
for x = 0, width - 1 do
for y = 0, height - 1 do
nextWorld[x + y * width] = checkCellNeighbours(x, y)
end
end
world, nextWorld = nextWorld, world
end
local function drawTable()
for x = 0, width - 1 do
for y = 0, height - 1 do
if (world[x + y * width] == 1) then
love.graphics.points(x, y)
end
end
end
end
local function newWorld()
world = {}
nextWorld = {}
for x = 0, width - 1 do
for y = 0, height - 1 do
world[x + y * width] = math.random(2) - 1 -- Рандом 0 или 1
end
end
end
function loadGame()
math.randomseed (os.time()) -- Сид для рандома, возможно у playdate sdk есть свой рандом
newWorld()
end
function updateGame()
if autoPlay then
nextStep()
end
end
function keypressed(key, code)
if key == "a" then
clearScreen = not clearScreen
end
if key == "p" then
autoPlay = not autoPlay
end
if key == "s" then
nextStep()
end
if key == "n" then
newWorld()
end
end
function drawGame()
if clearScreen then
love.graphics.clear(BLACK)
end
drawTable()
end