-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
362 lines (293 loc) · 7.54 KB
/
index.html
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
351
352
353
354
355
356
357
358
359
360
361
362
<!DOCTYPE html>
<html lang="pt-br">
<head>
<title>Jogo</title>
<meta charset="utf-8">
<script type="text/javascript" src="Sprite.js"></script>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
canvas{
position: absolute;
top: 0px;
bottom: 0px;
left: 0px;
right: 0px;
margin: auto;
}
</style>
</head>
<body>
<script type="text/javascript" defer>
//Variaveis do jogo
var canvas, ctx, ALTURA, LARGURA, frames = 0, jogo, record, pontosParaNovaFase = [5, 10, 15, 20], faseAtual = 0, VELOCIDADE = 6
const MAXPULOS = 3
const IMG = new Image()
const IMGHEART = new Image()
const IMGSTAR = new Image()
//Objetos
var labelNovaFase = {
texto: "",
opacidade: 0.0,
fadeIn: function(dt){
var fadeInId = setInterval(function(){
if(labelNovaFase.opacidade < 1.0){
labelNovaFase.opacidade += 0.01
}else{
clearInterval(fadeInId)
}
}, 10 * dt)
},
fadeOut: function(dt){
var fadeIOutId = setInterval(function(){
if(labelNovaFase.opacidade > 0.0){
labelNovaFase.opacidade -= 0.01
}else{
clearInterval(fadeIOutId)
}
}, 10 * dt)
}
}
var estados = {
jogar: 0,
jogando: 1,
perdeu: 2
}
var chao = {
y: 0,
x: 0,
altura: CHAO.altura,
atualiza: function(){
this.x -= VELOCIDADE
if(this.x <= -30){
this.x += 30
}
},
desenha: function(){
CHAO.desenha(this.x, this.y)
CHAO.desenha(this.x + CHAO.largura, this.y)
}
}
var bloco = {
x: 50,
y: 0,
altura: PERSONAGEM.altura,
largura: PERSONAGEM.largura,
gravidade: 1.5,
velocidade: 0,
forcaPulo: 20,
qtdePulos: 0,
score: 0,
rotacao: 0,
vidas: 3,
colidindo: false,
atualiza: function(){
this.velocidade += this.gravidade
this.y += this.velocidade
if((this.y + this.altura) >= chao.y && jogo != estados.perdeu){
this.y = (chao.y - this.altura)
this.qtdePulos = 0
this.velocidade = 0
}
this.rotacao += Math.PI / 180 * 10
},
reset: function(){
this.y = bloco.altura
this.velocidade = 0
this.gravidade = 1.5
if(this.score > record){
record = localStorage.record = this.score
}
this.score = 0
this.vidas = 3
this.colidindo = false
VELOCIDADE = 6
faseAtual = 0
},
pula: function(){
if(this.qtdePulos < MAXPULOS){
this.qtdePulos++
this.velocidade = -this.forcaPulo
}
},
desenha: function(){
ctx.save()
ctx.translate(this.x + this.largura / 2, this.y + this.altura / 2)
ctx.rotate(this.rotacao)
PERSONAGEM.desenha(-this.largura / 2, -this.altura / 2)
ctx.restore()
}
}
var obstaculos = {
_obs: [],
sprites: [REDOBS, PINKOBS, BLUEOBS, GREENOBS, YELLOWOBS],
scored: false,
tempoInsere: 0,
insere: function(){
this._obs.push({
x: LARGURA,
largura: 50,//30 + Math.floor(Math.random() * 21),
altura: 30 + Math.floor(Math.random() * 90),
sprite: this.sprites[Math.floor(Math.random() * this.sprites.length)]
})
this.tempoInsere += 30 + Math.floor(Math.random() * 20)
},
atualiza: function(){
if(this.tempoInsere == 0){
this.insere()
}else{
this.tempoInsere--
}
for(let i in this._obs){
this._obs[i].x -= VELOCIDADE
if(bloco.x < this._obs[i].x + this._obs[i].largura && bloco.x + bloco.largura >= this._obs[i].x && bloco.y + bloco.altura >= chao.y - this._obs[i].altura){
if(bloco.vidas > 0 && !bloco.colidindo){
bloco.vidas--
bloco.colidindo = true
if(bloco.vidas > 0)
this._obs.splice(i, 1)
setTimeout(() => bloco.colidindo = false, 500)
}
if(bloco.vidas == 0){
jogo = estados.perdeu
}
}else if(this._obs[i].x <= 0 && jogo == estados.jogando && !this._obs[i].scored){
bloco.score++
this._obs[i].scored = true
if(bloco.score == pontosParaNovaFase[faseAtual] && faseAtual < pontosParaNovaFase.length){
passarFase()
}
}else if(this._obs[i].x <= -this._obs[i].largura){
this._obs.splice(i, 1)
}
}
},
limpa: function(){
this._obs = []
},
desenha: function(){
for(let _ob of this._obs){
ctx.drawImage(IMG, _ob.sprite.x, _ob.sprite.y, _ob.sprite.largura, _ob.altura, _ob.x, (chao.y - _ob.altura), _ob.largura, _ob.altura)
}
}
}
function clique(event){
if(jogo == estados.jogando){
bloco.pula()
}else if(jogo == estados.perdeu){
obstaculos.limpa()
bloco.reset()
jogo = estados.jogar
faseAtual == 0
labelNovaFase.fadeOut(0)
}else{
bloco.reset()
jogo = estados.jogando
}
}
function passarFase(){
faseAtual++
VELOCIDADE++
bloco.vidas++
if(faseAtual == 4){
bloco.gravidade -= 0.5
}
labelNovaFase.texto = `Level: ${faseAtual}`
let tmp = 0.4
labelNovaFase.fadeIn(tmp)
setTimeout(function(){labelNovaFase.fadeOut(tmp)}, 800)
}
function main(){
//Tamanho da tela do jogo
ALTURA = window.innerHeight
LARGURA = window.innerWidth
if(LARGURA >= 500){
ALTURA = 600
LARGURA = 600
}
chao.y = ALTURA - chao.altura
bloco.reset()
//Canvas
canvas = window.document.createElement("canvas")
canvas.width = LARGURA
canvas.height = ALTURA
// canvas.style.border = "1px solid black"
ctx = canvas.getContext("2d")
window.document.body.appendChild(canvas)
//Evento de clique
window.document.addEventListener("mousedown", clique)
//Estado do jogo
jogo = estados.jogar
VELOCIDADE = 6
//Fases do jogo
pontosParaNovaFase = [5, 10, 15, 20]
faseAtual = 0
//Record do jogo
if(localStorage.record == undefined){
localStorage.record = 0
}
record = localStorage.record
//Imagens do jogo
IMG.src = "./Imagens/sheet.png"
IMGHEART.src = "./Imagens/heart.png"
IMGSTAR.src = "./Imagens/star.png"
//Loop
roda()
}
function roda(){
atualiza()
desenha()
window.requestAnimationFrame(roda, canvas)
}
function atualiza(){
frames++
if(jogo == estados.jogando || jogo == estados.perdeu){
obstaculos.atualiza()
}
chao.atualiza()
bloco.atualiza()
}
function desenha(){
ctx.clearRect(0, 0, LARGURA, ALTURA)
BACKGROUND.desenha(0, 0)
obstaculos.desenha()
chao.desenha()
bloco.desenha()
if(jogo == estados.jogando){
//Fase
ctx.fillStyle = `rgba(0, 0, 0, ${labelNovaFase.opacidade})`
ctx.font = "40px Arial"
ctx.fillText(labelNovaFase.texto, canvas.width / 2 - ctx.measureText(labelNovaFase.texto).width / 2, canvas.height / 3)
//Score
ctx.drawImage(IMGSTAR, 0, 0, 512, 512, 20, 20, 60, 60)
ctx.fillStyle = "white"
ctx.font = "25px Arial"
ctx.fillText(bloco.score, 43, 62)
//Vidas
ctx.drawImage(IMGHEART, 0, 0, 256, 256, LARGURA - 70, 20, 60, 60)
ctx.fillText(bloco.vidas, LARGURA - 47, 57)
}
if(jogo == estados.jogar){
JOGAR.desenha(LARGURA / 2 - JOGAR.largura / 2, ALTURA / 2 - JOGAR.altura / 2)
if(bloco.score > record){
localStorage.record = record = bloco.score
}
}else if(jogo == estados.perdeu){
PERDEU.desenha(LARGURA / 2 - PERDEU.largura / 2, ALTURA / 2 - PERDEU.altura / 2 - RECORD.altura / 2)
RECORD.desenha(LARGURA / 2 - RECORD.largura / 2, PERDEU.altura + RECORD.altura / 2)
ctx.fillStyle = "white"
ctx.font = "50px Arial"
ctx.fillText(bloco.score, 375, 390)
if(bloco.score > record){
NOVO.desenha(LARGURA / 2 - 180, ALTURA / 2 + RECORD.altura / 2 - 20)
}
ctx.fillText(record, 420, 470)
}
}
//Inicializando o jogo
window.addEventListener("load", main)
</script>
</body>
</html>