|
1 | 1 | /*
|
2 |
| - * @name Snake game |
3 |
| - * @description The famous snake game! Once you click run, click anywhere |
4 |
| - * inside the black area, and control the snake using i j k and l. Don't let |
5 |
| - * the snake hit itself or the wall!<br> |
6 |
| - * Example created by <a href='https://github.com/prashantgupta24' target='_blank'>Prashant Gupta |
| 2 | + * @name Juego de Serpiente |
| 3 | + * @description El famoso juego de Serpiente! Después de hacer click en run/ejecutar, haz click en cualquier parte |
| 4 | + * dentro del área negra, y controla la serpiente usando i,j,k,l. No dejes que |
| 5 | + * la serpiente choque contra sí misma o la pared.<br> |
| 6 | + * Ejemplo creado por <a href='https://github.com/prashantgupta24' target='_blank'>Prashant Gupta |
7 | 7 | */
|
8 | 8 |
|
9 |
| -// the snake is divided into small segments, which are drawn and edited on each 'draw' call |
10 |
| -var numSegments = 10; |
11 |
| -var direction = 'right'; |
| 9 | +// La serpiente se divide en pequeños segmentos, los que son dibujados y editados en cada ejecución de draw() |
| 10 | +let numeroSegmentos = 10; |
| 11 | +let direccion = 'derecha'; |
12 | 12 |
|
13 |
| -var xStart = 0; //starting x coordinate for snake |
14 |
| -var yStart = 250; //starting y coordinate for snake |
15 |
| -var diff = 10; |
| 13 | +let xInicio = 0; //coordenada x de partida de la serpiente |
| 14 | +let yInicio = 250; //coordenada y de partida de la serpiente |
| 15 | +let diferencia = 10; |
16 | 16 |
|
17 |
| -var xCor = []; |
18 |
| -var yCor = []; |
| 17 | +let xCuerpo = []; |
| 18 | +let yCuerpo = []; |
19 | 19 |
|
20 |
| -var xFruit = 0; |
21 |
| -var yFruit = 0; |
22 |
| -var scoreElem; |
| 20 | +let xFruta = 0; |
| 21 | +let yFruta = 0; |
| 22 | +let elementoPuntaje; |
23 | 23 |
|
24 | 24 | function setup() {
|
25 |
| - scoreElem = createDiv('Score = 0'); |
26 |
| - scoreElem.position(20, 20); |
27 |
| - scoreElem.id = 'score'; |
28 |
| - scoreElem.style('color', 'white'); |
29 |
| - |
| 25 | + elementoPuntaje = createDiv('Puntaje = 0'); |
| 26 | + elementoPuntaje.position(20, 20); |
| 27 | + elementoPuntaje.id = 'puntaje'; |
| 28 | + elementoPuntaje.style('color', 'white'); |
| 29 | + |
30 | 30 | createCanvas(500, 500);
|
31 | 31 | frameRate(15);
|
32 | 32 | stroke(255);
|
33 | 33 | strokeWeight(10);
|
34 |
| - updateFruitCoordinates(); |
| 34 | + actualizarCoordenadasFruta(); |
35 | 35 |
|
36 |
| - for (var i = 0; i < numSegments; i++) { |
37 |
| - xCor.push(xStart + (i * diff)); |
38 |
| - yCor.push(yStart); |
| 36 | + for (let i = 0; i < numeroSegmentos; i++) { |
| 37 | + xCuerpo.push(xInicio + (i * diferencia)); |
| 38 | + yCuerpo.push(yInicio); |
39 | 39 | }
|
40 | 40 | }
|
41 | 41 |
|
42 | 42 | function draw() {
|
43 | 43 | background(0);
|
44 |
| - for (var i = 0; i < numSegments - 1; i++) { |
45 |
| - line(xCor[i], yCor[i], xCor[i + 1], yCor[i + 1]); |
| 44 | + for (let i = 0; i < numeroSegmentos - 1; i++) { |
| 45 | + line(xCuerpo[i], yCuerpo[i], xCuerpo[i + 1], yCuerpo[i + 1]); |
46 | 46 | }
|
47 |
| - updateSnakeCoordinates(); |
48 |
| - checkGameStatus(); |
49 |
| - checkForFruit(); |
| 47 | + actualizarCoordenadasSerpiente(); |
| 48 | + comprobarEstadoJuego(); |
| 49 | + comprobarFruta(); |
50 | 50 | }
|
51 | 51 |
|
52 | 52 | /*
|
53 |
| - The segments are updated based on the direction of the snake. |
54 |
| - All segments from 0 to n-1 are just copied over to 1 till n, i.e. segment 0 |
55 |
| - gets the value of segment 1, segment 1 gets the value of segment 2, and so on, |
56 |
| - and this results in the movement of the snake. |
57 |
| -
|
58 |
| - The last segment is added based on the direction in which the snake is going, |
59 |
| - if it's going left or right, the last segment's x coordinate is increased by a |
60 |
| - predefined value 'diff' than its second to last segment. And if it's going up |
61 |
| - or down, the segment's y coordinate is affected. |
| 53 | + Los segmentos son actualizados en la dirección de la serpiente. |
| 54 | + Todos los segmentos entre 0 y n-1 son copiados al rango 1 hasta n, por ejemplo, el segmento 0 recibe el valor del segmento 1, el segmento 1 recibe el |
| 55 | +vallor del segmento 2, y así, esto resulta en el movimiento de la serpiente. |
| 56 | +
|
| 57 | + El último segmento es añadido según la dirección de movimiento de la serpiente, |
| 58 | + si está yendo hacia izquierda o derecha, la coordenada x del último segmento |
| 59 | + es igual a sumar un valor predefinido como 'diferencia' al valor del penúltimo |
| 60 | + segmento. Y si está yendo hacia arriba o abajo, la coordenada y es afectada. |
| 61 | + } |
| 62 | +
|
62 | 63 | */
|
63 |
| -function updateSnakeCoordinates() { |
| 64 | +function actualizarCoordenadasSerpiente() { |
64 | 65 |
|
65 |
| - for (var i = 0; i < numSegments - 1; i++) { |
66 |
| - xCor[i] = xCor[i + 1]; |
67 |
| - yCor[i] = yCor[i + 1]; |
| 66 | + for (let i = 0; i < numeroSegmentos - 1; i++) { |
| 67 | + xCuerpo[i] = xCuerpo[i + 1]; |
| 68 | + yCuerpo[i] = yCuerpo[i + 1]; |
68 | 69 | }
|
69 |
| - switch (direction) { |
70 |
| - case 'right': |
71 |
| - xCor[numSegments - 1] = xCor[numSegments - 2] + diff; |
72 |
| - yCor[numSegments - 1] = yCor[numSegments - 2]; |
| 70 | + switch (direccion) { |
| 71 | + case 'derecha': |
| 72 | + xCuerpo[numeroSegmentos - 1] = xCuerpo[numeroSegmentos - 2] + diferencia; |
| 73 | + yCuerpo[numeroSegmentos - 1] = yCuerpo[numeroSegmentos - 2]; |
73 | 74 | break;
|
74 |
| - case 'up': |
75 |
| - xCor[numSegments - 1] = xCor[numSegments - 2]; |
76 |
| - yCor[numSegments - 1] = yCor[numSegments - 2] - diff; |
| 75 | + case 'arriba': |
| 76 | + xCuerpo[numeroSegmentos - 1] = xCuerpo[numeroSegmentos - 2]; |
| 77 | + yCuerpo[numeroSegmentos - 1] = yCuerpo[numeroSegmentos - 2] - diferencia; |
77 | 78 | break;
|
78 |
| - case 'left': |
79 |
| - xCor[numSegments - 1] = xCor[numSegments - 2] - diff; |
80 |
| - yCor[numSegments - 1] = yCor[numSegments - 2]; |
| 79 | + case 'izquierda': |
| 80 | + xCuerpo[numeroSegmentos - 1] = xCuerpo[numeroSegmentos - 2] - diferencia; |
| 81 | + yCuerpo[numeroSegmentos - 1] = yCuerpo[numeroSegmentos - 2]; |
81 | 82 | break;
|
82 |
| - case 'down': |
83 |
| - xCor[numSegments - 1] = xCor[numSegments - 2]; |
84 |
| - yCor[numSegments - 1] = yCor[numSegments - 2] + diff; |
| 83 | + case 'abajo': |
| 84 | + xCuerpo[numeroSegmentos - 1] = xCuerpo[numeroSegmentos - 2]; |
| 85 | + yCuerpo[numeroSegmentos - 1] = yCuerpo[numeroSegmentos - 2] + diferencia; |
85 | 86 | break;
|
86 | 87 | }
|
87 | 88 | }
|
88 | 89 |
|
89 | 90 | /*
|
90 |
| - I always check the snake's head position xCor[xCor.length - 1] and |
91 |
| - yCor[yCor.length - 1] to see if it touches the game's boundaries |
92 |
| - or if the snake hits itself. |
| 91 | + Siempre reviso la posición de la cabeza de la serpiente |
| 92 | + xCuerpo[xCuerpo.length - 1] e yCuerpo[yCuerpo.length - 1] para revisar si toca |
| 93 | + los bordes del juego o si la serpiente se estrelló contra sí misma. |
93 | 94 | */
|
94 |
| -function checkGameStatus() { |
95 |
| - if (xCor[xCor.length - 1] > width || |
96 |
| - xCor[xCor.length - 1] < 0 || |
97 |
| - yCor[yCor.length - 1] > height || |
98 |
| - yCor[yCor.length - 1] < 0 || |
99 |
| - checkSnakeCollision()) { |
| 95 | +function comprobarEstadoJuego() { |
| 96 | + if (xCuerpo[xCuerpo.length - 1] > width || |
| 97 | + xCuerpo[xCuerpo.length - 1] < 0 || |
| 98 | + yCuerpo[yCuerpo.length - 1] > height || |
| 99 | + yCuerpo[yCuerpo.length - 1] < 0 || |
| 100 | + detectarColision()) { |
100 | 101 | noLoop();
|
101 |
| - var scoreVal = parseInt(scoreElem.html().substring(8)); |
102 |
| - scoreElem.html('Game ended! Your score was : ' + scoreVal); |
| 102 | + let puntajeValor = parseInt(elementoPuntaje.html().substring(8)); |
| 103 | + elementoPuntaje.html('Juego finalizado! Tu puntaje fue: ' + puntajeValor); |
103 | 104 | }
|
104 | 105 | }
|
105 | 106 |
|
106 | 107 | /*
|
107 |
| - If the snake hits itself, that means the snake head's (x,y) coordinate |
108 |
| - has to be the same as one of its own segment's (x,y) coordinate. |
| 108 | + Si la serpiente se estrella contra sí misma, esto significa que la coordenada |
| 109 | + (x,y) tiene que ser igual a la de un segmento propio. |
109 | 110 | */
|
110 |
| -function checkSnakeCollision() { |
111 |
| - var snakeHeadX = xCor[xCor.length - 1]; |
112 |
| - var snakeHeadY = yCor[yCor.length - 1]; |
113 |
| - for (var i = 0; i < xCor.length - 1; i++) { |
114 |
| - if (xCor[i] === snakeHeadX && yCor[i] === snakeHeadY) { |
| 111 | +function detectarColision() { |
| 112 | + let cabezaSerpienteX = xCuerpo[xCuerpo.length - 1]; |
| 113 | + let cabezaSerpienteY = yCuerpo[yCuerpo.length - 1]; |
| 114 | + for (let i = 0; i < xCuerpo.length - 1; i++) { |
| 115 | + if (xCuerpo[i] === cabezaSerpienteX && yCuerpo[i] === cabezaSerpienteY) { |
115 | 116 | return true;
|
116 | 117 | }
|
117 | 118 | }
|
118 | 119 | }
|
119 | 120 |
|
120 | 121 | /*
|
121 |
| - Whenever the snake consumes a fruit, I increment the number of segments, |
122 |
| - and just insert the tail segment again at the start of the array (basically |
123 |
| - I add the last segment again at the tail, thereby extending the tail) |
| 122 | + Cada vez que la serpiente consume una fruta, incremento el número de segmentos, |
| 123 | + y simplemente inserto este segmento de cola nuevamente al principio del arreglo |
| 124 | + (básicamente añado el último segmento a la cola, con lo que la alargo). |
124 | 125 | */
|
125 |
| -function checkForFruit() { |
126 |
| - point(xFruit, yFruit); |
127 |
| - if (xCor[xCor.length - 1] === xFruit && yCor[yCor.length - 1] === yFruit) { |
128 |
| - var prevScore = parseInt(scoreElem.html().substring(8)); |
129 |
| - scoreElem.html('Score = ' + (prevScore + 1)); |
130 |
| - xCor.unshift(xCor[0]); |
131 |
| - yCor.unshift(yCor[0]); |
132 |
| - numSegments++; |
133 |
| - updateFruitCoordinates(); |
| 126 | +function comprobarFruta() { |
| 127 | + point(xFruta, yFruta); |
| 128 | + if (xCuerpo[xCuerpo.length - 1] === xFruta && yCuerpo[yCuerpo.length - 1] === yFruta) { |
| 129 | + let prevScore = parseInt(elementoPuntaje.html().substring(8)); |
| 130 | + elementoPuntaje.html('Score = ' + (prevScore + 1)); |
| 131 | + xCuerpo.unshift(xCuerpo[0]); |
| 132 | + yCuerpo.unshift(yCuerpo[0]); |
| 133 | + numeroSegmentos++; |
| 134 | + actualizarCoordenadasFruta(); |
134 | 135 | }
|
135 | 136 | }
|
136 | 137 |
|
137 |
| -function updateFruitCoordinates() { |
| 138 | +function actualizarCoordenadasFruta() { |
138 | 139 | /*
|
139 |
| - The complex math logic is because I wanted the point to lie |
140 |
| - in between 100 and width-100, and be rounded off to the nearest |
141 |
| - number divisible by 10, since I move the snake in multiples of 10. |
| 140 | + Hice matemática compleja porque quería que el punto estuviera |
| 141 | + entre 100 y width-100, y que fuera aproximado al número divisible |
| 142 | + por 10 más cercano, ya que muevo la serpiente en múltiplos de 10. |
142 | 143 | */
|
143 | 144 |
|
144 |
| - xFruit = floor(random(10, (width - 100) / 10)) * 10; |
145 |
| - yFruit = floor(random(10, (height - 100) / 10)) * 10; |
| 145 | + xFruta = floor(random(10, (width - 100) / 10)) * 10; |
| 146 | + yFruta = floor(random(10, (height - 100) / 10)) * 10; |
146 | 147 | }
|
147 | 148 |
|
148 | 149 | function keyPressed() {
|
149 | 150 | switch (keyCode) {
|
150 | 151 | case 74:
|
151 |
| - if (direction != 'right') { |
152 |
| - direction = 'left'; |
| 152 | + if (direccion != 'derecha') { |
| 153 | + direccion = 'izquierda'; |
153 | 154 | }
|
154 | 155 | break;
|
155 | 156 | case 76:
|
156 |
| - if (direction != 'left') { |
157 |
| - direction = 'right'; |
| 157 | + if (direccion != 'izquierda') { |
| 158 | + direccion = 'derecha'; |
158 | 159 | }
|
159 | 160 | break;
|
160 | 161 | case 73:
|
161 |
| - if (direction != 'down') { |
162 |
| - direction = 'up'; |
| 162 | + if (direccion != 'abajo') { |
| 163 | + direccion = 'arriba'; |
163 | 164 | }
|
164 | 165 | break;
|
165 | 166 | case 75:
|
166 |
| - if (direction != 'up') { |
167 |
| - direction = 'down'; |
| 167 | + if (direccion != 'arriba') { |
| 168 | + direccion = 'abajo'; |
168 | 169 | }
|
169 | 170 | break;
|
170 | 171 | }
|
|
0 commit comments