Repository navigation
Expand file tree
/
Copy pathgame.js
More file actions
166 lines (132 loc) · 3.76 KB
/
Copy pathgame.js
File metadata and controls
166 lines (132 loc) · 3.76 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
151
152
153
154
155
156
157
158
159
160
161
162
const canvas = document.getElementById("gameCanvas");
const controls = document.querySelectorAll('.btn_control')
const ctx = canvas.getContext("2d");
const scale = 10;
const rows = canvas.height / scale;
const columns = canvas.width / scale;
let snake;
let food;
function Snake() {
this.x = 0;
this.y = 0;
this.xSpeed = scale * 1;
this.ySpeed = 0;
this.total = 0;
this.tail = [];
this.draw = function() {
//define snake color
ctx.fillStyle = "#AA33AA";
//draw snake head
ctx.fillRect(this.x, this.y, scale, scale);
//draw snake tail
for (let i = 0; i < this.tail.length; i++) {
ctx.fillRect(this.tail[i].x, this.tail[i].y, scale, scale);
}
}
this.update = function() {
for (let i = 0; i < this.tail.length - 1; i++) {
this.tail[i] = this.tail[i + 1];
}
this.tail[this.total - 1] = { x: this.x, y: this.y };
this.x += this.xSpeed;
this.y += this.ySpeed;
if (this.x >= canvas.width) {
this.x = 0;
}
if (this.y >= canvas.height) {
this.y = 0;
}
if (this.x < 0) {
this.x = canvas.width - scale;
}
if (this.y < 0) {
this.y = canvas.height - scale;
}
}
this.changeDirection = function(direction) {
switch (direction) {
case 'Up':
if (this.ySpeed === 0) {
this.xSpeed = 0;
this.ySpeed = -scale * 1;
}
break;
case 'Down':
if (this.ySpeed === 0) {
this.xSpeed = 0;
this.ySpeed = scale * 1;
}
break;
case 'Left':
if (this.xSpeed === 0) {
this.xSpeed = -scale * 1;
this.ySpeed = 0;
}
break;
case 'Right':
if (this.xSpeed === 0) {
this.xSpeed = scale * 1;
this.ySpeed = 0;
}
break;
}
}
this.eat = function(food) {
if (this.x === food.x && this.y === food.y) {
this.total++;
return true;
}
return false;
}
this.checkCollision = function() {
for (let i = 0; i < this.tail.length; i++) {
if (this.x === this.tail[i].x && this.y === this.tail[i].y) {
this.total = 0;
this.tail = [];
}
}
}
}
function Food() {
this.x;
this.y;
this.pickLocation = function() {
this.x = Math.floor(Math.random() * rows) * scale;
this.y = Math.floor(Math.random() * columns) * scale;
}
this.draw = function() {
ctx.fillStyle = "#f44336";
ctx.fillRect(this.x, this.y, scale, scale);
}
}
function start() {
canvas.width = 340;
canvas.height = 340;
snake = new Snake();
food = new Food();
food.pickLocation();
window.setInterval(() => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
food.draw();
snake.update();
snake.draw();
if (snake.eat(food)) {
food.pickLocation();
}
snake.checkCollision();
document.querySelector('h1').innerText = 'Pontos: ' + snake.total;
}, 200);
};
// controle por setas do teclado
window.addEventListener('keydown', e => {
const direction = e.key.replace('Arrow', '');
snake.changeDirection(direction);
});
// controle por btns ui
controls.forEach(btn => {
btn.addEventListener('click', e => {
const direction = e.target.value
snake.changeDirection(direction)
})
})
start()