How to Create a Simple Crypto Clicker Game

Posted By : Devashish

Jun 19, 2024

<div style="font-family: &#39;Open Sans&#39; !important"><div style="font-family: &#39;Open Sans&#39; !important"><p><span style="background-color: transparent; color: rgb(0, 0, 0); line-height: 1.5rem;">Creating a simple crypto clicker game is a fun way to learn the basics of </span><a href="https://blockchain.oodles.io/cryptocurrency-development-services/"><span style="background-color: transparent; color: rgb(0, 0, 0); line-height: 1.5rem;">crypto development</span></a><span style="background-color: transparent; color: rgb(0, 0, 0); line-height: 1.5rem;">. In this tutorial, we build a basic crypto clicker game where users can earn cryptocurrency by clicking a button. We will use HTML, CSS, and JavaScript for the front end and a simple Ethereum smart contract to handle the blockchain logic.</span></p><p>&nbsp;</p><h2><span style="background-color: transparent; color: rgb(0, 0, 0); line-height: 1.5rem;"><strong>Project Structure</strong></span></h2><p>&nbsp;</p><p><span style="background-color: transparent; color: rgb(0, 0, 0); line-height: 1.5rem;">Let&#39;s start by setting up the project structure. Create a new folder for your project and inside it, create the following files:</span></p><p>&nbsp;</p><ul><li><span style="background-color: transparent; color: rgb(24, 128, 56); line-height: 1.5rem;">index.html</span></li><li><span style="background-color: transparent; color: rgb(24, 128, 56); line-height: 1.5rem;">styles.css</span></li><li><span style="background-color: transparent; color: rgb(24, 128, 56); line-height: 1.5rem;">script.js</span></li><li><span style="background-color: transparent; color: rgb(24, 128, 56); line-height: 1.5rem;">contract.sol</span><span style="background-color: transparent; color: rgb(0, 0, 0); line-height: 1.5rem;"> (for the Ethereum smart contract)</span></li></ul><p>&nbsp;</p><h3><span style="background-color: transparent; color: rgb(0, 0, 0); line-height: 1.5rem;"><strong>Step 1: HTML Structure</strong></span></h3><p>&nbsp;</p><p><span style="background-color: transparent; color: rgb(0, 0, 0); line-height: 1.5rem;">We&#39;ll start by creating a simple HTML structure. Open&nbsp;</span><span style="background-color: transparent; color: rgb(24, 128, 56); line-height: 1.5rem;">index.html</span><span style="background-color: transparent; color: rgb(0, 0, 0); line-height: 1.5rem;"> and add the following code:</span></p><p>&nbsp;</p><pre><code class="language-plaintext">&lt;!DOCTYPE&nbsp;html&gt; &lt;html&nbsp;lang="en"&gt; &lt;head&gt; &nbsp; &lt;meta&nbsp;charset="UTF-8"&gt; &nbsp; &lt;meta&nbsp;name="viewport"&nbsp;content="width=device-width, initial-scale=1.0"&gt; &nbsp; &lt;title&gt;Crypto Clicker Game&lt;/title&gt; &nbsp; &lt;link&nbsp;rel="stylesheet"&nbsp;href="styles.css"&gt; &lt;/head&gt; &lt;body&gt; &nbsp; &lt;div&nbsp;class="game-container"&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;h1&gt;Crypto Clicker&lt;/h1&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;div&nbsp;id="crypto-account"&gt;Connect Wallet&lt;/div&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;div&nbsp;id="crypto-count"&gt;0&lt;/div&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;button&nbsp;id="click-button"&gt;Click me!&lt;/button&gt; &nbsp; &lt;/div&gt; &nbsp; &lt;script&nbsp;src="script.js"&gt;&lt;/script&gt; &lt;/body&gt; &lt;/html&gt;</code></pre><p>&nbsp;</p><p><span style="color: rgb(0, 0, 0); line-height: 1.5rem;"><strong>Also, Explore | &nbsp;</strong></span><a href="https://blockchain.oodles.io/blog/develop-a-crypto-swap-aggregator-platform/"><span style="color: rgb(0, 0, 0); line-height: 1.5rem;">How to Develop a Crypto Swap Aggregator Platform</span></a></p><p>&nbsp;</p><h3><span style="background-color: transparent; color: rgb(0, 0, 0); line-height: 1.5rem;"><strong>Step 2: Styling with CSS</strong></span></h3><p>&nbsp;</p><p><span style="background-color: transparent; color: rgb(0, 0, 0); line-height: 1.5rem;">Next, we&#39;ll add some basic styles to make our game look nicer. Open&nbsp;</span><span style="background-color: transparent; color: rgb(24, 128, 56); line-height: 1.5rem;">styles.css</span><span style="background-color: transparent; color: rgb(0, 0, 0); line-height: 1.5rem;"> and add the following code:</span></p><p>&nbsp;</p><pre><code class="language-plaintext">body { &nbsp; display:&nbsp;flex; &nbsp; justify-content:&nbsp;center; &nbsp; align-items:&nbsp;center; &nbsp; height:&nbsp;100vh; &nbsp; margin:&nbsp;0; &nbsp; font-family:&nbsp;Arial,&nbsp;sans-serif; &nbsp; background-color:&nbsp;#f0f0f0; } .game-container { &nbsp; text-align:&nbsp;center; &nbsp; background:&nbsp;white; &nbsp; padding:&nbsp;20px; &nbsp; border-radius:&nbsp;10px; &nbsp; box-shadow:&nbsp;0&nbsp;0&nbsp;10px&nbsp;rgba(0,&nbsp;0,&nbsp;0,&nbsp;0.1); &nbsp; width:300px; &nbsp; max-width:&nbsp;100%; } #crypto-account{ &nbsp; padding:8px&nbsp;12px; &nbsp; background-color:&nbsp;#f2f2f2; &nbsp; border-radius:&nbsp;2px; &nbsp; max-width:100%; &nbsp; word-break:&nbsp;break-all; } #crypto-count { &nbsp; font-size:&nbsp;2em; &nbsp; margin:&nbsp;20px&nbsp;0; } #click-button { &nbsp; padding:&nbsp;10px&nbsp;20px; &nbsp; font-size:&nbsp;1em; &nbsp; cursor:&nbsp;pointer; }</code></pre><p>&nbsp;</p><p><span style="color: rgb(0, 0, 0); line-height: 1.5rem;"><strong>You may also like |</strong></span><a href="https://blockchain.oodles.io/blog/crypto-staking-platform/"><span style="color: rgb(0, 0, 0); line-height: 1.5rem;"><strong> </strong>Staking Platform Development: A Step-by-Step Guide</span></a></p><p>&nbsp;</p><h3><span style="background-color: transparent; color: rgb(0, 0, 0); line-height: 1.5rem;"><strong>Step 3: JavaScript Functionality</strong></span></h3><p>&nbsp;</p><p><span style="background-color: transparent; color: rgb(0, 0, 0); line-height: 1.5rem;">Now, let&#39;s add the game logic. Open&nbsp;</span><span style="background-color: transparent; color: rgb(24, 128, 56); line-height: 1.5rem;">script.js</span><span style="background-color: transparent; color: rgb(0, 0, 0); line-height: 1.5rem;"> and add the following code:</span></p><p>&nbsp;</p><pre><code class="language-plaintext">document.addEventListener(&#39;DOMContentLoaded&#39;, (event)&nbsp;=&gt; { let&nbsp;count&nbsp;=&nbsp;0; const&nbsp;cryptoCountElement&nbsp;=&nbsp;document.getElementById(&#39;crypto-count&#39;); const&nbsp;clickButton&nbsp;=&nbsp;document.getElementById(&#39;click-button&#39;); clickButton.addEventListener(&#39;click&#39;, ()&nbsp;=&gt; { &nbsp;&nbsp;&nbsp; count++; &nbsp;&nbsp;&nbsp; cryptoCountElement.innerText&nbsp;=&nbsp;count; }); }); </code></pre><p>&nbsp;</p><h2><span style="background-color: transparent; color: rgb(0, 0, 0); line-height: 1.5rem;"><strong>Step 4: Ethereum Smart Contract</strong></span></h2><p>&nbsp;</p><p><span style="background-color: transparent; color: rgb(0, 0, 0); line-height: 1.5rem;">We&#39;ll create a simple Ethereum smart contract to handle the blockchain logic. Open&nbsp;</span><span style="background-color: transparent; color: rgb(24, 128, 56); line-height: 1.5rem;">contract.sol</span><span style="background-color: transparent; color: rgb(0, 0, 0); line-height: 1.5rem;"> and add the following code:</span></p><p>&nbsp;</p><pre><code class="language-plaintext">// SPDX-License-Identifier: MIT pragma&nbsp;solidity ^0.8.0; contract CryptoClicker { &nbsp; mapping(address&nbsp;=&gt;&nbsp;uint256)&nbsp;public balances; &nbsp; function&nbsp;earnCrypto()&nbsp;public { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;balances[msg.sender]&nbsp;+=&nbsp;1; &nbsp;&nbsp;} &nbsp; function&nbsp;getBalance()&nbsp;public&nbsp;view&nbsp;returns (uint256) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return balances[msg.sender]; &nbsp;&nbsp;} } </code></pre><p>&nbsp;</p><p><span style="background-color: transparent; color: rgb(0, 0, 0); line-height: 1.5rem;">This contract allows users to earn cryptocurrency by calling the&nbsp;</span><span style="background-color: transparent; color: rgb(24, 128, 56); line-height: 1.5rem;">earnCrypto</span><span style="background-color: transparent; color: rgb(0, 0, 0); line-height: 1.5rem;"> function and to check their balance using the&nbsp;</span><span style="background-color: transparent; color: rgb(24, 128, 56); line-height: 1.5rem;">getBalance</span><span style="background-color: transparent; color: rgb(0, 0, 0); line-height: 1.5rem;"> function.</span></p><p>&nbsp;</p><h3><span style="background-color: transparent; color: rgb(0, 0, 0); line-height: 1.5rem;"><strong>Step 5: Interacting with the Smart Contract</strong></span></h3><p>&nbsp;</p><p><span style="background-color: transparent; color: rgb(0, 0, 0); line-height: 1.5rem;">To interact with the Ethereum smart contract, we will use the Web3.js library. Add the following script tag to the end of your&nbsp;</span><span style="background-color: transparent; color: rgb(24, 128, 56); line-height: 1.5rem;">index.html</span><span style="background-color: transparent; color: rgb(0, 0, 0); line-height: 1.5rem;"> file to include Web3.js:</span></p><p>&nbsp;</p><pre><code class="language-plaintext">&lt;script&nbsp;src="https://cdn.jsdelivr.net/gh/ethereum/web3.js/dist/web3.min.js"&gt;&lt;/script&gt; </code></pre><p>&nbsp;</p><p><span style="background-color: transparent; color: rgb(0, 0, 0); line-height: 1.5rem;">Update your&nbsp;</span><span style="background-color: transparent; color: rgb(24, 128, 56); line-height: 1.5rem;">script.js</span><span style="background-color: transparent; color: rgb(0, 0, 0); line-height: 1.5rem;"> to include Web3.js integration:</span></p><p>&nbsp;</p><pre><code class="language-plaintext">document.addEventListener(&#39;DOMContentLoaded&#39;,&nbsp;async (event)&nbsp;=&gt; { &nbsp; let&nbsp;count&nbsp;=&nbsp;0; &nbsp; const&nbsp;cryptoCountElement&nbsp;=&nbsp;document.getElementById(&#39;crypto-count&#39;); &nbsp; const&nbsp;cryptoAccountElement&nbsp;=&nbsp;document.getElementById(&#39;crypto-account&#39;); &nbsp; const&nbsp;clickButton&nbsp;=&nbsp;document.getElementById(&#39;click-button&#39;); &nbsp; if (typeof&nbsp;web3&nbsp;!==&nbsp;&#39;undefined&#39;) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; console.log(&#39;Web3 Detected! Using provider:&#39;,&nbsp;web3.currentProvider.constructor.name); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; window.web3&nbsp;=&nbsp;new&nbsp;Web3(web3.currentProvider); &nbsp;&nbsp;}&nbsp;else { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; console.log(&#39;No Web3 Detected... using HTTP Provider&#39;); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; window.web3&nbsp;=&nbsp;new&nbsp;Web3(new&nbsp;Web3.providers.HttpProvider("http://localhost:8545"));&nbsp;// Replace with your provider URL if needed &nbsp;&nbsp;} &nbsp; console.log(&#39;web3 initiated&#39;,&nbsp;window.web3); &nbsp;&nbsp; const&nbsp;eth&nbsp;=&nbsp;window?.web3?.eth;&nbsp;// Utilize optional chaining for safety &nbsp; let&nbsp;accounts&nbsp;= []; &nbsp;&nbsp; if (!eth) { &nbsp;&nbsp;&nbsp; console.error(&#39;Web3 not available, functionality limited.&#39;); &nbsp;&nbsp;}&nbsp;else { &nbsp;&nbsp;&nbsp; accounts&nbsp;=&nbsp;await&nbsp;eth.getAccounts(); &nbsp;&nbsp;&nbsp; if (accounts.length) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cryptoAccountElement.textContent&nbsp;=&nbsp;accounts[0]; &nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cryptoAccountElement.addEventListener(&#39;click&#39;,&nbsp;async ()&nbsp;=&gt; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const&nbsp;requestedAccounts&nbsp;=&nbsp;await&nbsp;eth.requestAccounts(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (requestedAccounts.length) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cryptoAccountElement.textContent&nbsp;=&nbsp;requestedAccounts[0]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; console.warn(&#39;User denied access to accounts.&#39;); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;} &nbsp;&nbsp; // Contract initialization (replace placeholders with actual values) &nbsp; const&nbsp;contractAddress&nbsp;=&nbsp;"YOUR_CONTRACT_ADDRESS"; &nbsp; const&nbsp;abi&nbsp;= [ &nbsp;&nbsp;&nbsp; // The ABI of your smart contract &nbsp;&nbsp;]; &nbsp;&nbsp; let&nbsp;contract&nbsp;=&nbsp;null; &nbsp; if (contractAddress&nbsp;&amp;&amp;&nbsp;abi.length) { &nbsp;&nbsp;&nbsp; contract&nbsp;=&nbsp;new&nbsp;web3.eth.Contract(abi,&nbsp;contractAddress); &nbsp;&nbsp;} &nbsp;&nbsp; // Button click handler with error handling &nbsp; clickButton.addEventListener(&#39;click&#39;,&nbsp;async ()&nbsp;=&gt; { &nbsp;&nbsp;&nbsp; count++; &nbsp;&nbsp;&nbsp; cryptoCountElement.innerText&nbsp;=&nbsp;count; &nbsp;&nbsp;&nbsp;&nbsp; if (!eth&nbsp;||&nbsp;!contract) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; console.error(&#39;Web3 or contract not initialized. Functionality limited.&#39;); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp; const&nbsp;accounts&nbsp;=&nbsp;await&nbsp;eth.getAccounts(); &nbsp;&nbsp;&nbsp; if (!accounts.length) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; console.warn(&#39;No accounts found. Please connect a wallet.&#39;); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp; const&nbsp;account&nbsp;=&nbsp;accounts[0]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; contract?.methods.earnCrypto().send({&nbsp;from:&nbsp;account }) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.then(()&nbsp;=&gt; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; console.log(&#39;Crypto earned!&#39;); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.catch((error)&nbsp;=&gt; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; console.error(&#39;Error earning crypto:&#39;,&nbsp;error); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;}); });</code></pre><p>&nbsp;</p><p><span style="background-color: transparent; color: rgb(0, 0, 0); line-height: 1.5rem;">Replace&nbsp;</span><span style="background-color: transparent; color: rgb(24, 128, 56); line-height: 1.5rem;">YOUR_CONTRACT_ADDRESS</span><span style="background-color: transparent; color: rgb(0, 0, 0); line-height: 1.5rem;"> with the address of your deployed contract and include the ABI of your contract in the&nbsp;</span><span style="background-color: transparent; color: rgb(24, 128, 56); line-height: 1.5rem;">abi</span><span style="background-color: transparent; color: rgb(0, 0, 0); line-height: 1.5rem;"> array.</span></p><p>&nbsp;</p><p><span style="background-color: transparent; color: rgb(0, 0, 0); line-height: 1.5rem;"><strong>Also, Check | </strong></span><a href="https://blockchain.oodles.io/blog/crypto-launchpad/"><span style="background-color: transparent; color: rgb(0, 0, 0); line-height: 1.5rem;">Building a Crypto Launchpad: From Concept to Launch</span></a></p><p>&nbsp;</p><h2><span style="background-color: transparent; color: rgb(0, 0, 0); line-height: 1.5rem;"><strong>Conclusion</strong></span></h2><p>&nbsp;</p><p><span style="background-color: transparent; color: rgb(0, 0, 0); line-height: 1.5rem;">Congratulations! You have created a simple crypto clicker game that integrates with the Ethereum blockchain. This project covered the basics of HTML, CSS, JavaScript, and blockchain integration using Web3.js. You can expand this game by adding more features, such as upgrades, leaderboards, or additional game mechanics. Have fun coding! If you are looking for crypto development services to build and launch your crypto project, connect with our skilled </span><a href="https://blockchain.oodles.io/about-us/"><span style="background-color: transparent; color: rgb(0, 0, 0); line-height: 1.5rem;">crypto developers</span></a><span style="background-color: transparent; color: rgb(0, 0, 0); line-height: 1.5rem;"> for a quick discussion. &nbsp;</span></p></div></div>

Leave a

Comment

Name is required

Invalid Name

Comment is required

Recaptcha is required.

blog-detail

November 8, 2024 at 08:25 pm

Your comment is awaiting moderation.

By using this site, you allow our use of cookies. For more information on the cookies we use and how to delete or block them, please read our cookie notice.

Chat with Us
Telegram Button
Youtube Button
Contact Us

Oodles | Blockchain Development Company

Name is required

Please enter a valid Name

Please enter a valid Phone Number

Please remove URL from text