Zephyrnet Logo

How to Make a Pokémon-Like Game With ERC-721 Tokens

Date:

First of all, let’s outline some assumptions about each Ethermon:

  • They start at level one.
  • You battle other Pokemon.
  • You gain levels by battling.

Project creation

Please note, we’re going to use the Truffle Suite in this walkthrough.

mkdir ethermon
cd ethermon/
truffle init

Use OpenZeppelin

To use OpenZeppelin, we need to import the library via npm. Let’s initialize npm, then get the correct version of OpenZeppelin. We’re using the latest stable version, which at time of writing is version 2.5.0. Make sure you have Solidity compiler version 0.5.5.

npm init
npm install @openzeppelin/contracts@2.5.0 --save

Extend ERC-721

In our contracts/ folder, let’s create a new file called Ethermon.sol. To include all of the functionality written for us in the OpenZeppelin code, we need to import and extend ERC721.sol.

Figure 1: Ethermon.sol
Figure 2: 2_deploy_contracts.js

Write Ethermon logic

We need the Ethermon contract to be able to:

  1. Assign monsters to their owners.
  2. Let owners battle their monsters against other monsters.
Figure 3: Creating new monsters

Battle logic

As mentioned earlier, our battle logic determines how many levels a monster goes up. The higher-level monster wins and goes up two levels, the loser goes up one. If they are the same level, the attacker wins. Figure 4 shows what our contract looks like when we add the code for battles.

Figure 4: Ethermon.sol with Battle function added

Source: https://medium.com/better-programming/how-to-make-a-pok%C3%A9mon-like-game-with-erc-721-tokens-704f2344626?source=rss——-8—————–cryptocurrency

spot_img

Latest Intelligence

spot_img