• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

203
Views
el navegador se congela cuando está en el bucle while (juego de serpientes)

Estoy haciendo la tarea del popular juego de serpientes para js. Tengo una función que está destinada a garantizar que después de comer una manzana, se mueva a una ubicación diferente en el lienzo sin estar en ninguna parte de la serpiente , la función sin el bucle funciona bien (aunque las manzanas también se colocan en la serpiente ):

 move() { let onSnake = true; let x = this.getRandomNumber(0, canvas.width - 1); //UnitSize);; let y = this.getRandomNumber(0, canvas.height - 1); //UnitSize);; this.x = x; this.y = y; this.draw(); }

Sin embargo, la función con el ciclo termina congelándose después de comer algunas manzanas (justo antes de golpear una manzana):

 move() { let onSnake = true; let x = this.getRandomNumber(0, canvas.width - 1); //UnitSize);; let y = this.getRandomNumber(0, canvas.height - 1); //UnitSize);; while (onSnake) { onSnake = false; x = this.getRandomNumber(0, canvas.width - 1); //UnitSize); y = this.getRandomNumber(0, canvas.height - 1); //UnitSize); for (let index = 0; index < snake.parts.length; index++) { if (x === snake.parts[index].x || y === snake.parts[index].y) { onSnake = true; break; } } } this.x = x; this.y = y; this.draw(); }

siendo getRandomNumber:

 getRandomNumber(min, max) { let r = Math.floor(Math.random() * (max - min + 1)) + min; r = r - (r % UnitSize); return r; }

esta es mi segunda pregunta sobre SO, no me fríes por favor....

almost 3 years ago · Juan Pablo Isaza
1 answers
Answer question

0

No tuve un momento para probar, pero sospecho que es tan simple como la siguiente evaluación.

 if (x === snake.parts[index].x || y === snake.parts[index].y) { onSnake = true; break; }

Está aceptando la colisión X o Y. Lo que significa que la manzana no puede compartir ninguna coordenada X o Y con ninguna de las piezas de serpiente. Lo que quieres en cambio es y creo. Es como decir que los dos vivimos en la misma casa porque vivimos en la misma calle. No, también necesitamos tener la misma dirección.

Como medida de seguridad, puede agregar un límite a la cantidad de iteraciones del ciclo que se pueden ejecutar en caso de que no quede más espacio para una manzana.

 let counter = 0; while (onSnake && counter < 5000) { onSnake = false; x = this.getRandomNumber(0, canvas.width - 1); //UnitSize); y = this.getRandomNumber(0, canvas.height - 1); //UnitSize); for (let index = 0; index < snake.parts.length; index++) { if (x === snake.parts[index].x && y === snake.parts[index].y) { onSnake = true; break; } } counter += 1; } if (counter == 5000) { alert("Could not find space for any more apples!"); }
almost 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error