JavaScript Why is my snake not increasing after eating food?

  • Thread starter Thread starter shivajikobardan
  • Start date Start date
  • Tags Tags
    Food Increasing
Click For Summary
The discussion centers around a code snippet for a snake game, specifically addressing the functionality of increasing the snake's size upon colliding with food. The provided code successfully updates the snake's position and triggers sound effects when food is consumed, although sound may not function on CodePen. The main issue identified was the absence of a CSS class for the snake, which has since been resolved. The conversation emphasizes the importance of ensuring all necessary elements, including CSS, are in place for the game to function correctly.
shivajikobardan
Messages
637
Reaction score
54
TL;DR
snake not increasing after eating food.

Here's the full code.
JavaScript:
snakeArr.unshift({ x: snakeArr[0].x + inputDir.x, y: snakeArr[0].y + inputDir.y })
This is the code to increase the size of snake once it collides with food.
The code above it and below it, sound play when food is eaten and changing food location when food is eaten are working(sound play won't work on codepen).
What's the problem. Please help to fix the issue and understand it as well.
 
Technology news on Phys.org
This issue has been fixed, I forgot to add CSS class to snake.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 14 ·
Replies
14
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
63
Views
5K
  • · Replies 39 ·
2
Replies
39
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
Replies
9
Views
3K