Workshop - Z4 & ZK games

Z4 is a multiplayer online real-time game development framework. This article will introduce how to use the z4 framework through a specific small game

Here is more Z4 introduction.

Z4 is suitable for low-latency, high-performance games, such as FPS. The core idea of ​​Z4 is off-chain games, zk proofs, and on-chain verification.

Using the Z4 development framework, developers do not need smart contracts, no on-chain and off-chain interactions, and no network communication between the server and the client. They only need to design and implement the core logic of the game and the game client to get a high-performance, low-latency game on web3. Players do not need to pay gas, there is no wallet interaction, no block time, and a truly smooth gaming experience.

Next, take a Z4 tour, we will build a shoot game with Z4!

Step 0: Setup coding environment

Z4 is develped in Rust, so we need install rust and cargo firstly. It is recommended to use rustup.

# install rustup
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

# use stable rustc
rustup default stable

# install z4 command
cargo install z4

# generate new game with z4 command
z4 new --name my-game

cd my-game

In the demo project, we will see z4 in dependency. z4-engine = "0.1"

Step 1: Deploy standard Z4 game contract

We have two options:

  • we can use the standard game contract that comes with Z4 and deploy it directly through scripts.

Then, we will get the game contract address in terminal output.

  • we can also do some custom development based on the Z4 standard game contract. Contract is here. And now the default contracts will at contracts/ and only the solidity files, because solidity is default, you can choose other smart contract template when new a game project.

Step 2: Game logic with Z4 trait

Firstly, we defined the Shoot game basic structs. The player has hp and bullet. and everyone has 5 hp and 5 bullet when starting.

As you see, we also collected all operations when happened in the game for do zkp.

Next, we can implement the main trait provided by Z4 engine.

Step 3: ZK game logic

Here we use the standard plonk scheme and cs provided by zypher to write zk circuits, because it has complete on-chain support. Of course, it also supports any other zk schemes to write logic circuits. The detailed circuit code can be found in z4/engine/examples.

After we finished writing the circuit, we found that it is actually very difficult to write zk circuits manually, which is not conducive to game developers to develop complex game logic. So, we provide a set of support for zkvm, including usage and integration, and also provide on-chain verification contracts for common zkvm, such as risc-v zkvm of risc0, so that developers do not need to worry about and deal with any zk knowledge. We will use games to demonstrate it later.

Step 4: Running Z4 node

After we have completed the logic of the game, we need to run it. Configure some parameters in .env file, if not exists, copy .env-template. in .env:

we need change games contract address, z4 node account secret key, and other configures. Next we run z4!

In this way, a completed z4 node will be running. When a new room created on the chain and is ready, the node will automatically accept the order and inform the players. The player program will automatically connect and play the game. When the game is over, the proof and result will be automatically submitted to the chain by the z4 node, and the whole process will be guaranteed by zk for security.

Step 5: Game front-end with Z4 node

The game front end will do two functions.

One is when the player clicks create room and join room, tx is sent to the chain and listening the status of the room. When the room is accepted, a websocket connection is made according to the z4 node information.

The other is to interact with the z4 node for a websocket connection. The interaction process uses the jsonrpc format. After the connection is completed, the connect method need be called. The parameters of this function are formulated according to the game online logic. If the online method of the Handler is not implemented, it is empty, and then the game interaction is carried out.

Tip: If you want to get a list of all currently created and ongoing rooms, you can get it from any z4 node through the room_market method.

Step 6: Play and more ecological libraries

Now we know how to develop a blockchain-based decentralized game using the z4 framework. We will continue to add more component integrations and development kits.

We have currently implemented the front-end, back-end and on-chain game development in the rust environment. For the front-end, we provide the bevy-web3 and z4-bevy libraries, which encapsulate the BevyEngine.

For more game engine and front-end toolkits, please check z4 documentation.

Last updated

Was this helpful?