Why is my snake not increasing after eating food?

  • Context: JavaScript 
  • Thread starter Thread starter shivajikobardan
  • Start date Start date
  • Tags Tags
    Food Increasing
Click For Summary
SUMMARY

The issue of the snake not increasing in size after eating food was resolved by adding a CSS class to the snake element. The relevant code snippet used for updating the snake's position is snakeArr.unshift({ x: snakeArr[0].x + inputDir.x, y: snakeArr[0].y + inputDir.y }). The sound effects and food relocation functionalities were confirmed to be operational, although sound playback does not function on CodePen. The primary focus was on ensuring the visual representation of the snake was correctly updated through CSS.

PREREQUISITES
  • JavaScript array manipulation
  • Understanding of CSS class application
  • Basic knowledge of game development concepts
  • Familiarity with event handling in JavaScript
NEXT STEPS
  • Investigate JavaScript array methods for game state management
  • Learn about CSS transitions and animations for dynamic elements
  • Explore debugging techniques in JavaScript for game development
  • Research sound integration in web-based games using JavaScript
USEFUL FOR

Game developers, JavaScript programmers, and anyone interested in enhancing visual feedback in web-based games.

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.
 
  • Like
Likes   Reactions: jedishrfu

Similar threads

  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · 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