-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_game.lua
More file actions
252 lines (212 loc) · 8.53 KB
/
Copy pathmain_game.lua
File metadata and controls
252 lines (212 loc) · 8.53 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
local composer = require("composer")
local physics = require("physics")
physics.start()
local scene = composer.newScene()
local games = { "draw_box", "getcoin", "tug of war" }
local riverbank
local player
function scene:create(event)
-- local scene_group = self.view
-- background
local river = display.newImage("images/river.png", display.contentCenterX, display.contentCenterY)
riverbank = display.newImage("images/riverbank.png", display.contentCenterX, display.contentCenterY)
local screen_ratio = display.contentHeight / display.contentWidth
riverbank.width = display.contentWidth - 100 -- weird, but works
riverbank.height = display.contentHeight
local pic_ratio = river.height / river.width
if pic_ratio > screen_ratio then
river.width = display.contentWidth
river.height = river.width * pic_ratio
else
river.height = display.contentHeight
river.width = river.height / pic_ratio
end
-- scene_group:insert(river)
-- scene_group:insert(riverbank)
-- local game_over_pic = display.newImage("images/game_over.png", display.contentCenterX,
-- display.contentCenterY)
-- pic_ratio = game_over_pic.height / game_over_pic.width
-- if pic_ratio > screen_ratio then
-- game_over_pic.width = display.contentWidth
-- game_over_pic.height = game_over_pic.width * pic_ratio
-- else
-- game_over_pic.height = display.contentHeight
-- game_over_pic.width = game_over_pic.height / pic_ratio
-- end
-- game_over_pic.alpha = 0
--player
player = display.newImageRect("images/player.png", 50, 50)
player.x = display.contentCenterX
player.y = display.contentCenterY
physics.addBody(player, "dynamic")
player.gravityScale = 0
-- scene_group:insert(player)
-- scene_group:insert(move)
-- scene_group:insert(on_touch)
-- score
local scoredisplay = display.newText("Score : " .. Score, display.contentCenterX, 25, system.nativeFont, 18)
timer.performWithDelay(1000, function()
Score = Score + 1
scoredisplay.text = "Score : " .. Score
if Score % 100 == 0 then
new_obj("images/swirl.png", 200, 200, true)
end
end, 0)
-- crocodile
local scale = 0.05
local pos1, pos2 = display.contentCenterX - 25 * 5 + 7.5, display.contentCenterX + 25 * 5 - 7.5
timer.performWithDelay(250, function()
local crocodile = display.newImage("images/crocodile_l.png", pos1, display.contentHeight - 50)
crocodile:scale(scale, scale)
timer.performWithDelay(500, function()
if crocodile.rotation == 0 then
crocodile.rotation = -20
else
crocodile.rotation = 0
end
end, 0)
pos1 = pos1 + 25
end, 5)
timer.performWithDelay(250, function()
local crocodile = display.newImage("images/crocodile_r.png", pos2, display.contentHeight - 50)
crocodile:scale(scale, scale)
timer.performWithDelay(500, function()
if crocodile.rotation == 0 then
crocodile.rotation = -20
else
crocodile.rotation = 0
end
end, 0)
pos2 = pos2 - 25
end, 5)
local lose_area = display.newRect(display.contentCenterX, display.contentHeight, display.contentWidth, 50)
lose_area.alpha = 0
physics.addBody(lose_area, "static", { isSensor = true })
-- events
player:addEventListener("collision", function(event)
if event.phase == "began" and not over then
if event.other == lose_area then
scene:addEventListener("show", scene)
-- composer.gotoScene("game_over", { effect = "fade", time = 500, params = { score = Score } })
-- game_over_pic.alpha = 0.5
-- player:removeSelf()
physics.removeBody(player)
player.x = display.contentCenterX
player.y = display.contentCenterY
player.alpha = 0
over = true
display.newText("Game Over", display.contentCenterX, display.contentCenterY, system.nativeFont, 40)
display.newText("your score : " .. Score, display.contentCenterX, display.contentCenterY + 50,
system.nativeFont, 20)
end
end
end)
timer.performWithDelay(10, function() if player.y > display.contentCenterY then player.y = player.y - 1 end end,
0)
end
function scene:show(event)
-- local scene_group = self.view
local move_time = 1 -- optional
local touch_x
local on_touch
local move = function()
if touch_x == player.x then return end
local x
if touch_x > player.x then
x = math.min(touch_x, player.x + 10)
else
x = math.max(touch_x, player.x - 10)
end
transition.to(player, { x = x, time = math.abs(x - player.x) * move_time, onComplete = on_touch })
end
on_touch = function(event)
if event.phase == "began" or event.phase == "moved" or event.phase == "stationary" then -- weird issue: using only 'else' will make the player movement not continuous
if event.x < 100 then
touch_x = 100
elseif event.x > display.contentWidth - 100 then
touch_x = display.contentWidth - 100
else
touch_x = event.x
end
elseif event.phase == "ended" then
transition.cancel(player)
return
end
move()
end
Runtime:addEventListener("touch", on_touch)
-- new object
local new_obj = function(pic, width, height, body)
local obj = display.newImageRect(pic, width, height)
if not over then
-- game_over_pic:toFront()
-- else
riverbank:toFront()
player:toFront()
end
if pic == "images/swirl.png" then
obj.x = display.contentCenterX
else
obj.x = math.random(75, display.contentWidth - 75)
end
obj.y = -10
if pic == "images/swirl.png" then
physics.addBody(obj, "dynamic", { isSensor = true })
obj.gravityScale = 0
obj:addEventListener("collision", function(event)
if event.other == player then
composer.gotoScene(games[math.random(#games)],
{ effect = "fade", time = 500, params = { score = Score } })
end
end)
transition.to(obj,
{
y = display.contentHeight,
rotation = obj.rotation + 360,
time = 5000,
iterations = 0,
deltaAngle = 45,
onComplete = function() obj:removeSelf() end
})
else
if body then
physics.addBody(obj, "dynamic")
obj.gravityScale = 0
end
transition.to(obj,
{
y = display.contentHeight,
time = math.random(1000, 3000) / (Score / 100 + 0.5),
onComplete = function() obj:removeSelf() end
})
end
end
-- waves
timer_generate_waves = timer.performWithDelay(math.random(1000, 1500) / (Score / 100 + 0.5),
function() new_obj("images/wave1.png", math.random(50, 100), math.random(50, 100)) end, 0)
timer.performWithDelay(math.random(500, 1000) / (Score / 100 + 0.5),
function() new_obj("images/wave2.png", math.random(50, 100), math.random(50, 100)) end, 0)
-- elements
local background_elements = { "leaf1", "leaf2", "leaf3", "starfish", "conch", "grass" }
local obstacle_elements = { "stone1", "stone2", "stone3", "stone4", "stone5", "driftwood1", "driftwood2" }
-- background only
timer.performWithDelay(math.random(1000, 3000) / (Score / 100 + 0.5),
function()
new_obj("images/" .. background_elements[math.random(#background_elements)] .. ".png",
math.random(50, 75),
math.random(50, 75), false)
end, 0)
-- stone
timer.performWithDelay(math.random(1000, 1500) / (Score / 100 + 0.5),
function()
new_obj("images/" .. obstacle_elements[math.random(#obstacle_elements)] .. ".png", math.random(50, 100),
math.random(50, 100), true)
end, 0)
end
function scene:hide()
timer.cancel(timer_generate_waves)
end
-- listeners
scene:addEventListener("create", scene)
scene:addEventListener("show", scene)
return scene