Home › Forums › JavaScript › Snake game ( separate snake body )
- This topic is empty.
-
AuthorPosts
-
August 3, 2013 at 7:16 am #145581KuzyoParticipant
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
August 3, 2013 at 11:14 pm #145628KuzyoParticipantOk, 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.
August 4, 2013 at 6:54 am #145639KuzyoParticipantNow 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--) { };
August 4, 2013 at 2:13 pm #145660KuzyoParticipantThe last result. I separated snakeHead from snakeBody( contains divs which will go after the snakeHead). And now every step coords of snakeHead goes to
gameObj.pastCoordarray. I' m trying to make that every step elements from
snakeObj.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
August 5, 2013 at 2:20 am #145680KuzyoParticipantWooooow ))) I feel almost in this way http://funnyasduck.net/wp-content/uploads/2012/11/funny-computer-programmer-designer-code-work-doesnt-no-idea-pics.jpg
August 6, 2013 at 8:45 am #145851KuzyoParticipantThanks @CrocoDillon, you very help me. Here is final result, but I failed to detect collision with snake body http://codepen.io/Kuzyo/full/pamzC
August 7, 2013 at 8:42 am #146000KuzyoParticipantWant 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?
-
AuthorPosts
- The forum ‘JavaScript’ is closed to new topics and replies.