Pet nurturing game developed by NAS Nebula Chain DAPP
This post was synchronized from Wing’s local tavern
In the last article, we developed our first Demo level DAPP and received an official NAS award. As more and more applications are submitted, the review process has increased, so demo level applications have become difficult to pass.
So I developed a blockchain game with my friends to help me better learn blockchain.
The idea is to do a pet get into the game, has been released address is as follows: chicken god everything down http://inspoy.cc/dapp/pet/, welcome to play ~, can enjoy yourself a bit, together to share the prize pool of NAS.
If you don’t have NAS, send your wallet address in the comments and I’ll transfer you to 0.011NAS (enough to raise chickens and buy double bonus cards) to help you experience blockchain and deploy your own contract.
demand
The main requirements are listed as follows:
- Pets have satiety and can be fed.
- Pets have mood value and can play with pets.
- Pet has experience value, experience value full can become god.
- The system has a points system, playing, becoming a god, feeding all get points.
- Pets will die if they are fed too low, and points will be deducted for death.
- The system has leaderboards, which are ranked by points.
- The system has a double scorecard. The coins purchased by the double scorecard will enter the prize pool and be distributed to the top ten in the list if they meet a certain amount.
Originally wanted to do the most simple pet game, did not expect the demand is so complex. Let’s take a look at the final product
Haha, because my friends and I have not been in contact with the front end, so the interface is relatively simple, but also relatively ugly, do not laugh ~
After the requirement analysis, let’s talk about the division of labor. My friend is responsible for the front end. Since I haven’t had any experience with it, I choose Bootstrap +jQuery as before. The contract is JS.
Using the tutorial
In view of the possibility that some partners do not understand how to use, here write a simple tutorial. Start by installing a Chrome wallet plugin: github.com/ChengOrange…
- Click to download the ZIP package or clone it.
- Open the Chrome menu with more tools – extensions.
- Open developer mode in the upper right corner.
- Click load unzipped gap to download the import.
At this point, you can see that the plugin is ready.
Click New Wallet to save your wallet file and password. The generated n1xxXXXX. json file name (without JSON) represents your wallet address.
At this time, open the chicken god cultivation record, input your wallet address, click login, there will be a window like this:
Click generate trade to get one of your chickens ~~
Sometimes the message “Nonce” must be larger than “Nonce” or “Nonce” must be larger than “Nonce” or “Nonce” must be larger than “Nonce” or “Nonce”.
In addition, logging in consumes some GAS, usually 1000000. 1NAS = 10^18 GAS, so you only need 0.01 NAS to trade many times.
If you don’t have NAS, send your wallet address in the comments and I’ll transfer you to 0.011NAS (enough to raise chickens and buy double bonus cards) to help you experience blockchain and deploy your own contract.
After that, all you need to do is feed and play, and when the score is in the leaderboard and the purchase amount of double scorecard is greater than 0.5NAS, these NAS will be distributed to the leaderboard
implementation
The data structure
Firstly, we need to think about how to store data. Since NAS itself provides map storage, we need to define a map storage space for storing game data. Key is the user address and value is the user-defined game data.
Let’s think about what the game data contains, because pets have attributes like mood, satiety, experience, and so on. In addition, time limits should be set for both play and feeding to prevent unlimited feeding, so the last feeding and play time should be included. Including scores, scores, double time, etc. :
The data structure is defined. Then complete the various actions.
Query data
The first step is to adopt a pet, read whether there is game data in the blockchain, and if there is no game data, generate a data:
There are some problem is now I don’t know how to execute code regularly, so I can only at the time of pet information query, get a logged in, to store the game data in the query, such as feed every minute to fall, so the query for the first time, gas consumption, there will be a full degree of the effect of the facts on the decline, is actually the user login, Dynamic based on time. Here, the decrease of satiety is calculated first. If the decrease exceeds a certain amount, the chick dies:
Second, update the game state, which stores data, and reset the mood at 12 o ‘clock every day:
The saveGameData method stores game data in a map type:
behavior
play
Next, a series of behaviors need to be performed, starting with play. There are several main points of play. The first point is the judgment of the interval between play, so the calculation is based on the interval between play and the last time we played. In addition, if you are in a double score card, the score will be *2, the maximum mood is 1.
Mood increases proportionally each time.
Hurl food
Feeding is similar to playing, the main difference is that when feeding, chickens can become gods, here to judge the experience value can be:
Buy a double bonus card
In fact, the double credit card is also a time attribute. After the user pays, he can increase the corresponding time. One thing to note is that you need to determine how much the user is paying, and if not, reject the transaction. In addition, we cannot know the current balance of the contract inside the contract, so we need to maintain it by ourselves. Here the NAS official uses the BigNumber library to calculate large numbers.
Judge whether the balance of the prize pool is greater than 0.5NAS each time you receive payment. If it is, the bonus pool will be 10 cents forward and the ranking data will be cleared.
Get leaderboard data
This is special because the official map does not provide traversal. Therefore, only another map can be used to store the user address corresponding to index. When traversing, first obtain the address from the address map and then retrieve the data from the data map:
The complete contract code is now complete.
Next up is the front end
The core of the front end is to call the block data according to the SDK. First, encapsulate two methods, one of which needs to be paid to write data functions. One is a query data function that does not require payment:
When the page is initialized, make explicit and implicit controls on the UI and only query the number of people online:
landing
Click the login button to query the pet information data, and after login, refresh the game data to the contract rotation training:
Eat & play
Click the corresponding button to call the corresponding interface.
Feeding button:
Play button:
Purchase double bonus card:
So, the basic interface is in place: here’s a basic introduction to play:
That’s the end of this article ~ the second DAPP of my life is complete.
If you find it helpful, you can like it and share it
And very suggest you to play the chicken form “god, can obtain the NAS oh ~ is brothers, chicken to chop down me ~ god everything down address: http://inspoy.cc/dapp/pet/
If you don’t have NAS, send your wallet address in the comments and I’ll transfer you to 0.011NAS (enough to raise chickens and buy double bonus cards) to help you experience blockchain and deploy your own contract.
Welcome to join qq group to discuss blockchain technology: 615075629