티스토리 뷰

아래는 유니티로 간단한 지뢰찾기 게임을 구현하기 위한 예시 소스 코드입니다.

 

using UnityEngine;

public class Minefield : MonoBehaviour
{
    public int width = 10;
    public int height = 10;
    public int mineCount = 10;
    public GameObject tilePrefab;
    public GameObject minePrefab;

    private Tile[,] tiles;
    private int[,] minefield;

    private void Start()
    {
        GenerateMinefield();
        InstantiateTiles();
    }

    private void GenerateMinefield()
    {
        minefield = new int[width, height];

        // Randomly place mines
        for (int i = 0; i < mineCount; i++)
        {
            int x = Random.Range(0, width);
            int y = Random.Range(0, height);

            // Check if the tile already has a mine
            if (minefield[x, y] == -1)
            {
                // If it does, try again
                i--;
            }
            else
            {
                minefield[x, y] = -1;
            }
        }

        // Calculate the number of adjacent mines for each tile
        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                if (minefield[x, y] != -1)
                {
                    int count = 0;
                    for (int dx = -1; dx <= 1; dx++)
                    {
                        for (int dy = -1; dy <= 1; dy++)
                        {
                            int nx = x + dx;
                            int ny = y + dy;

                            if (nx >= 0 && nx < width && ny >= 0 && ny < height && minefield[nx, ny] == -1)
                            {
                                count++;
                            }
                        }
                    }
                    minefield[x, y] = count;
                }
            }
        }
    }

    private void InstantiateTiles()
    {
        tiles = new Tile[width, height];

        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                GameObject tileObj = Instantiate(tilePrefab, new Vector3(x, 0, y), Quaternion.identity);
                Tile tile = tileObj.GetComponent<Tile>();
                tile.Init(x, y, minefield[x, y]);
                tiles[x, y] = tile;
            }
        }
    }
}

위의 코드는 Minefield 스크립트로서 지뢰찾기 게임의 로직을 다루고 있습니다. width와 height 변수를 통해 지뢰찾기 보드의 크기를 설정하고, mineCount 변수로 지뢰의 개수를 설정합니다. tilePrefab은 타일의 프리팹을, minePrefab은 지뢰의 프리팹을 저장합니다.

Start() 메서드에서는 GenerateMinefield() 함수를 호출하여 지뢰찾기 보드를 생성하고, InstantiateTiles() 함수를 호출하여 타일들을 생성합니다. GenerateMinefield() 함수는 랜덤하게 지뢰를 배치하고, 주변 지뢰의 개수를 계산합니다. InstantiateTiles() 함수는 타일 게임 오브젝트를 생성하고 Tile 컴포넌트를 초기화하여 보드를 구성합니다.

Tile 스크립트는 타일의 동작을 다루는 스크립트입니다. 타일의 위치와 주변 지뢰의 개수를 저장하고, 클릭 이벤트를 처리하는 기능을 구현할 수 있습니다. 예를 들어, 타일을 클릭했을 때 주변 지뢰 개수를 표시하거나, 지뢰를 클릭했을 때 게임 오버 처리 등을 구현할 수 있습니다.

이 예시 코드는 지뢰찾기 게임의 핵심 로직을 담고 있으며, 추가적인 기능(게임 상태, 스코어, 타일 표시 등)을 구현하여 게임을 확장시킬 수 있습니다.

댓글
최근에 달린 댓글
글 보관함
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Total
Today
Yesterday
    뽀로로친구에디
    최근에 올라온 글