/
gameOfLife.ms
164 lines (142 loc) · 3.8 KB
/
gameOfLife.ms
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
clear
cellSize = 32
overlap = -1
display(5).mode = displayMode.tile
tileDisplay = display(5)
tileDisplay.tileSet = file.loadImage("/sys/pics/TileShapes.png")
tileDisplay.tileSetTileSize = 64
tileDisplay.cellSize = cellSize + overlap
tileDisplay.overlap = overlap
tileDisplay.extent = [960/cellSize, 640/cellSize]
tileDisplay.clear 1
cols = floor(960/cellSize)
rows = floor(640/cellSize)
board = []
for col in range(0, cols-1)
board.push []
for row in range(0, rows-1)
board[col].push 0
end for
end for
playButton = new Sprite
playButton.image = file.loadImage("/sys/pics/Block.png")
playButton.x = 960 - 48
playButton.y = 640 - 48
display(4).sprites.push playButton
playSprite = new Sprite
playSprite.image = file.loadImage("/sys/pics/shapes/Triangle.png")
playSprite.x = 960 - 48
playSprite.y = 640 - 48
playSprite.rotation = 270
playSprite.scale = 0.5
display(4).sprites.push playSprite
stopSprite = new Sprite
stopSprite.image = file.loadImage("/sys/pics/shapes/Square.png")
stopSprite.x = 960 - 48
stopSprite.y = 640 - 48
stopSprite.scale = 0.4
display(4).sprites.push stopSprite
playButton.handleMouseOver = function()
if not self.localBounds then
self.localBounds = new Bounds
self.localBounds.width = self.image.width
self.localBounds.height = self.image.height
end if
if self.contains(mouse) then
self.tint = color.rgba(255,255,255,255)
if not isPlaying then
playSprite.tint = color.green
stopSprite.tint = color.rgba(255,255,255,0)
else
playSprite.tint = color.rgba(255,255,255,0)
stopSprite.tint = color.blue
end if
else
self.tint = color.rgba(255,255,255,128)
if not isPlaying then
playSprite.tint = color.rgba(0,128,0,128)
stopSprite.tint = color.rgba(255,255,255,0)
else
playSprite.tint = color.rgba(255,255,255,0)
stopSprite.tint = color.rgba(0,0,255,128)
end if
end if
end function
renderBoard = function()
for col in range(0, cols-1)
for row in range(0, rows-1)
if board[col][row] then
tileDisplay.setCell col, row, 4
else
tileDisplay.setCell col, row, 0
end if
end for
end for
end function
changeCellAt = function(pos)
if pos[0] < 0 or pos[0] >= cols or pos[1] < 0 or pos[1] >= rows then return
board[pos[0]][pos[1]] = not board[pos[0]][pos[1]]
end function
isPlaying = false
changePlayState = function()
globals.isPlaying = not globals.isPlaying
end function
dirs = [[0,1],[1,1],[1,0],[1,-1],[0,-1],[-1,-1],[-1,0],[-1,1]]
numberOfNeighbours = function(col, row, board)
amount = 0
for dir in dirs
pos = [col+dir[0], row+dir[1]]
if pos[0] < 0 then pos[0] = cols-1
if pos[0] >= cols then pos[0] = 0
if pos[1] < 0 then pos[1] = rows-1
if pos[1] >= rows then pos[1] = 0
if not board[pos[0]][pos[1]] then continue
amount = amount + 1
end for
return amount
end function
nextBoard = function(oldBoard)
newBoard = []
for col in range(0,cols-1)
newBoard.push []
for row in range(0,rows-1)
value = oldBoard[col][row]
neighbours = numberOfNeighbours(col, row, oldBoard)
if neighbours < 2 then value = 0
if neighbours == 3 then value = 1
if neighbours > 3 then value = 0
newBoard[col].push value
end for
end for
return newBoard
end function
mouseWasDown = false
lastTimestamp = time
tickTime = 0
while true
currentTimestamp = time
delta = currentTimestamp - lastTimestamp
lastTimestamp = currentTimestamp
if key.pressed("q") then break
playButton.handleMouseOver
mouseIsDown = mouse.button
if mouseIsDown and not mouseWasDown then
pos = [floor(mouse.x/cellSize),floor(mouse.y/cellSize)]
if playButton.contains(mouse) then
changePlayState
else if not isPlaying then
changeCellAt pos
end if
end if
mouseWasDown = mouseIsDown
if isPlaying then
tickTime = tickTime + delta
if tickTime >= 0.25 then
board = nextBoard(board)
tickTime = tickTime - 0.25
end if
end if
renderBoard
yield
end while
clear