Haz que se mueva
Acá está el secreto de todos los videojuegos, y es más simple de lo que parece.
Make it move
Here is the secret behind every video game, and it is simpler than it looks.
En una pantalla nada se mueve nunca. Lo que pasa es que el computador borra todo y lo vuelve a dibujar un poquito más allá, unas 60 veces por segundo. Tu ojo se encarga del resto.
Exactamente como los dibujos animados: muchas imágenes fijas, una tras otra.
Ese ciclo se llama requestAnimationFrame: "cuando estés listo para el
siguiente cuadro, llámame".
On a screen, nothing ever moves. What happens is that the computer erases everything and redraws it slightly further along, about 60 times a second. Your eye does the rest.
Exactly like hand-drawn cartoons: many still frames, one after another.
That cycle is called requestAnimationFrame: "when you are ready for the next
frame, call me".
Rebotar suena complicado y son dos líneas. La pelota guarda su velocidad
en vx y vy. Cuando toca un borde, esa velocidad se invierte: si iba
hacia la derecha, ahora va a la izquierda.
Eso es todo. El signo menos es la física completa.
Bouncing sounds complicated and it is two lines. The ball stores its speed
in vx and vy. When it hits an edge, that speed flips: if it was
going right, now it goes left.
That is it. The minus sign is the entire physics engine.
Agrégale una paleta que sigas con el dedo y ya es un juego. En serio: esto es literalmente el motor de un pong.
Mueve el mouse o el dedo sobre el recuadro.
Add a paddle you control with your finger and it is a game. Seriously: this is literally the engine of a pong.
Move your mouse or finger over the box.
El bucle de animación: borrar, calcular, dibujar, repetir. Todos los juegos que existen funcionan así, desde este pong hasta los de consola. La diferencia es cuántas cosas se calculan en cada vuelta.
Nuestro juego Swipong usa este mismo bucle. Solo que la paleta, en vez de seguir tu dedo, sigue tu mano de verdad.
The animation loop: erase, calculate, draw, repeat. Every game that exists works this way, from this pong to console titles. The difference is how much gets calculated on each pass.
Our game Swipong uses this exact loop. Only the paddle, instead of following your finger, follows your real hand.