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

162
Vistas
Snake Game Values Broken

So I'm having a problem with my snake game it's coded in js and I'm trying to figure out how to make a menu where you can change the values, the value doesn't get logged in the console and it does not appear in the game.

https://jsfiddle.net/Faxtixe/duq6ba3x/3/

class Apple {
    constructor() {
        let isTouching

        while (true) {
            isTouching = false;
            this.x = Math.floor(Math.random() * canvas.width / snake.size) * snake.size
            this.y = Math.floor(Math.random() * canvas.height / snake.size) * snake.size

            for (let i = 0; i < snake.tail.length; i++) {
                if (this.x == snake.tail[i].x && this.y == snake.tail[i].y) {
                    isTouching = false
                }
            }

            var tb1 = document.getElementById("setInterval").value // fps/Interval var curruntly not working
            var tb2 = document.getElementById("thisColor").value // color var also curruntly not working
            console.log(tb1);
            console.log(tb2);


            this.size = snake.size
            this.color = tb2.value // here is the color of the food

And Console Errors

window.onload = () => {
    gameLoop()
}
function gameLoop() {
    setInterval(show, 1000/tb1.value) // here is our fps value
}

This is the HTML code

<form>
    <label for="setInterval">Speed/FPS:</label>
    <input type="text" id="setInterval" name="setInterval"><br><br>
    <label for="this.color">Food Color:</label>
    <input type="text" id="thisColor" name="thisColor"><br><br>
    <input type="submit" value="Submit">
</form>

Menu

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

At minimum you need to initialize the tb1 variable.

const tb1 = document.getElementById("setInterval")

In the form you need to have some initial value, given that you start your game running when the page loads and before the user would have a chance to set that variable. There are other problems you will need to solve to actually get things working as you intend, but at least this will help you get past the error in your question.

<input type="text" id="setInterval" name="setInterval" value="50"><br><br>
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