-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLetters.lua
350 lines (252 loc) · 7.68 KB
/
Letters.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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
local composer = require( "composer" )
local Letters = {word = "test"}
local frames = {}
local width, height = 96
local str = "agmsybhntzciou,djpv.ekqw?flrx!"
local alphabets = "abcdefghijklmnopqrstuvwxyz"
local player = require("player")
local playerScore = player:new({xPos = 0, yPos=0})
local physics = require("physics")
local garbageCleaner = {}
local countObects = 1
physics.start( )
physics.setGravity( 0, 2 )
local highScore = require("highscore")
playerScore:displayScore(0)
local letterMap = {}
local centerX, centerY = display.contentCenterX, display.contentCenterY
local correctSelect = audio.loadStream( "assets/Correct.wav" )
local wrongSelect = audio.loadStream( "assets/wrong.wav" )
-- Go nextLevel --
function gotoLevel(lev)
display.setDefault( "background", 0, 0, 0 )
local goToSceneOptions = {
effect = "crossFade",
time = 2400,
}
for k,v in ipairs(garbageCleaner) do
v:removeSelf( )
end
garbageCleaner = {}
countObects = 1
composer.gotoScene( "game", goToSceneOptions)
end
function completed()
local nextButton = display.newImage("assets/next.png", centerX, centerY + 65)
nextButton:scale(0.35,0.35)
nextButton:addEventListener( "tap", function()
nextButton:removeSelf()
gotoLevel(2)
end )
end
function append(object, element)
object[countObects] = element
countObects = countObects + 1
return object
end
-- Go nto game over --
function gotoGameOver()
display.setDefault( "background", 0, 0, 0 )
local goToSceneOptions = {
effect = "crossFade",
time = 1400,
}
composer.gotoScene( "gameOver", goToSceneOptions)
end
function initFrames( )
local x, y, count = -96, -96, 0
for i=1,6 do
x=x+96
y = -96
for j=1,5 do
count = count +1
local letter = string.sub(str, count, count)
frames[letter]={}
y = y+96
frames[letter] = {x=x, y = y}
end
end
end
local spriteFrames = {}
initFrames( )
for i = 1, string.len(alphabets) do
local letter = string.sub(alphabets, i, i)
letterMap[letter] = i
spriteFrames[i] = {x=frames[letter].x, y = frames[letter].y, width = 96, height = 96}
--print(string.sub(alphabets, i, i), frames[string.sub(alphabets, i, i)])
end
local myFrame = {{x=frames['d'].x, y = frames['d'].y, width = 96, height = 96},
{x=96, y = 0, width = 96, height = 96}}
print(frames['a'].x, frames['b'].y)
local lettersOpt =
{
frames = spriteFrames
};
local lettersSheet = graphics.newImageSheet( "assets/Letters.jpg", lettersOpt );
function shuffle(word)
local shufledWord = ""
local wordLength = string.len(word)
if(wordLength%2 ~= 0)then
word = word..'z'
end
for i = 1, string.len(word)/2 do
letterEven = string.sub(word, i, i)
letterPair = string.sub(word, wordLength-i+1, wordLength-i+1)
shufledWord = shufledWord..letterPair..letterEven
end
return shufledWord
end
function letterInWord(word, letter)
for i = 1, string.len(word) do
if(letter == string.sub(word, i, i)) then
return true
end
end
return false
end
function Letters:getRandomLetters(word, numOfLetters)
local pos = math.random( 1,26 )
local count = 0
while count < numOfLetters do
rand = math.random( 1,26 )
if(not letterInWord(word, string.sub(alphabets, rand, rand)) ) then
word = word..string.sub(alphabets, rand, rand)
count = count + 1
end
end
print("word", word)
return shuffle(word)
end
function Letters:new( o )
o = o or {}
setmetatable( o, self )
self.__index = self
return o
end
function Letters:getLettersProsition_2( word )
LettersXposYpos = {}
local xPos, yPos = 30, (3*centerY/2) - 100
for i = 1, string.len(word) do
local ele = string.sub(word, i, i)
if(i % 6 ~= 0) then
xPos = xPos+(64)
else
xPos = 30+64
yPos = yPos + 64
end
LettersXposYpos[i] = display.newRect( xPos - 60, yPos - 48, 48, 48 )
end
return LettersXposYpos
end
function Letters:getLettersProsition_1( word )
local count = 0
local objects, checkLetters = {}, {}
local xPos, yPos = 30, (3*centerY/2)+15
local lettersPosition = {}
for i = 1, string.len(word) do
local ele = string.sub(word, i, i)
if(lettersPosition[ele] == nil) then
lettersPosition[ele]=i
else
lettersPosition[ele] = lettersPosition[ele]..i
end
end
word = self:getRandomLetters(word, 4)
for i = 1, string.len(word) do
local ele = string.sub(word, i, i)
if(checkLetters[ele] == nil) then
count = count + 1
local temp= self:displayLetter(ele, xPos,yPos, lettersPosition[ele] )
if(count % 5 ~= 0) then
xPos = xPos+(64)
else
xPos = 30
yPos = yPos + 64
end
objects[count] = temp
checkLetters[ele] = ele
end
end
self.mixedWord = word
return objects
end
function Letters:tap( event )
local hasMultiLetters = false
if(event.target.lettersPosition == nil) then
print("Klicked",event.target.letter)
if(event.target.clicked ~=nil) then
return
end
event.target.clicked = true
--event.target:setFillColor( 1,0,0 )
event.target:removeSelf( )
audio.play( wrongSelect, { channel=1 } )
self.myHanger:onWrongSelect()
if(self.myHanger.wrongSelectionCount >= 5) then
local buttomBar = display.newRect( centerX, 900, display.contentWidth, 20 )
local hangedMan = display.newImage("assets/hanged.jpg", centerX, centerY)
gameOver = display.newImage("assets/gameOver.png", centerX, 0)
physics.addBody( gameOver, "dynamic",{ density=1, friction=0.3, bounce=0.95} )
physics.addBody( buttomBar, "static" )
hangedMan.xScale = 2
hangedMan.yScale = 2
gameOver.xScale = 0.5
gameOver.yScale = 0.5
hangedMan.xScale = 2
hangedMan.yScale = 2
end
return
end
hasMultiLetters = false
for i = 1, string.len(event.target.lettersPosition) do
local pos = tonumber(string.sub(event.target.lettersPosition, i, i))
playerScore.tries = playerScore.tries + 1
local placeHolder = self.letterXposYpos[pos]
local x, y = placeHolder.x, placeHolder.y
local shapeTrasn = event.target
garbageCleaner = append(garbageCleaner, shapeTrasn)
if(hasMultiLetters) then
shapeTrasn = self:displayLetter( event.target.letter , event.target.x, event.target.y, nil ).shape
end
transition.to( shapeTrasn, { time=1500, x= x, y= y, onComplete= function () placeHolder:removeSelf( )
-- body
end } )
hasMultiLetters = true
garbageCleaner = append(garbageCleaner, shapeTrasn)
end
if(playerScore.tries == string.len(self.ChosedWord) and self.myHanger.wrongSelectionCount <= 4) then
playerScore.tries = 0
self.myHanger.wrongSelectionCount = 1
playerScore:displayScore(1)
if(player.score > tonumber(player.myHighScore:getHigScore())) then
player.myHighScore:setHigScore(player.score)
end
self.myHanger:reset()
self.myHanger.wrongSelectionCount = 1
timer.performWithDelay( 1500, function() completed() end )
--completed()
--gotoLevel(2)
end
audio.play( correctSelect, { channel=1 } )
--TODO Add logic if USER misses a letter
end
function Letters:displayWord( word )
self.ChosedWord = word
self.letterXposYpos = self:getLettersProsition_2( word )
return self:getLettersProsition_1( word )
end
function Letters:displayLetter( letter , xPos, yPos, lettersPosition )
local letterObj ={}
letterObj.shape = display.newImage( lettersSheet, letterMap[letter] );
letterObj.shape.x = xPos
letterObj.shape.y = yPos
letterObj.shape.xScale = (0.5/display.contentWidth) * 320
letterObj.shape.yScale = (0.5/display.contentHeight) * 480
letterObj.shape.letter = letter
letterObj.shape.lettersPosition = lettersPosition
return letterObj
end
-- function Letters:show( )
-- display.newSprite( [parent,] imageSheet, sequenceData )
-- end
return Letters