Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

99
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda