-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_loop.lua
237 lines (220 loc) · 3.89 KB
/
game_loop.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
--game loop
function _update60()
updateparticles()
doblink()
shakescr()
if gamestate=="start" then
update_start()
elseif gamestate=="game" then
update_game()
elseif gamestate=="gameoverwait" then
update_gameoverwait()
elseif gamestate=="gameover" then
update_gameover()
elseif gamestate=="levelover" then
update_levelover()
end
end
function _draw()
pal()
if fadeamnt > 0 then
fadepalette(fadeamnt)
end
if gamestate=="start" then
draw_start()
elseif gamestate=="game" or gamestate=="gameoverwait" then
draw_game()
elseif gamestate=="gameover" then
draw_gameover()
elseif gamestate=="levelover" then
draw_levelover()
end
end
function _init()
cls(0)
shkamnt = 0
blink_g = 5
blinkframe = 0
blinkspeed = 6
blinkindex = 1
blinkindex2 = 1
fadeamnt = 0
particles = {}
gamestate="start"
combo=1
levelnum=1
levels={}
levels[1]="x6b"
levels[2]="b9bb9bb9bb9bb9bb9b"
levels[3]="bxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbx"
levels[1]="bxhxixexp"
levels[1]="i999999bbbhhhppee"
-- levels[1]="e9999999"
levels[1]="p999"
-- levels[1]="b9bi9ii9ii9ibxxxxxb"
-- levels[1]="i99bi9999"
-- levels[1]="b9bb9bb9bxxxpxxx"
end
function serveball()
balls={ newball() }
sticky=false
balls[1].x=pad_x+flr(pad_w0/2)
balls[1].y=117
balls[1].dx=1
balls[1].dy=-1
balls[1].ang=1
balls[1].stuck=true
hasstuck=true
last_dir="right"
powerups={}
t_reduce = 0
t_expand = 0
t_speed = 0
t_mega = 0
offset=flr(pad_w0/2)
end
function update_game()
move_paddle()
move_pwp()
if btnp(5) then
releasestuck()
hasstuck=false
end
for ball in all(balls) do
update_ball(ball)
-- for i=#balls, 1, -1 do
-- update_ball(balls[i])
end
checkexplosions()
if levelfinished() then
_draw()
levelover()
end
timer()
update_bricks()
end
function draw_game()
if fadeamnt > 0 then
fadeamnt = max(fadeamnt - 0.05, 0)
end
cls()
rectfill(0,0,127,127,12)
rectfill(0,0,128,6,8)
if debug then
print("debug:"..debug, 1, 1, 7)
else
print("lives:"..lives,1,1,7)
print("score:"..points,40,1,7)
print("combo:"..combo.."x",85,1,7)
end
drawparticles()
draw_ball()
draw_paddle()
draw_brick()
drawpickups()
end
function draw_start()
cls()
print("breakout",48,50,7)
print("press ❎ to start",31,70,blink_g)
end
function update_start()
if not titletimer or titletimer < 0 then
if btnp(5) then
sfx(14)
blinkspeed = 1.5
titletimer = 40
end
else
titletimer -= 1
fadeamnt = (40 - titletimer) / 40
if titletimer <= 0 then
blinkspeed = 8
titletimer = -1
game_start()
end
end
end
function game_start()
gamestate="game"
pad_x=52
combo=1
lives=3
points=0
serveball()
levelnum=1
buildbricks(levels[levelnum])
end
function nextlevel()
particles = {}
gamestate="game"
levelnum+=1
if levelnum > #levels then
gamestate="start"
return
end
pad_x=52
pad_dx=0
serveball()
buildbricks(levels[levelnum])
combo=1
end
function draw_gameover()
rectfill(0,47,128,78,0)
print("game over",46,53,7)
print("press ❎ to play again",20,68,blink_g)
end
function draw_levelover()
rectfill(0,47,128,78,0)
print("level complete!",31,53,7)
print("press ❎ to continue",20,68,6)
end
function gameover()
gamestate="gameoverwait"
govertimer = 60
end
function levelover()
gamestate="levelover"
end
function update_gameover()
if not titletimer or titletimer < 0 then
if btnp(5) then
sfx(14)
blinkspeed = 1.5
titletimer = 40
end
else
titletimer -= 1
fadeamnt = (40 - titletimer) / 40
if titletimer <= 0 then
blinkspeed = 8
titletimer = -1
game_start()
end
end
end
function update_gameoverwait()
if shkamnt <= 0 then
blink_g = 5
blinkframe = 0
gamestate = "gameover"
end
-- govertimer -= 1
-- if govertimer <= 0 then
-- govertimer = -1
-- gamestate = "gameover"
-- end
end
function update_levelover()
if btnp(5) then
nextlevel()
end
end
function lose_life()
lives-=1
if lives<0 then
gameover()
else
serveball()
end
end