Chain Game Development: Zombie War

Source of this project https://cryptozombies.io/

0. Preface

Recently, I have been paying attention to the relevant information of NFT+ games, and decided to actually operate it, based on solidity and web3js, implement the erc721 protocol , and develop a small zombie war game. During the development process, I also learned about solidity and web3js.

This tweet, as the first in this series, mainly implements a zombie factory , which can randomly generate new zombies through input strings .

1. Key knowledge

1. All Solidity source code must be marked with "version pragma" - indicating the version of the Solidity compiler . In order to avoid future new compilers that may destroy your code. At the same time, the code of Solidity is wrapped in the contract. A contract is the basic module of the Ethereum application . All variables and functions belong to a contract, which is the starting point of all your applications. A simple code is as follows:

pragma solidity ^0.4.19;
contract ZombieFactory {

}

2. In solidity, state variables are permanently stored in the contract. That is to say they are written into the Ethereum blockchain. Imagine writing to a database and declaring variables (uint defaults to uint256 type). As follows

uint myUnsignedInteger = 100;

3. Solidity has a structure , which is similar in form to C language. At the same time, if you want to build a collection, you can use the array type . Solidity supports dynamic and static . Because of the high gas fee of Ethereum, dynamic arrays are commonly used, and the Push function is used to add elements to dynamic arrays . You can use public to declare public arrays, and other contracts can read data from this array (but not write data), so this is a useful pattern for storing public data in contracts.

// 固定长度为2的静态数组:
uint[2] fixedArray;
// 固定长度为5的string类型的静态数组:
string[5] stringArray;
// 动态数组,长度不固定,可以动态添加元素:
//给people动态数组添加Person元素
people.push(Person(16, "Vitalik"));
uint[] dynamicArray;
//声明公共数组
Person[] public people;

4. The syntax of function definition in Solidity is as follows:

function eatHamburgers(string _name, uint _amount) {

}

5. More knowledge about functions: The properties of functions defined by Solidity are public by default. This means that any party (or other contract) can call functions in your contract. And using the keyword private after the function name can be defined as private. Similar to function parameters, this means that only other functions in our contract can call this function . At the same time, functions also have modifiers . There are two main applications, one is view , which means it can only read data but not change data: and pure , which means that this function does not even access the data in the application. details as follows:

//公共函数、具有返回值
string greeting = "What's up dog";

function sayHello() public returns (string) {
  return greeting;
}
//定义一个具有view修饰的函数
function sayHello() public view returns (string) {

}

//定义一个具有pure修饰的函数
function sayHello() public pure returns (string) {

}

6. Hash function : The keccak256() function accepts a string and generates a 256-bit hexadecimal number. A small change in the string will cause a great change in the hash data, such as:

//6e91ec6b618bb462a4a6ee5aa2cb0e9cf30f7a052bb467b0ba58b8748c00d2e5
keccak256("aaaab");

7. Event is a mechanism for contract and blockchain communication. Your front-end application listens to certain events and reacts to them, for example:

// 这里建立事件
event IntegersAdded(uint x, uint y, uint result);

function add(uint _x, uint _y) public {
  uint result = _x + _y;
  //触发事件,通知app
  IntegersAdded(_x, _y, result);
  return result;
}

Front-end monitoring:

YourContract.IntegersAdded(function(error, result) {
  // 干些事
})

2. Code

The programming knowledge involved in the code is all in the above key knowledge. The WEB3JS code in this chapter is mainly used to call the interface for generating zombies and render the zombie model.

//每个僵尸有16位的DNA,前端根据这16位DNA进行渲染。

//设置版本
pragma solidity ^0.4.19;

contract ZombieFactory {
//设置监听事件。
      event NewZombie(uint zombieId, string name, uint dna);
//用于生成DNA
    uint dnaDigits = 16;
    uint dnaModulus = 10 ** dnaDigits;
//僵尸结构体
    struct Zombie {
        string name;
        uint dna;
    }
//设置公共可查的僵尸数组。
    Zombie[] public zombies;
    
//定义私有函数 生成僵尸。
    function _createZombie(string _name, uint _dna) private {
           uint id = zombies.push(Zombie(_name, _dna)) - 1;
         NewZombie(id, _name, _dna);
    }
//生成随机DNA
    function _generateRandomDna(string _str) private view returns (uint) {
        uint rand = uint(keccak256(_str));
        return rand % dnaModulus;
    }
    
    function createRandomZombie(string _name) public {
        uint randDna = _generateRandomDna(_name);
        _createZombie(_name, randDna);
    }

}

3. Reflection

There is still some work to be done:

  • Using remix and Ethereum wallet, you can easily debug the system on the test network. This series of attempts will be carried out in the next step.
  • The learning of WEB3JS should be strengthened.

Guess you like

Origin blog.csdn.net/doreen211/article/details/129170441