[Unity3D development of small 2D Games Bejeweled game tutorial

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/q764424567/article/details/100543508

I. Introduction

Let's do a gem-style game in Unity. For those who have never heard of it, Bejeweled is a three turn-based game, released in 2001, was later copied for the popular candy crushing games. (This also makes it crush candy is a tutorial Unity).

We will focus on the core mechanics of the game. We will implement a 8x8 grid of gems, you can be interchanged. The goal is always to match at least three gems of the same type, either horizontal or vertical.

Games will be achieved with less than 140 lines of code. As usual, everything will be explained as simply as possible, so that everyone can understand it.

Second, Download

Picture resource:
Source file:

Third, the text

Note: Unity version 5.0.0f4

1, the camera adjustment

If we choose the main camera in the hierarchy and then we can set the background color to black, adjust the size of the position as shown below:
Here Insert Picture Description

2, precious stones

Here Insert Picture Description
Imported into our project, Sprites folder:
Here Insert Picture Description
then modify the settings:
Here Insert Picture Description
Note: Pixels Per Unit: 16 16x16 pixels which means a unit that will fit in the game world. We will refer to this value for all of our gems. Other values, such as Filter Mode primarily to balance the compression and quality.

Creating game objects
will stones dragged into the scene:
Here Insert Picture Description
Note: The image we project area gem just a picture, nothing more. But once we put it onto the scene, it becomes a game object that has a position, a rotation, a scale, a renderer and various other components we can add to them.

创建Gem.cs脚本
Here Insert Picture Description
将脚本拖入到Scripts文件夹中(要养成规范分类的习惯):
Here Insert Picture Description
编辑Gem.cs脚本:

using UnityEngine;
using System.Collections;

public class Gem : MonoBehaviour {

    // Use this for initialization
    void Start () {
    
    }
    
    // Update is called once per frame
    void Update () {
    
    }
}

我们不需要启动函数,所以让我们删除它:

using UnityEngine;
using System.Collections;

public class Gem : MonoBehaviour {
    
    // Update is called once per frame
    void Update () {
    
    }
}

让我们添加一个函数,让我们知道两个GEM是否是同一类型的。如果我们能把两个蓝宝石和==,因为它们是不同的游戏对象,所以它们永远不会是平等的。我们只想知道它们是否属于同一种宝石类型,或者换句话说,它们是否使用相同的Sprites:

public bool sameType(Gem other) {
    return GetComponent<SpriteRenderer>().sprite == other.GetComponent<SpriteRenderer>().sprite;
}

注意:如果这还不清楚,想象两颗宝石在一排。如果我们能把它们和==那他们就是不平等因为它们是不同的游戏对象,在不同的位置等等。然而,如果我们将它们与我们的同类型功能,那么它们将是平等如果它们都是蓝色的宝石,或者都是红色的宝石。他们会不平等如果其中一个是蓝色另一个是红色…当一条线上的三颗宝石相等时,它们就是匹配的,可以从游戏中移除。

创建宝石的预制体

将宝石拖入到我们的Project面板中的Prefabs文件夹中:
Here Insert Picture Description
然后,我们就可以在场景中删除blue对象了,因为我们可以在脚本中实例化对象

红宝石
Here Insert Picture Description
设置属性:
Here Insert Picture Description
然后也把它拖入到场景中,添加Gem.cs脚本,并且设置成预制体:
Here Insert Picture Description
Here Insert Picture Description
其他宝石
Here Insert Picture DescriptionHere Insert Picture DescriptionHere Insert Picture DescriptionHere Insert Picture DescriptionHere Insert Picture Description
我们将其他的宝石也重复这个流程做一遍:
Here Insert Picture Description

3、网格

让我们添加一个新脚本叫做Grid.cs,挂载到Main Camera物体上
Here Insert Picture Description
将Grid.cs脚本拖入到Scripts文件夹中(规范分类啊喂!):
Here Insert Picture Description
让我们编辑Grid.cs脚本:

using UnityEngine;
using System.Collections;

public class Grid : MonoBehaviour {

    // Use this for initialization
    void Start () {
    
    }
    
    // Update is called once per frame
    void Update () {
    
    }
}

我们可以删除启动函数,因为我们不需要它:

using UnityEngine;
using System.Collections;

public class Grid : MonoBehaviour {
    
    // Update is called once per frame
    void Update () {
    
    }
}

网格尺寸
让我们添加一个宽度和高度变量以控制水平和垂直宝石的数量:

using UnityEngine;
using System.Collections;

public class Grid : MonoBehaviour {
    // Grid size
    public static int w = 8;
    public static int h = 8;
    
    // Update is called once per frame
    void Update () {
    
    }
}

Note: w h means the average width and height. We will use 8 In both cases, we finally got an 8 × 8 grid. We made two public static variables so you can access them from any other script Grid.w in the following ways or Grid.h

To be continued. . .

Guess you like

Origin blog.csdn.net/q764424567/article/details/100543508