Trying my hand at a Cascading Style Sheets jQuery Snake

I'm a beginner in the realm of Javascript, and I've recently tried my hand at creating a simple classic snake game. Instead of using complex graphics, I opted for a more HTML/CSS approach where each pixel on the map is represented by a div box, and the snake's movements are depicted through background color changes using CSS animations.

You can check out my version of the game on CodePen here.

Currently, I am facing some issues with the game:
*Rare - sometimes the berry spawns on the snake's body, which shouldn't happen.
*Extremely rare - occasionally the snake dies unexpectedly when it gets longer.

There might be other issues that have slipped under my radar.

I would greatly appreciate any help, feedback, or suggestions regarding optimization or any unnecessary/overcomplicated code snippets.

//PAINT MAP
function paintMap() {

var i = 1;
while (i < 101){
    $('.map').append('<div class="box" id="loadID""></div>');
    $("#loadID").attr("id", i);
    i++;
}

console.log("Map loaded and starting move engine");
moveEngine();
}

Thank you for taking the time to review my code and provide valuable insights and assistance!

Answer №1

I found a solution by making the following adjustment:

if ($("#"+ berryPosition).css("background-color") === "rgb(0, 0, 0)"){ 
console.log("collision detected/respawn berry") call function that randomly puts berry...}

changed to

while($("#"+ berryPosition).css("background-color") === "rgb(0, 0, 0)) . 
 {console.log("collision detected/respawn berry")pick random numbers until it fits empty position for berry...}

This change now causes the berry to respawn without explicitly calling the function and while moveTimer is updated. However, I am still unsure why the function was failing sporadically in certain situations.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

JavaScript: Adjust DIV Width Based on Window Height

Is there a way to make the width of a div equal to the height of the window in jquery? I attempted this method: <script type="text/javascript"> $(".rt-container").style.width = $(window).height(); </script> Unfortunately, it did not work. I ...

Launching an app using Electron JS incorporating an Express backend and a standalone HTML frontend

I have a unique situation where my application consists of separate ExpressJS backend and HTML front-end modules. The ExpressJS backend is located at root/scripts/server.js Meanwhile, the HTML front-end is situated in root/index.html. The front-end utili ...

`Web scraping with BeautifulSoup presenting an issue with href tags`

I've been working on a web scraping project, but I'm facing challenges extracting href links from a particular webpage. The URL in question is . My goal is to gather all the article links using the href attribute. Below is an example of the link ...

How to select specific folders for packaging with asar within an Electron app

I'm currently working on an Electron application and experimenting with using asar to package the node_modules and sources directories, while excluding other directories. However, I've run into an issue where when building the application with a ...

Retrieve the text located in a specific row and column

I'm experiencing an issue where I am only receiving the entire row as an object, but what I actually need is the data from the second column in that row. $('#table_id tbody').on('click', 'tr', function (e) { var ...

"Adding a Numeric Keypad to Enhance Security in Password Fields on Devices

I have been encountering a common issue where none of the solutions I have come across seem to work for the devices I am testing. Specifically, Android, iOS, and Windows devices are not implementing the numeric keypad as expected, regardless of whether the ...

What is the most efficient approach for handling numerous transactions individually through AJAX calls?

Just a heads up: there's quite a bit of pseudo code coming up, but it's necessary to illustrate my issue. My solution involves more complex management of ajax icons and statuses, but I've simplified it for clarity. Essentially, I'm fac ...

Tips for generating a dynamic JavaScript object that contains a function as its value

I am in search of a dynamic method to generate Quasar Table column definitions. While the existing method shown below does work, I believe that replacing the lengthy switch statement with a for loop would be a more efficient solution. How can I implement ...

What methods can be used for child components to communicate in React and React-Native?

Is there a way for child components in React to directly call functions from one another, such as accessing test1() from test2() or vice versa? I already have access to both functions from the parent components through ref_to_test1 and ref_to_test2. But ...

Inject additional space into a variable in order to utilize it in HTML coding

Why isn't there any space between the arrows on my page, even though I tried adding &nbsp; within the string? @{ var hero_statement = "We grow companies from code."; var hero_message = "We offer talented teams to deliver results fro ...

Troubleshooting the Issue of Tailwind CSS Failing to Build Accurately in a Next.js Application Launched

Currently, I am working on a project that involves a Next.js frontend situated in a client directory and a Node.js backend in a server directory. The project structure resembles the following: jukerabbit/ ├─ client/ │ ├─ pages/ │ ├─ comp ...

Fade in and out of page or div elements with a simple click

I've been dedicating some time to my recruitment website lately. As a beginner in coding, I'm looking for a script that will automatically fade out the Employment review content when clicking on each employment vacancy div and then fade back in w ...

Experiencing issues with handling $.json in Ember

I've been experimenting with using $.getJSON in conjunction with Ember.js (specifically, I'm aiming to bypass Ember-Data). Below is the code snippet: App = Ember.Application.Create(); App.Model = Ember.Object.extend({ }); App.Users = App.Mode ...

Function is not triggered in React component

When the LoginPage calls AuthForm, the structure is as follows: function mapDispatchToProps(dispatch: Redux.Dispatch<any>) { return { signUpWithEmail: function(email: string, password: string) { // bla bla }, }; } handleForm ...

Unable to utilize drop-down menu for input in form

I have created this drop-down menu using HTML: <div id="mySelect" class="select btn-group m-b" data-resize="auto"> <button style="font-weight:700;background-color:#fff;border-style:solid; border-width:2px" type="button" id="expiry_month" ...

Tips for preventing flex items from having equal height when collapsing

When I try to place elements from an array in a list with display flex, the height of other elements gets modified when one is collapsed. I have attempted different display and inline placement on li elements but nothing has worked so far. Is there a way ...

algorithm for selecting the greatest value

I'm attempting to create a function that identifies the largest number in an array, but for some reason, I'm only able to retrieve the first number in the array. function findLargest(numbers) { var bigNum = 1; for(i = 0; i < numbers.len ...

Data is present in a JavaScript array, yet it is returning a value of

In my quest to create an array of User IDs for an AJAX Post Request, I encountered a strange issue. Despite successfully retrieving and displaying the User IDs individually in console.log, once I push them to the connectionData array, accessing specific in ...

Making a colorful box within a paragraph using a span element

Check out my code snippet: <p style="font-size: 12px;line-height: 24px;font-weight: normal;color: #848484;padding: 0;margin: 0;"><b>COLOR:</b> <span style="width: 15px; height: 15px; margin:auto; display: inline_block; b ...

Incrementally Increasing Array Elements with Javascript's Splice Method

I'm attempting to insert "-" before capital letters in a string. For example, transforming helloWorld into hello-world. Despite my efforts, the dashes are being placed incorrectly within the output array. For instance, changing thisIsSpinalTap to thi ...