Skip to content

Commit 92cced0

Browse files
committed
Capítulo 16. Secciones: Actores y Encapsulación
1 parent ddb79c1 commit 92cced0

File tree

1 file changed

+65
-137
lines changed

1 file changed

+65
-137
lines changed

16_game.md

Lines changed: 65 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ Podemos representar el fondo como una tabla ya que es una ((cuadrícula)) invari
5757

5858
{{index performance, [DOM, graphics]}}
5959

60-
En los juegos y otros programas que deben animar ((gráficos)) y responder a la ((entrada)) del usuario sin retraso notable, la ((eficiencia)) es importante. Aunque el DOM no fue diseñado originalmente para gráficos de alto rendimiento, es realmente mejor en ello de lo que se esperaría. Viste algunas ((animaciones)) en [Chapter ?](dom#animation). En una máquina moderna, un simple juego como este desempeña bien, aún si no nos preocupamos mucho de la ((optimización)).
60+
En los juegos y otros programas que deben animar ((gráficos)) y responder a la ((entrada)) del usuario sin retraso notable, la ((eficiencia)) es importante. Aunque el DOM no fue diseñado originalmente para gráficos de alto rendimiento, es realmente mejor en ello de lo que se esperaría. Viste algunas ((animaciones)) en [Capítulo ?](dom#animation). En una máquina moderna, un simple juego como este desempeña bien, aún si no nos preocupamos mucho de la ((optimización)).
6161

6262
{{index canvas, [DOM, graphics]}}
6363

64-
En el [siguiente capítulo](canvas), exploraremos otra tecnología del ((navegador)), la etiqueta `<canvas>`, la cual provee un forma más tradicional de dibujar gráficos, trabajando en término de formas y ((pixel))es más que elementos del DOM.
64+
En el [siguiente capítulo](canvas), exploraremos otra tecnología del ((navegador)), la etiqueta `<canvas>`, la cual provee un forma más tradicional de dibujar gráficos, trabajando en término de formas y ((pixel))es más que elementos del DOM.
6565

6666
## Niveles
6767

@@ -72,7 +72,7 @@ Vamos a querer una forma de especificar niveles que sea fácilmente leíble y ed
7272
El plano para un nivel pequeño podría lucir como esto:
7373

7474
```{includeCode: true}
75-
let planoDeNivel = `
75+
let simplePlanoDeNivel = `
7676
......................
7777
..#................#..
7878
..#..............=.#..
@@ -141,7 +141,7 @@ Para interpretar los caracteres en el plano, el constructor del `Nivel` usa el o
141141

142142
{{index "Vec class"}}
143143

144-
La posición del actor es guardada como un objeto `Vector`. Este es un vector bidimensional, un objeto con propiedades `x` y `y`, como se vió en los ejercicios de [Chapter ?](object#exercise_vector).
144+
La posición del actor es guardada como un objeto `Vector`. Este es un vector bidimensional, un objeto con propiedades `x` y `y`, como se vió en los ejercicios de [Capítulo ?](object#exercise_vector).
145145

146146
{{index [state, in objects]}}
147147

@@ -170,36 +170,21 @@ La propiedad `estatus` cambiará a `"perdido"` or `"ganado"` cuando el juego hay
170170

171171
Esto es de nuevo una estructura de datos persistente-actualizar el estado del juego crea un nuevo estado y deja el anterior intacto.
172172

173-
## Actors
173+
## Actores
174174

175175
{{index actor, "Vec class", [interface, object]}}
176176

177-
Actor objects represent the current position and state of a given
178-
moving element in our game. All actor objects conform to the same
179-
interface. Their `pos` property holds the coordinates of the
180-
element's top-left corner, and their `size` property holds its size.
177+
Los objetos Actor representan la posición actual y el estado de un elemento móvil dado en nuestro juego. Todos los objetos Actor se ajustan a la misma interface. Su propiedad `posicion` tiene las coordenadas de la esquina superior izquierda del elemento y su propiedad `tamano` tiene su tamaño.
181178

182-
Then they have an `update` method, which is used to compute their
183-
new state and position after a given time step. It simulates the thing
184-
the actor does—moving in response to the arrow keys for the player and
185-
bouncing back and forth for the lava—and returns a new, updated actor
186-
object.
179+
Luego tienen un método `update`, que es usado para computar su nuevo estado y posición después de un paso de tiempo. Simula lo que hace el actor -moviéndose en respuesta a las teclas de flecha para el jugador y rebotar de un lado a otro para la lava-y regresa un objeto actor nuevo y actualizado.
187180

188-
A `type` property contains a string that identifies the type of the
189-
actor—`"player"`, `"coin"`, or `"lava"`. This is useful when drawing
190-
the game—the look of the rectangle drawn for an actor is based on its
191-
type.
181+
Una propiedad `tipo` contiene la cadena de caracteres que identifica el tipo de actor `"jugador"`, `"moneda"` o `"lava"`. Esto es útil cuando se dibuja el juego-la apariencia del rectángulo dibujado para un actor está basado en su tipo.
192182

193-
Actor classes have a static `create` method that is used by the
194-
`Level` constructor to create an actor from a character in the level
195-
plan. It is given the coordinates of the character and the character
196-
itself, which is needed because the `Lava` class handles several
197-
different characters.
183+
Las clases actor tienen un método estático `create` que es usado por el constructor de `Nivel` para crear un actor a partir de un caracter en el plano de nivel. Se le dan las coordenadas del caracter y el caracter mismo, el cuál es necesario debido a que la clase `Lava` maneja diferentes caracteres.
198184

199185
{{id vector}}
200186

201-
This is the `Vector` class that we'll use for our two-dimensional values,
202-
such as the position and size of actors.
187+
Esta es la clase `Vector` que usaremos para nuestros valores bidimensionales, tales como la posición y el tamaño de los actores.
203188

204189
```{includeCode: true}
205190
class Vector {
@@ -217,195 +202,139 @@ class Vector {
217202

218203
{{index "times method", multiplication}}
219204

220-
The `times` method scales a vector by a given number. It will be
221-
useful when we need to multiply a speed vector by a time interval to
222-
get the distance traveled during that time.
205+
El método `times` escala un vector por un número dado. Será útil cuando necesitemos multiplicar un vector de velocidad por un intervalo de tiempo para obtener la distancia recorrida durante ese tiempo.
223206

224-
The different types of actors get their own classes since their
225-
behavior is very different. Let's define these classes. We'll get to
226-
their `update` methods later.
207+
Los diferentes tipos de actores tienen sus propias clases ya que su comportamiento es diferente. Vamos a definir estas clases. Veremos sus métodos `update` después.
227208

228209
{{index simulation, "Player class"}}
229210

230-
The player class has a property `speed` that stores its current speed
231-
to simulate momentum and gravity.
211+
La clase jugador tiene una propiedad `velocidad` que guarda su velocidad actual para simular momento y gravedad.
232212

233213
```{includeCode: true}
234-
class Player {
235-
constructor(pos, speed) {
236-
this.pos = pos;
237-
this.speed = speed;
214+
class Jugador {
215+
constructor(posicion, velocidad) {
216+
this.posicion = posicion;
217+
this.velocidad = velocidad;
238218
}
239219
240-
get type() { return "player"; }
220+
get type() { return "jugador"; }
241221
242-
static create(pos) {
243-
return new Player(pos.plus(new Vector(0, -0.5)),
222+
static create(posicion) {
223+
return new Jugador(posicion.plus(new Vector(0, -0.5)),
244224
new Vector(0, 0));
245225
}
246226
}
247227
248-
Player.prototype.size = new Vector(0.8, 1.5);
228+
Player.prototype.tamano = new Vector(0.8, 1.5);
249229
```
250230

251-
Because a player is one-and-a-half squares high, its initial position
252-
is set to be half a square above the position where the `@` character
253-
appeared. This way, its bottom aligns with the bottom of the square it
254-
appeared in.
231+
Debido a que un jugador tiene una altura de un cuadrado y medio, su posición inicial está establecida a ser medio cuadrado arriba de la posición donde el caracter `@` apareció. De esta manera, su parte inferior se alinea con la parte inferior del cuadrado en el que apareció.
255232

256-
The `size` property is the same for all instances of `Player`, so we
257-
store it on the prototype rather than on the instances themselves. We
258-
could have used a ((getter)) like `type`, but that would create and
259-
return a new `Vector` object every time the property is read, which would
260-
be wasteful. (Strings, being ((immutable)), don't have to be re-created
261-
every time they are evaluated.)
233+
La propiedad `tamano` es la misma para todas las instancias de `Jugador`, así que la guardamos en el prototipo en vez de en la instancia misma. Podríamos haber usado un ((getter)) como `type`, pero eso crearía y regresaría un nuevo objeto `Vector` cada vez que la propiedad sea leída, lo cual sería desperdicio. (Las cadenas de caracteres, siendo ((inmutables)), no tienen que ser creadas de nuevo cada vez que son evaluadas.)
262234

263235
{{index "Lava class", bouncing}}
264236

265-
When constructing a `Lava` actor, we need to initialize the object
266-
differently depending on the character it is based on. Dynamic lava
267-
moves along at its current speed until it hits an obstacle. At that
268-
point, if it has a `reset` property, it will jump back to its start
269-
position (dripping). If it does not, it will invert its speed and
270-
continue in the other direction (bouncing).
237+
Al construir un actor `Lava`, necesitamos inicializar el objeto de manera diferente dependiendo del caracter en el que está basado. La lava dinámica se mueve a su velocidad actual hasta que pega con un obstáculo. En ese punto, si tiene una propiedad `reiniciar`, brincará de regreso a su posición inicial (goteando). Si no lo hace, invertirá su velocidad y continuará en otra dirección (rebotando).
271238

272-
The `create` method looks at the character that the `Level`
273-
constructor passes and creates the appropriate lava actor.
239+
El método `create` mira al caracter que el constructor de `Level` pasa y crea el actor de lava apropiado.
274240

275241
```{includeCode: true}
276242
class Lava {
277-
constructor(pos, speed, reset) {
278-
this.pos = pos;
279-
this.speed = speed;
280-
this.reset = reset;
243+
constructor(posicion, velocidad, reiniciar) {
244+
this.posicion = posicion;
245+
this.velocidad = velocidad;
246+
this.reiniciar = reiniciar;
281247
}
282248
283249
get type() { return "lava"; }
284250
285-
static create(pos, ch) {
286-
if (ch == "=") {
287-
return new Lava(pos, new Vector(2, 0));
288-
} else if (ch == "|") {
289-
return new Lava(pos, new Vector(0, 2));
290-
} else if (ch == "v") {
291-
return new Lava(pos, new Vector(0, 3), pos);
251+
static create(posicion, car) {
252+
if (car == "=") {
253+
return new Lava(posicion, new Vector(2, 0));
254+
} else if (car == "|") {
255+
return new Lava(posicion, new Vector(0, 2));
256+
} else if (car == "v") {
257+
return new Lava(posicion, new Vector(0, 3), posicion);
292258
}
293259
}
294260
}
295261
296-
Lava.prototype.size = new Vector(1, 1);
262+
Lava.prototype.tamano = new Vector(1, 1);
297263
```
298264

299265
{{index "Coin class", animation}}
300266

301-
`Coin` actors are relatively simple. They mostly just sit in their
302-
place. But to liven up the game a little, they are given a "wobble", a
303-
slight vertical back-and-forth motion. To track this, a coin object
304-
stores a base position as well as a `wobble` property that tracks the
305-
((phase)) of the bouncing motion. Together, these determine the coin's
306-
actual position (stored in the `pos` property).
267+
Los actores `Moneda` son relativamente simples. Mayormente se quedan en su lugar. Pero para avivar un poco el juego, se les da un "bamboleo", un leve movimiento vertical de arriba a abajo. Para serguir esto, un objeto moneda guarda una posición base así como una propiedad `wobble` que registra la ((fase)) del movimiento de rebote. Juntos, estos determinan la posición real de la moneda (guardada en la propiedad `posicion`).
307268

308269
```{includeCode: true}
309-
class Coin {
310-
constructor(pos, basePos, wobble) {
311-
this.pos = pos;
312-
this.basePos = basePos;
313-
this.wobble = wobble;
270+
class Moneda {
271+
constructor(posicion, posBase, bamboleo) {
272+
this.posicion = posicion;
273+
this.posBase = posBase;
274+
this.bamboleo = bamboleo;
314275
}
315276
316277
get type() { return "coin"; }
317278
318-
static create(pos) {
319-
let basePos = pos.plus(new Vector(0.2, 0.1));
320-
return new Coin(basePos, basePos,
279+
static create(posicion) {
280+
let posBase = posicion.plus(new Vector(0.2, 0.1));
281+
return new Coin(posBase, posBase,
321282
Math.random() * Math.PI * 2);
322283
}
323284
}
324285
325-
Coin.prototype.size = new Vector(0.6, 0.6);
286+
Coin.prototype.tamano = new Vector(0.6, 0.6);
326287
```
327288

328289
{{index "Math.random function", "random number", "Math.sin function", sine, wave}}
329290

330-
In [Chapter ?](dom#sin_cos), we saw that `Math.sin` gives us the
331-
y-coordinate of a point on a circle. That coordinate goes back and
332-
forth in a smooth waveform as we move along the circle, which makes
333-
the sine function useful for modeling a wavy motion.
291+
En el [Capítulo ?](dom#sin_cos), vimos que `Math.sin` nos da la coordenada-y de un punto en un círculo. Esa coordenada va de lado a otro en una forma de onda suave mientras nos movemos por el círculo, lo cual hace útil a la función seno para modelar movimiento ondulado.
334292

335293
{{index pi}}
336294

337-
To avoid a situation where all coins move up and down synchronously,
338-
the starting phase of each coin is randomized. The _((phase))_ of
339-
`Math.sin`'s wave, the width of a wave it produces, is 2π. We multiply
340-
the value returned by `Math.random` by that number to give the coin a
341-
random starting position on the wave.
295+
Para evitar una situación donde todas las monedas se mueven arriba y abajo sincrónicamente, la fase inicial de cada moneda es aleatoria. La _((fase))_ de la onda de `Math.sin`, el ancho de la onda que produce, es 2π. Multiplicamos el valor retornado por `Math.random` por ese número para dar a la moneda una posición inicial aleatoria en la onda.
342296

343297
{{index map, [object, "as map"]}}
344298

345-
We can now define the `levelChars` object that maps plan characters to
346-
either background grid types or actor classes.
299+
Ahora podemos definir el objeto `caracteresDeNivel` que mapea los caracteres del plano ya sea a rejilla de fondo o clases de actores.
347300

348301
```{includeCode: true}
349-
const levelChars = {
350-
".": "empty", "#": "wall", "+": "lava",
351-
"@": Player, "o": Coin,
302+
const caracteresDeNivel = {
303+
".": "vacío", "#": "muro", "+": "lava",
304+
"@": Jugador, "o": Moneda,
352305
"=": Lava, "|": Lava, "v": Lava
353306
};
354307
```
355308

356-
That gives us all the parts needed to create a `Level` instance.
309+
Eso nos da todas las partes que necesitamos para crear una instancia de `Level`.
357310

358311
```{includeCode: strip_log}
359-
let simpleLevel = new Level(simpleLevelPlan);
360-
console.log(`${simpleLevel.width} by ${simpleLevel.height}`);
312+
let simpleNivel = new Nivel(simplePlanoDeNivel);
313+
console.log(`${simpleNivel.width} by ${simpleNivel.height}`);
361314
// → 22 by 9
362315
```
363316

364-
The task ahead is to display such levels on the screen and to model
365-
time and motion inside them.
317+
La tarea adelante es mostrar dichos niveles en la pantalla y modelar tiempo y movimiento dentro de ellos.
366318

367-
## Encapsulation as a burden
319+
## Encapsulación como una carga
368320

369321
{{index "programming style", "program size", complexity}}
370322

371-
Most of the code in this chapter does not worry about
372-
((encapsulation)) very much for two reasons. First, encapsulation
373-
takes extra effort. It makes programs bigger and requires additional
374-
concepts and interfaces to be introduced. Since there is only so much
375-
code you can throw at a reader before their eyes glaze over, I've made
376-
an effort to keep the program small.
323+
La mayoría del código en este capítulo no se preocupa mucho de la ((encapsulación)) por dos razones. Primero, encapsular necesita esfuerzo extra. Hace a los programas más grandes y requiere que conceptos e interfaces adicionales sean introducidos. Dado que sólo hay una cantidad de código que le puedes mostrar a un lector antes de que sus ojos pierdan la atención, he hecho un esfuerzo por mantener el programa pequeño.
377324

378325
{{index [interface, design]}}
379326

380-
Second, the various elements in this game are so closely tied together
381-
that if the behavior of one of them changed, it is unlikely that any
382-
of the others would be able to stay the same. Interfaces between the
383-
elements would end up encoding a lot of assumptions about the way the
384-
game works. This makes them a lot less effective—whenever you change
385-
one part of the system, you still have to worry about the way it
386-
impacts the other parts because their interfaces wouldn't cover the
387-
new situation.
388-
389-
Some _((cutting point))s_ in a system lend themselves well to
390-
separation through rigorous interfaces, but others don't. Trying to
391-
encapsulate something that isn't a suitable boundary is a sure way to
392-
waste a lot of energy. When you are making this mistake, you'll
393-
usually notice that your interfaces are getting awkwardly large and
394-
detailed and that they need to be changed often, as the program
395-
evolves.
327+
Segundo, los elementos varios en este juego están tan estrechamente vinculados que si el comportamiento de uno de ellos cambia, es poco probable que alguno de los otros logre quedarse igual. Las interfaces entre los elementos terminarían codificando muchas de los supuestos acerca de la forma que el juego funciona. Esto las hace mucho menos efectivas-cuando sea que cambies una parte del sistema, todavía tienes que precouparte de la forma en que impacta a las otras partes porque sus interfaces no cubrirían la nueva situación.
328+
329+
Algunos _((puntos de corte))_ en un sistema se prestan bien a la separacion mediante interfaces rigurosas, pero otros no. Intentando encapsular algo que no es un límite adecuado es una manera segura de deserdiciar mucha energía. Cuando cometes este error, te darás cuenta que tus interfaces se hacen incómodamente grandes y detalladas y que necesitan cambiarse frecuentemente, mientras el programa evoluciona.
396330

397331
{{index graphics, encapsulation, graphics}}
398332

399-
There is one thing that we _will_ encapsulate, and that is the
400-
((drawing)) subsystem. The reason for this is that we'll ((display))
401-
the same game in a different way in the [next
402-
chapter](canvas#canvasdisplay). By putting the drawing behind an
403-
interface, we can load the same game program there and plug in a new
404-
display ((module)).
333+
Hay una cosa que sí _vamos_ a encapsular, y eso es el subsistema de ((dibujo)). La razón para esto es que ((mostraremos)) el mismo juego en una manera diferente en el [próximo capítulo](canvas#canvasdisplay). Poniendo el dibujo detrás de una interface, podemos cargar el mismo programa de juego ahí y conectar un nuevo ((module)) de pantalla.
405334

406335
{{id domdisplay}}
407336

408-
## Drawing
337+
## Dibujar
409338

410339
{{index "DOMDisplay class", [DOM, graphics]}}
411340

@@ -519,8 +448,7 @@ don't want space between the ((table)) cells or padding inside them.
519448
The `background` rule sets the background color. CSS allows colors to
520449
be specified both as words (`white`) or with a format such as
521450
`rgb(R, G, B)`, where the red, green, and blue components of the color
522-
are separated into three numbers from 0 to 255. So, in `rgb(52, 166,
523-
251)`, the red component is 52, green is 166, and blue is 251. Since
451+
are separated into three numbers from 0 to 255. So, in `rgb(52, 166, 251)`, the red component is 52, green is 166, and blue is 251. Since
524452
the blue component is the largest, the resulting color will be bluish.
525453
You can see that in the `.lava` rule, the first number (red) is the
526454
largest.

0 commit comments

Comments
 (0)