Active3 years, 11 months ago

I'm really new to Javascript, so I decided to create a simple SnakeGame to embed in HTML. However, my code for changing the snake's direction freezes up after a few turns.

More patorjk.com apps - source code. JavaScript Snake. Use the arrow keys on your keyboard to play the game. On Windows, press F11 to play in Full Screen mode. JavaScript Snake.

Jun 05, 2019  -To Download Snake Game In JavaScript With Source Code for free (Scroll down) This Snake Game is a single-player game. Here, the player has to control the square shaped box (termed as a snake) on a bordered plane. Rather than other snake games, the main objective of this snake game is to escape from the round dot balls in order to stay alive. Oct 15, 2017  Learn awesome tutorial how to make classic JavaScript snake game in 2018 using HTML, CSS and vanilla JavaScript that supports mobile devices, grab a source code on GitHub and play the game.

Note: I'm running this in an HTML Canvas.

Source:

Snake Game Javascript Code

Thanks

michael99manmichael99man

1 Answer

As Evan said, the main issue is how you are handling pending directions.

The issue occurs when you turn twice in rapid succession, which causes two pending directions to be added for the same block. If these aren't handled in the correct order, then the blocks may move in the wrong direction. On every update, only one pending direction for each block is needed, so I redesigned how this is handled to avoid multiple directions on one block during a single update.

Here is the link to it: http://jsbin.com/EkOSOre/5/edit

Furthermore, installing the wrong Sony drivers can make these problems even worse.Recommendation: If you are inexperienced with updating Sony device drivers manually, we highly recommend downloading the. Maintaining updated Sony Xperia™ software prevents crashes and maximizes hardware and system performance. Sony xperia downloads. Using outdated or corrupt Sony Xperia™ drivers can cause system errors, crashes, and cause your computer or hardware to fail. Manufacturer:SonyHardware Type:Mobile PhoneModel:Xperia™Compatibility:Windows XP, Vista, 7, 8, 10Downloads:85,124,475Download Size:3.4 MBDatabase Update:Available Using DriverDoc:Optional Offer for DriverDoc by Solvusoft This page contains information about installing the latest Sony Xperia™ driver downloads using the.Sony Xperia™ drivers are tiny programs that enable your Mobile Phone hardware to communicate with your operating system software.

Notice, when a change in direction is made, the pending direction on the first block is updated, overwriting any existing pending direction.

Then, when an update occurs, the list of blocks is looped through, and the pending direction of the next block is set to be the current direction of the current block.

If the current block has a pending direction, set the direction to the pending direction.

Then update the block locations like normal.

Javascript Snake Tutorial

You also had various other issues, such as using a variable (b) before it was initialized, and how you caught the null/undefined error (you should just do a check for that situation and handle it appropriately), but this was the main issue with your algorithm.

You'll also want to remove the old blocks when the user hits 'n', because the old one is left, increasing the speed and number of total blocks present.

Good luck with the rest of the game, and good luck learning JavaScript.

forgivenson

How To Make A Game Using Javascript

Snake Game Javascript Code

Snake Game Javascript Source Code

forgivenson
3,9892 gold badges15 silver badges28 bronze badges

Javascript Snake Code Complete

Not the answer you're looking for? Browse other questions tagged javascript or ask your own question.