-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.lua
270 lines (209 loc) · 5.56 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
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
display.setStatusBar( display.HiddenStatusBar )
local composer = require( "composer" )
local scene = composer.newScene()
local mydata = require "mydata"
local physics = require "physics"
physics.start()
physics.setGravity( 0, 7 )
local player
local blocks = {}
local addBlockTimer
local newScene = false
local touched = false
local direction = 1
local momentum = 0.0
local scoreText
function scene:create( event )
local sceneGroup = self.view
local color = 'red'
for i = 1, 10 do
local block = display.newRect( -1000, -1000, 60, 2 )
block.index = i
block.name = 'block'
block.isAlive = false
block.isVisible = false
physics.addBody( block, 'static' )
blocks[i] = block
block.color = color
color = switchColor(block)
sceneGroup:insert( block )
end
local ceiling = display.newRect(
display.contentWidth/2, -2,
display.contentWidth, 2
)
physics.addBody( ceiling, 'static' )
sceneGroup:insert( ceiling )
local leftWall = display.newRect(
0, display.contentHeight/2,
2, display.contentHeight
)
physics.addBody( leftWall, 'static' )
sceneGroup:insert( leftWall )
local rightWall = display.newRect(
display.contentWidth, display.contentHeight/2,
2, display.contentHeight
)
physics.addBody( rightWall, 'static' )
sceneGroup:insert( rightWall )
player = display.newRect( 0, 0, 20, 20 )
player.name = 'player'
player.x = display.contentWidth / 2
player:setFillColor( 1, 0, 0 )
player.color = 'red'
physics.addBody( player, 'dynamic', {friction=1.0})
player.collision = onCollision
player:addEventListener( 'collision', player )
player.isFixedRotation = true
sceneGroup:insert( player )
-- create score
mydata.score = 0
scoreText = display.newText( {text='Score: ' .. mydata.score, x=display.contentWidth-60, y=20, width=100, height=20, align="right"} )
sceneGroup:insert( scoreText )
end
-- "scene:show()"
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Called when the scene is still off screen (but is about to come on screen).
elseif ( phase == "did" ) then
Runtime:addEventListener( 'enterFrame', update )
Runtime:addEventListener( 'touch', screenTouched )
addBlockTimer = timer.performWithDelay( 800, addBlock, 0 )
end
end
-- "scene:hide()"
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
Runtime:removeEventListener( 'enterFrame', update )
Runtime:removeEventListener( 'touch', screenTouched )
Runtime:removeEventListener( 'collision', onCollision)
timer.cancel( addBlockTimer )
elseif ( phase == "did" ) then
end
end
-- "scene:destroy()"
function scene:destroy( event )
local sceneGroup = self.view
-- Called prior to the removal of scene's view ("sceneGroup").
-- Insert code here to clean up the scene.
-- Example: remove display objects, save state, etc.
end
-- function sign(num)
-- if num >= 0 then return 1 else return -1 end
-- end
-- function distance(x1, y1, x2, y2)
-- return math.sqrt( x1*x2 + y1*y2 )
-- end
function switchColor(obj)
if obj == nil then
print(object.name .. " is nil!!")
end
-- return 0
if obj.color == 'red' then
obj:setFillColor(0, 1, 0)
obj.color = 'green'
elseif obj.color == 'green' then
obj:setFillColor(0, 0, 1)
obj.color = 'blue'
elseif obj.color =='blue' then
obj:setFillColor(1, 0, 0)
obj.color = 'red'
end
return obj.color
end
function removeBlock(block)
block.isAlive = false
block.x = -1000
block.y = -1000
end
function addBlock()
for k, block in pairs(blocks) do
if block.isAlive ~= true then
block.contentWidth = 60
block.contentHeight = 2
block.isAlive = true
block.isVisible = true
block.x = math.random(
block.contentWidth/2 + 20,
display.contentWidth - block.contentWidth/2 - 20
)
block.y = display.contentHeight
break
end
end
end
function onCollision(self, e)
if e.phase == 'began' then
if self.color == e.other.color then
local block
if self.name == 'block' then
block = self
elseif e.other.name == 'block' then
block = e.other
end
timer.performWithDelay(1, function()
switchColor(player)
removeBlock(block)
mydata.score = mydata.score + 1
scoreText.text = 'Score: ' .. mydata.score
end )
elseif self.name == 'block' or e.other.name == 'block' then
gameOver()
end
end
end
function gameOver()
if not newScene then
newScene = true
mydata.setBestScore( )
composer.gotoScene( 'retry', {effect='fade', time=200} )
end
end
function screenTouched(e)
if e.phase == 'began' then
touched = true
elseif e.phase == 'ended' then
touched = false
end
if e.x < display.contentWidth/2 then
direction = -1
else
direction = 1
end
end
function update()
for k, block in pairs(blocks) do
if block.isAlive then
block.y = block.y - 3
if block.y < 0 then
removeBlock(block)
end
end
end
if player.y > display.contentHeight then
gameOver()
end
if momentum > 1 then
momentum = 1
elseif momentum < 0 then
momentum = 0
end
if touched then
momentum = momentum + 0.1
player:setLinearVelocity(direction * 120, -100)
else
momentum = momentum - 0.1
end
end
-- -------------------------------------------------------------------------------
-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -------------------------------------------------------------------------------
return scene