在我的游戏中添加cookie

Adding cookies to my game

本文关键字:添加 cookie 游戏 我的      更新时间:2023-09-26

所以,我目前正在创建一个类似cookie点击器的游戏。我需要在JavaScript代码中添加cookie,这样在刷新页面时就不会丢失所有cookie。下面的JavaScript代码。

var cookies = 0;
function cookieClick(number){
cookies = cookies + number;
document.getElementById("cookies").innerHTML = cookies;
};
var cursors = 0;
function buyCursor(){
var cursorCost = Math.floor(10 * Math.pow(1.1,cursors));     //works out the cost of this cursor
    if(cookies >= cursorCost){                                   //checks that the player can afford the cursor
        cursors = cursors + 1;                                   //increases number of     cursors
        cookies = cookies - cursorCost;                          //removes the cookies spent
        document.getElementById('cursors').innerHTML = cursors;  //updates the number of cursors for the user
        document.getElementById('cookies').innerHTML = cookies;  //updates the number of cookies for the user
    };
var nextCost = Math.floor(10 * Math.pow(1.1,cursors));       //works out the cost of the next cursor
document.getElementById('cursorCost').innerHTML = nextCost;  //updates the cursor cost for the user
}; 
var grandmas = 0;
function buyGrandma(){
var grandmaCost = Math.floor(100 * Math.pow(1.1,grandmas));     //works out the cost of this grandma
if(cookies >= grandmaCost){                                   //checks that the player can afford the grandma
    grandmas = grandmas + 1;                                   //increases number of grandmas
    cookies = cookies - grandmaCost;                          //removes the cookies spent
    document.getElementById('grandmas').innerHTML = grandmas;  //updates the number of grandmas for the user
    document.getElementById('cookies').innerHTML = cookies;  //updates the number of cookies for the user
};
var nextCost = Math.floor(100 * Math.pow(1.1,grandmas));       //works out the cost of the next grandma
document.getElementById('grandmaCost').innerHTML = nextCost;  //updates the grandma cost for the user
};
window.setInterval(function(){
    cookieClick(cursors);
}, 1000);
window.setInterval(function(){
    cookieClick(grandmas);
}, 400);

下面的HTML代码。

<html>
<head>
</head>
<body>
<center>
    <img src="http://hydra-media.cursecdn.com/minecraft.gamepedia.com/7/70/Cookie.png?version=3afa70a9c4eea17707300251844f3c1c" onclick="cookieClick(1)" />
    <br />
    Cookies: <span id="cookies">0</span>
    <br />
    <br />
    <button onclick="buyCursor()">Buy Cursor</button>Cost: <span id="cursorCost">10</span>
    <br />
    Cursors: <span id="cursors">0</span><img src="http://png-4.findicons.com/files/icons/2566/horizon/16/cursor.png" />
    <br />
    <br />
    <button onclick="buyGrandma()" title="Gives 4 cookies every second">Buy Grandma</button>Cost: <span id="grandmaCost">100</span>
    <br />
    Grandmas: <span id="grandmas">0</span><img src="http://img3.wikia.nocookie.net/__cb20130710043044/pokemontowerdefensetwo/images/1/18/Grandmum_icon.png" style="width:16px; height:20px;" />
    <br />
    <script type="text/javascript" src="main.js"></script>
<center>
</body>
</html>

请考虑改用localStorage

window.localStorage.numberOfCookiesCollected = 123456;

重新加载页面

alert(window.localStorage.numberOfCookiesCollected); // 123456