-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
150 lines (122 loc) · 3.64 KB
/
index.html
File metadata and controls
150 lines (122 loc) · 3.64 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
<html>
<head>
<style>
#myCanvas{
margin-left: 400px;
}
</style>
</head>
<body onload="animate();">
<div id="wrapper">
<canvas id="myCanvas" width="600" height="400" ></canvas>
</div>
<script type="text/javascript" src="js.js"></script>
<script type="text/javascript" src="elemento.js"></script>
<script type="text/javascript" src="scene.js"></script>
<script type="text/javascript" src="player.js"></script>
<script type="text/javascript" src="stick.js"></script>
<script>
window.requestAnimFrame = (function(callback)
{
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
function(callback)
{
window.setTimeout(callback, 1000 / 60);
};
})();
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
// Inicializa objetos da tela
var scene = new Scene(context);
var player = new Player();
var scoreSticks = new Array();
var deathSticks = new Array();
var nDeathSticks = 1;
//ate 150 sem lag
var nSticks = 15;
var intervalDeath = 2000;
scene.backgroundImg.onload = function ()
{
scene.setImg(context);
}
//score sticks
for (var i = 0; i < nSticks; i++)
{
var stick = new Stick();
stick.randomPos();
scoreSticks[i] = stick;
}
//death sticks
for (var i = 0; i < nDeathSticks; i++)
{
var deathStick = new Stick();
deathStick.setImg("images/death stick.png");
deathStick.randomPos();
deathStick.intervalChangePosition(deathStick, intervalDeath);
deathSticks[i] = deathStick;
}
// - - FIM - - Inicializa objetos da tela
// Eventos Tela
var keys = {};
document.onkeydown = function(e)
{
keys[e.which] = true;
}
document.onkeyup = function (e)
{
delete keys[e.which];
}
// - - FIM - - Eventos Tela
//fps
var filterStrength = 20;
var frameTime = 0, lastLoop = new Date, thisLoop;
function animate()
{
context.clearRect(0, 0, canvas.width, canvas.height);
// move player, e checa colições
player.move(keys);
for (i = 0; i < nSticks; i++)
{
scene.colisionScore(player,scoreSticks[i]);
}
for (i = 0; i < nDeathSticks; i++)
{
if(scene.colisionDeath(player,deathSticks[i]))
{
//fim de jogo
document.onkeydown = "";
document.onkeyup = "";
keys = "";
delete keys;
}
}
// - - FIM - - move player, e checa colições
scene.drawScene(context, canvas);
//draw sticks
for (i = 0; i < nSticks; i++)
{
scoreSticks[i].drawElement(context);
}
//draw death sticks
for (i = 0; i < nDeathSticks; i++)
{
deathSticks[i].drawElement(context);
}
player.drawElement(context);
player.drawScore(context, canvas);
var thisFrameTime = (thisLoop=new Date) - lastLoop;
frameTime+= (thisFrameTime - frameTime) / filterStrength;
lastLoop = thisLoop;
requestAnimFrame(function()
{
animate();
});
}
//animate();
setInterval(function(){
var fdpsLog = (1000/frameTime).toFixed(1) + " fps";
console.log(fdpsLog);
},500);
</script>
</body>
</html>