-
Notifications
You must be signed in to change notification settings - Fork 81
/
snakegame.js
231 lines (189 loc) · 4.36 KB
/
snakegame.js
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
//Paulo Henrique SN
//github/paulohenriquesn
var mov=0;
var time=20;
var timeez=0;
var tempao = true;
var textotempo = document.getElementById("time")
var canvas = document.getElementById('game');
var context = canvas.getContext('2d');
var scorex = document.getElementById('score')
var dif = document.getElementById('dificuldade')
var grid = 16;
var score=0;
var count = 0;
var record=0;
var fps = 4;
var fpsz=0;
var snake = {
x: 160,
y: 160,
dx: grid,
dy: 0,
cells: [],
maxCells: 4
};
var apple = {
x: 320,
y: 320
};
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
function loop() {
requestAnimationFrame(loop);
if (++count < fps) {
return;
}
timeez++;
if(timeez>10){
time--
textotempo.innerHTML = `Tempo ${time}`
timeez=0
}
if(time<=0){
snake.x = 160;
snake.y = 160;
snake.cells = [];
snake.maxCells = 4;
snake.dx = grid;
snake.dy = 0;
apple.x = getRandomInt(0, 25) * grid;
apple.y = getRandomInt(0, 25) * grid;
if(score>record){
record=score;
}
score=0
fps=4
fpsz=0
scorex.innerHTML = "Score "+ score + " Recorde. " + record
dif.innerHTML="Dificuldade Facil"
time=20
}
count = 0;
context.clearRect(0,0,canvas.width,canvas.height);
snake.x += snake.dx;
snake.y += snake.dy;
if (snake.x < 0) {
snake.x = canvas.width - grid;
}
else if (snake.x >= canvas.width) {
snake.x = 0;
}
if (snake.y < 0) {
snake.y = canvas.height - grid;
}
else if (snake.y >= canvas.height) {
snake.y = 0;
}
snake.cells.unshift({x: snake.x, y: snake.y});
if (snake.cells.length > snake.maxCells) {
snake.cells.pop();
}
context.fillStyle = 'red';
//context.fillRect(apple.x, apple.y, grid-1, grid-1);
context.fillStyle = 'white';
context.shadowColor='black'
context.shadowBlur=6;
context.shadowOffsetX=6
context.shadowOffsetY=6
context.beginPath()
context.fillStyle='purple'
context.arc(apple.x, apple.y, grid-1, 0, 2 * Math.PI);
context.stroke()
context.fill(
)
snake.cells.forEach(function(cell, index) {
// context.fillRect(cell.x, cell.y, grid-1, grid-1);
context.beginPath()
context.fillStyle='cyan'
context.arc(cell.x, cell.y, grid-1, 0, 2* Math.PI);
context.stroke()
context.fill()
if (cell.x === apple.x && cell.y === apple.y) {
snake.maxCells++;
if(fpsz<5){
fpsz++
}else{
fps--
if(fps== 3){
dif.innerHTML = "Dificuldade Medio"
}
if(fps<3){
dif.innerHTML = "Dificuldade Difícil"
}
fpsz=0
}
time=20
score++
scorex.innerHTML = "Score "+ score + " Recorde. " + record
apple.x = getRandomInt(0, 25) * grid;
apple.y = getRandomInt(0, 25) * grid;
}
for (var i = index + 1; i < snake.cells.length; i++) {
if (cell.x === snake.cells[i].x && cell.y === snake.cells[i].y) {
snake.x = 160;
snake.y = 160;
snake.cells = [];
snake.maxCells = 4;
snake.dx = grid;
snake.dy = 0;
apple.x = getRandomInt(0, 25) * grid;
apple.y = getRandomInt(0, 25) * grid;
if(score>record){
record=score;
}
score=0
fps=4
fpsz=0
scorex.innerHTML = "Score "+ score + " Recorde. " + record
dif.innerHTML="Dificuldade Facil"
}
}
})
}
document.addEventListener('keydown', function(e) {
// left arrow key
if (e.which === 37 && snake.dx === 0) {
snake.dx = -grid;
snake.dy = 0;
}
// up arrow key
else if (e.which === 38 && snake.dy === 0) {
snake.dy = -grid;
snake.dx = 0;
}
// right arrow key
else if (e.which === 39 && snake.dx === 0) {
snake.dx = grid;
snake.dy = 0;
}
// down arrow key
else if (e.which === 40 && snake.dy === 0) {
snake.dy = grid;
snake.dx = 0;
}
});
//android and ios
function Anda(){
if(mov == 2){
snake.dx = -grid;
snake.dy = 0;
}
if (mov == 0){
snake.dx = grid;
snake.dy = 0; }
if(mov == 3){
snake.dy = -grid;
snake.dx = 0; }
if(mov == 1){
snake.dy = grid;
snake.dx = 0;
}
if(mov<3){
mov++
}else{
mov = 0;
}
}
requestAnimationFrame(loop);