Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!