-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
231 lines (154 loc) · 4.51 KB
/
main.lua
File metadata and controls
231 lines (154 loc) · 4.51 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
-- Austin Ruggles 371
-- This is my own work ;)
local numOfTilesX = 1000 -- num of x tiles
local numOfTilesY = 1000 -- num of y tiles
-- total num of tiles
--
-- this should be an even num to avoid parity
-- errors in random color match.
--
local numOfTiles = numOfTilesX * numOfTilesY
--size of the title bar
local titleBar = 45
--Breakdown the entire screen to find the size of each tile
local xx = (display.contentWidth)/numOfTilesX
local yy = (display.contentHeight-titleBar)/numOfTilesY
-- setup a score system
--
-- Simple scoring system.
-- --Added pairs left amount to titlebar
--
local score = 0
local pairsL = numOfTiles/2
local scoreText = display.newText( "score:"..score, display.contentWidth/3, titleBar/2, Helvetica, 40 )
local pairsText = display.newText( "pairs:"..pairsL, display.contentWidth-display.contentWidth/3, titleBar/2, Helvetica, 40 )
local function updateScore( amount )
score = amount + score
scoreText.text = string.format( "score:%d", score )
end
local function updatePairs( )
pairsL = pairsL - 1
pairsText.text = string.format( "pairs:%d", pairsL )
end
-- setup random color gen
--
--
--
math.randomseed( os.time() ) -- seed numbers
-- assuming we want "pairs" of colors
--
-- ToDo: fix ..
-- if numOfTiles/2 is odd
-- we might end up with
-- odd number of paired colors.
--
local numOfColors = math.ceil( numOfTiles/2 )
-- return an array of evenly distributed floats
--
-- these will be used to create the colors
-- one array per "RGB"
-- the index will match the triplet.
--
local function colorArray( )
local colorIndex = {}
local tmpArray = {}
for i=1, numOfColors do
colorIndex[i] = i
end
local cnt = 1
while table.getn( colorIndex ) > 0 do
idx = math.random(1, table.getn( colorIndex ))
tmpArray[cnt] = colorIndex[idx]/numOfColors
table.remove(colorIndex, idx)
cnt = cnt + 1
end
return tmpArray
end
-- initialize the RGB triplet
local r = colorArray()
local g = colorArray()
local b = colorArray()
-- create a table of colors,
-- with the number each can be used.
local colors = {}
for i=1, numOfColors do
colors[i] = { r[i], g[i], b[i], 2 }
end
--
--
--
--
-- simple transitions to add some pizazz!!!
local function bounceDown( target )
transition.to( target, {time = 150, xScale = .8, yScale = .8})
end
local function bounceUp( target )
transition.to( target, {time = 200, xScale = 1, yScale = 1})
end
local function bounceOut( target )
--after this transition remove object
transition.to( target, {time = 800, xScale = 6, yScale = 6, onComplete=function(obj) obj:removeSelf(); obj = nil end})
end
local function fadeOut( target )
--start bounce out
transition.fadeOut( target, {time = 500, transition=easing.outCubic, onStart=bounceOut} )
end
local check = 1 -- check to see if we just matched and restart prevTarget
local prevTarget -- keep the last known target
local function mark( event )
if check == 1 then
-- prevTarget dose not exist so set a new one.
check = 0
prevTarget = event.target
end
if not event.target.marked then
-- mark the current target
event.target.marked = true
bounceDown( event.target )
else
-- if marked then unmark it.
event.target.marked = false
bounceUp( event.target )
check = 1
end
if event.target.marked and (prevTarget ~= event.target) then
if event.target.color == prevTarget.color then
-- if colors match than remove them
fadeOut(event.target)
fadeOut(prevTarget)
--print(" match ")
updateScore( 5 )
updatePairs( )
check = 1
else
-- colors don't match unmark prev, mark new.
prevTarget.marked = false
bounceUp(prevTarget)
bounceDown(event.target)
updateScore( -5 )
--print(" mis-match ")
end
end
prevTarget = event.target
end
for i=1,numOfTilesX do -- rows
for j=1,numOfTilesY do -- columns
-- create new tiles
local mb = display.newRect( xx*i-xx/2, yy*j-yy/2+titleBar, xx, yy )
-- find random color and set the tile color
idx = math.random( 1, table.getn( colors ) )
mb:setFillColor( colors[idx][1], colors[idx][2], colors[idx][3] )
-- create unique color identifier as string.
mb.color = colors[idx][1]..colors[idx][2]..colors[idx][3]
mb.marked = false --set the marked variable to false as default
if colors[idx][4] > 1 then
-- if color selected remove 1 color pair
colors[idx][4] = colors[idx][4] - 1
else
-- when both color pairs selected remove from the list of available colors
table.remove(colors, idx)
end
-- event listener for tap
mb:addEventListener( "tap", mark )
end
end