我目前正在用Flask做一个应用程序,我遇到了一些问题。因此,目前这款应用是一个非常简单的在线点击游戏,你可以点击屏幕中心的按钮。
我还实现了一个系统,在这个系统中,浏览器跟踪用户点击中央按钮的次数。以下是游戏主屏幕的完整HTML代码:
function clickCounter() {
if (typeof(Storage) !== "undefined") {
if (localStorage.clickcount) {
localStorage.clickcount = Number(localStorage.clickcount) + 1;
} else {
localStorage.clickcount = 1;
}
document.getElementById("result").innerHTML = "You have currently gained " + localStorage.clickcount + " clicks stored on this browser.";
} else {
document.getElementById("result").innerHTML = "Your browser does not support web storage... (clicks: UNRESOLVED)";
}
}/* clicking_game style */
body {
background-color: lightblue;
}
h1 {
color: navy;
margin-left: 400;
font-family: Cursive;
}
div {
margin-left: 360;
position: fixed;
font-family: Cursive;
}
button {
margin-left: 470;
display: inline-block;
padding: 15px 25px;
font-size: 50px;
cursor: pointer;
text-align: center;
text-decoration: none;
outline: none;
color: #fff;
background-color: lime;
border: none;
border-radius: 15px;
box-shadow: 0 9px #999;
}
button:hover {
background-color: yellow;
}
button:active {
background-color: red;
box-shadow: 0 5px #666;
transform: translateY(4px);
}
img {
position: static;
}<!-- HTML5 Main -->
<html>
<!-- Head -->
<head>
<!-- Title -->
<title>Clicker Game</title>
<h1>The Clicking Game</h1>
</head>
<body>
<!-- Image Placements -->
<img src="../static/game_screen_img/arrow1.png" style="width:100px;height:100px;margin-left: 300">
<img src="../static/game_screen_img/arrow2.png" style="width:100px;height:100px;margin-left: 300">
<img src="../static/game_screen_img/salutation.png" style="width:100px;height:200px;float:right;margin:-8px;">
<img src="../static/game_screen_img/stunned.png" style="width:80px;height:120px;margin-left:-300;margin-bottom: -30px">
<img src="../static/game_screen_img/upsidedown.png" style="width:70px;height:120px;margin-left: -610;margin-bottom: -350">
<!-- Using Scripts -->
<p><button onclick="clickCounter()" type="button">Click</button></p>
<div id="result"></div>
</body>
</html>
现在,我想要添加的是一个排行榜,它显示了点击次数最多的前8个人。在排行榜的第九位,会有你和你的个人分数。我也希望排行榜在页面的左上角。如何使用JavaScript添加这种交互性并将其实现到我的HTML代码中?
为了澄清,这里有一张我希望排行榜在页面上的图片:

发布于 2021-01-18 05:46:15
现在,您将所有点击都存储在localStorage中,但这只会保存本地用户在其本地设备上的点击。为了有一个排行榜,你将需要实现一个后端与某种数据库来存储用户和他们的点击量。如果你熟悉像NodeJS或PHP和SQL这样的后端语言,那么你可以从中学到一些东西,或者如果你更喜欢将其全部保存在JavaScript中,我知道Google Firebase实时数据库或Firebase Firestore都是可行的选择,它们都可以与JavaScript一起工作,并且只需要很少的后端知识。
在这两种情况下,您需要做的是收集每个用户的唯一标识符(如姓名或电子邮件),然后存储他们的点击次数。
既然你刚刚在评论中说你想让它出现在一台设备上,我再次建议你收集一段识别信息,比如名字,这样你就可以把它和他们的点击计数一起存储在localStorage中,当他们回到网站时,他们只需输入相同的信息,然后从你离开的地方继续。但这只能在一个人的设备上工作。
https://stackoverflow.com/questions/65765613
复制相似问题