Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Snake game ( separate snake body )

  • This topic is empty.
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #145581
    Kuzyo
    Participant

    Found a problem writing JS game ( Snake ). Can’t separating divs in snake body. Now I achieved that several part of snake body move in one direction, but one on another ( top an left position update the same for each block ). I want to separate them like in a snake, but don’t have idea how to do that. Can somebody to give some advices? Here is my js code ( works only in chrome): http://codepen.io/Kuzyo/pen/esFbf

    #145628
    Kuzyo
    Participant

    Ok, never thought about rows and cols, for me example was http://patorjk.com/games/snake/http://patorjk.com/games/snake/ here coder seems don’t use rows and cols. I will try. Thanks for the help.

    #145639
    Kuzyo
    Participant

    Now I have two arrays: one is snakeBody(contains divs that will be body of the snake), second array contains arrays with coordinats of first element from snakeBody(snakeBody[0]) after he start moving in such form pastCoord = [[20,0], [40, 0], [60, 0]……] (here [coordX, coordY]). Every step is 20px.

    I don’t know how to apply coords from pastCoord array to elements in snakeBody array.

    I think about double for loop, but what to do in this loops I don’t know:

    for (var i = this.snakeBody.length - 1; i >= 0; i--) {
             for (var j = gameObj.pastCoord.length - 1; j >= 0; j--) {
    
    };
    
    #145660
    Kuzyo
    Participant

    The last result. I separated snakeHead from snakeBody( contains divs which will go after the snakeHead). And now every step coords of snakeHead goes togameObj.pastCoordarray. I' m trying to make that every step elements fromsnakeObj.snakeBody arrayapply to elements from gameObj.pastCoord array. I hardcoded only for the first element from the snakeObj.snakeBody. And here is result http://codepen.io/Kuzyo/pen/pamzC

    #145680
    Kuzyo
    Participant
    #145851
    Kuzyo
    Participant

    Thanks @CrocoDillon, you very help me. Here is final result, but I failed to detect collision with snake body http://codepen.io/Kuzyo/full/pamzC

    #146000
    Kuzyo
    Participant

    Want to ask you about structure of your JS code, how do you structure your code? Do you have special pattern?. After writing something more complex my code become more messy. I tried to find more info about organazing JS code. I read this article of Chris https://css-tricks.com/how-do-you-structure-javascript-the-module-pattern-edition/ . Very interesting idea about settings object where you “cached” all important DOM elements. But I don’t really understood ( maybe you could explain )- I need only one such settings object for whole game (in context of Snake game) or it better to have such “setting” object for every big object in a game?

Viewing 7 posts - 1 through 7 (of 7 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.