Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

94
Vistas
Snake not moving properly for pixel Snake Game

Why isn't my snake properly moving? h is the head. The segments always seem to collapse into each other all getting set to the head coords. I thought it'd be as simple as remove from the back and add to the front but I guess not. Any help?

        this.segments.forEach(seg => s.setPixel(seg.x, seg.y, 1));

        const h = [...this.segments][0];

        h.x += this.vX;
        h.y += this.vY;

        this.segments.unshift(h);
        this.segments.pop();
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I think maybe the problem is that you are failing to properly clone the head.

const h = [...this.segments][0]; 

The above assigns the first element in a newly created Array that still has the exact same objects inside. Try using the Object.assign() method:

        this.segments.forEach(seg => s.setPixel(seg.x, seg.y, 1));

        const h = Object.assign({}, this.segments[0]);

        h.x += this.vX;
        h.y += this.vY;

        this.segments.unshift(h);
        this.segments.pop();

https://www.samanthaming.com/tidbits/70-3-ways-to-clone-objects/#_1-using-spread https://www.samanthaming.com/tidbits/70-3-ways-to-clone-objects/#_2-using-object-assign

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda