Is there a way for me to reset my game using tabs, jQuery, and game elements?

Could someone assist me with creating an invisible background color that appears after 10 seconds of losing a game? Also, I need to add a "Restart" button that resets the game.

I have tried researching online for ways to implement the restart function, but it seems complicated. Can someone explain a simple method as I am just a student and prefer straightforward solutions?

const width = $(document).width() - 200 // window html width 1366
const height = $(document).height() - 200 // window html height 600
let timer = 10
let points = 0
let isClick = false
let intervalID

$('.item-1').click(function () {
    setRandomPosition()
    $('.points').text(points)
    points += 10
   if(points == 100) { 
        endGame('You Win')
        clearInterval(intervalID)
    }
})
let intervalID = setInterval(function() {
    if(timer > 0) {
        timer--
        $('.timer').text(timer)
    } else {
        endGame('You Lose')    
    }
}, 1000)

function setRandomPosition() {
    $('.item-1').css({
        'top' : Math.floor(Math.random() * height),
        'left' : Math.floor(Math.random() * width)
    })
}

function endGame(endText) {
    $('.end').css('display', 'flex')
    $('h1').text(endText)
}
* {
    margin: 0;
    padding: 0;
}
.item {
    width: 200px;
    aspect-ratio: 1/1;
    background-color: lightpink;
    position: absolute;
}
.timer {
    font-size: 65px;
    text-align: center;
    position: absolute;
    left: 0;
    right: 0;
    z-index: 1;
}
.points {
    font-size: 65px;
    position: absolute;
    right: 20px;
    z-index: 1;
}
.end {
    position: absolute;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.497);
    align-items: center;
    justify-content: center;
}
.restart {
    
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Game</title>
    <link rel="stylesheet" href="./style.css">
</head>
<body>
    <div class="item item-1"></div>
    <p class="timer">10</p>
    <p class="points">0</p>
    <div class="end">
        <h1></h1>
    </div>
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
    <script src="./script.js"></script>
</body>
</html>

Answer №1

If you're unsure how to reset the game, here's an example:

$('.end h1').click(function() {
  points = 0;
  $('.points').text(points);
  timer = 10;
  intervalID = setInterval(_timer, 1000)
});

I've made a slight adjustment to your code layout as well.

Check out the demo below:

const width = $(document).width(); - 200 // window width html 1366
const height = $(document).height(); - 200 // window height html 600
let timer = 10;
let points = 0;
let isClick = false;
let intervalID;

// ... (remaining JS code) ...

function endGame(endText) {
  $('.end').css('display', 'flex')
  $('h1').text(endText)
}
* {
  margin: 0;
  padding: 0;
}

.item {
  width: 200px;
  aspect-ratio: 1/1;
  background-color: lightpink;
  position: absolute;
}

.timer {
  font-size: 65px;
  text-align: center;
  position: absolute;
  left: 0;
  right: 0;
  z-index: 1;
}

.points {
  font-size: 65px;
  position: absolute;
  right: 20px;
  z-index: 1;
}

.end {
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.497);
  align-items: center;
  justify-content: center;
}

.restart {}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8>
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Game</title>
  <link rel="stylesheet" href="./style.css">
</head>

<body>
  <div class="item item-1"></div>
  <p class="timer">10</p>
  <p class="points">0</p>
  <div class="end">
    <h1></h1>
  </div>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
</body>

</html>

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

AngularJS displays true for $scope.form.$submitted

I am working on a multi-step form and I am facing an issue with controlling user submissions. I need to validate each step before proceeding to the next one. Even though I am implementing validation checks and moving to the next step accordingly, the $scop ...

Sorting and preserving the filter settings in a table after it is refreshed

I'm working with a table of data from a database and I want to be able to filter it (based on one column) using checkboxes without having to reload the page, while also saving the filter status if the user refreshes the page. When a checkbox is chec ...

What is the best way to incorporate new elements into the DOM in order to allow users to share their comments

Can anyone help me with the code below? I have a text box and a comment section, along with a button to add comments. However, I need assistance with adding the posted comment below the comment section. Below is the code snippet: <div id="comments"&g ...

Error message in ASP.Net MVC when using JQuery: Receiving an undefined return

How can I modify my .Net MVC controller to successfully return a value using an SQL query that involves an Email field? Currently, the email is being read by the query but the resulting value is not being transferred over to the View with Json. Here is th ...

After making the request, $.getJSON initially comes back as undefined but eventually delivers

I found myself in a sticky situation. I was working on coding a Wikipedia search tool for a personal project, but encountered a small glitch. When a user types a word into the search bar, it gets stored in the data parameter of $.getJSON. The response then ...

Tips for encapsulating an image generated using execCommand

<input type="button" onclick="document.execCommand('insertimage',null,'http://barwoncopiers.com/wp-content/uploads/2011/03/linkedin-icon-45x45.jpg');" value="Img"/> <br> <div contenteditable="true" class="editor" id="edit ...

Can you attach a jQuery function to multiple CSS selectors at once?

Is it feasible to bind a mouseout call to two CSS selectors in order to trigger another action when the mouse moves away from both elements simultaneously? ...

Unable to interact with button within a clickable div

Currently, I am encountering an issue with Chakra UI in React where the functionality of a Box component is being executed instead of a Button when clicked inside it. Despite trying to adjust the z-index, the problem persists. Here's my code snippet: ...

create a hexagon-shaped video player

Does anyone know of a video player that can take on unconventional shapes like a hexagon or other unique designs? YouTube doesn't seem to offer this feature - is there a Flash or HTML 5 player out there that can accommodate customized video shapes? ...

Updating CSS for dropdown menu in Fluent/Fabric design

I am working with a dropdown component from Fluent UI and I am trying to customize the CSS of the dropdown options. Although I can add classes using className to the dropdown itself, I am facing difficulty in styling the dropdown options directly due to th ...

Generating pages dynamically based on the number of divs

So, I've been using template literals to create and append new divs, but now I want to take it a step further. Is there a way to automatically generate a 'next page' when a certain number of divs have been appended in HTML & JS? Appreciate ...

Manipulate table rows in real-time using jQuery to effortlessly add or remove rows

UPDATE: After setting a goal to delete rows, I am now facing an issue with recreating a deletion button in the row previous to the one that was just deleted. Here is the code snippet: HTML: <div id="container"> <img id="header" src="images/ ...

Is there a way to extract information from the HTML source code of an external website and display it in my application?

Seeking a method to search an external URL for content that matches "title" and then display the results on my HTML page using Javascript. Despite my efforts with Javascript, I have not come across any resources that address this issue. Could it be that I ...

Patience is key when letting AJAX calls complete their execution in jQuery functions

In each of my 3 functions, I have a synchronous AJAX call. Here is an example: function() { a(); b(); c(); } a() { ajaxGet(globals.servicePath + '/Demo.svc/GetDemoList/' + sessionStorage.SessionId,function(data, success) {}, '&apos ...

Pass the AngularJS object to a different MVC Controller when a button is clicked in the MVC view

When a button is clicked on the view, I need to pass an AngularJs object to another controller. CHTML <div ng-repeat="hotel in respData.hotels"> <button type="button" class="btn" data-ng-click="setTab(hotel.code)">Check Availability</bu ...

Issues with AJAX authentication not functioning in Internet Explorer

It seems like IE is causing some trouble again. I titled this "working sporadically" because the code has worked in IE at times, but there have been instances where it hasn't worked without any changes to the code. This issue also affects one of my c ...

Updating JSON data within JavaScript

Currently, I am in the process of developing a webpage that pulls data from a json feed. However, I am looking to have it update every 30 seconds without refreshing the entire page, just refreshing the Div & Jquery elements. I have attempted various solut ...

How to mimic the "show more" feature on Twitter without using ajax?

I have a list of 50 items that I want to display 10 at a time with a "More" link at the bottom. Can someone guide me on how to achieve this effect without using ajax? You can refer to this example for reference. Your help is much appreciated! UPDATE: Tha ...

Troubleshooting the issue of JavaScript not executing on elements with a specific CSS class

I am attempting to execute a JavaScript function on each element of an ASP.NET page that is assigned a specific CSS Class. Despite my limited knowledge of JavaScript, I am unable to determine why the code is not functioning properly. The CSS Class is being ...

Is it possible to use a Wcf polling duplex service with a client that is not using Silverlight?

I've been developing an online TicTacToe game and I'm utilizing a WCF polling duplex service to make it work. However, after dedicating a whole week to research, it seems that this service is only possible for Silverlight clients. If anyone is a ...