Unity小游戏制作

游戏简介:

        这是一个关于数字叠加的智力小游戏,游戏创意新颖,机制有趣,适合青少年锻炼数学思维。

游戏机制:

        玩家可点击5X5方框中的任意未被点击的按钮,所有按钮默认值为0。按钮被点击后值会变为1,而四周按钮也会相应加1。

       若两个斜对角的按钮值都为3,则得分加2。

       累计得分达到8分,即可通关游戏。

游戏制作代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Threading;
using System.Diagnostics;

public class ChessGame : MonoBehaviour {

    // Entities and their states / Model
    private static int count;
    private int goal;
    private int[,] chessBoard = new int[5, 5];

    int GetRandomSeed()
    {
        //Thread.Sleep(500);
        byte[] bytes = new byte[4];
        System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
        rng.GetBytes(bytes);
        return BitConverter.ToInt32(bytes, 0);        
    }
    // System Handlers
    void Start () {
        Init();
    }
    void OnGUI() {
        GUI.Box(new Rect(210, 25, 400, 500), "");//410,275
        if (GUI.Button(new Rect(360, 50, 100, 30), "Restart")) Init();
        if (!GameOver(goal)) {
            for (int i = 0; i < 5; i++) {
                for (int j = 0; j < 5; j++) {
                    if(chessBoard[i, j] == 0 && GUI.Button(new Rect(235 + j * 70, 150 + i * 70, 70, 70)," ")){
                        PutChess(i,j);
                    }
                    else if (chessBoard[i, j] == 8) GUI.Button(new Rect(235 + j * 70, 150 + i * 70, 70, 70), "8");
                    else if (chessBoard[i, j] == 1) GUI.Button(new Rect(235 + j * 70, 150 + i * 70, 70, 70), "1");
                    else if (chessBoard[i, j] == 2) GUI.Button(new Rect(235 + j * 70, 150 + i * 70, 70, 70), "2");
                    else if (chessBoard[i, j] == 3) GUI.Button(new Rect(235 + j * 70, 150 + i * 70, 70, 70), "3");
                    else if (chessBoard[i, j] == 4) GUI.Button(new Rect(235 + j * 70, 150 + i * 70, 70, 70), "4");
                    else if (chessBoard[i, j] == 5) GUI.Button(new Rect(235 + j * 70, 150 + i * 70, 70, 70), "5");
                    else if (chessBoard[i, j] == 6) GUI.Button(new Rect(235 + j * 70, 150 + i * 70, 70, 70), "6");
                    else if (chessBoard[i, j] == 7) GUI.Button(new Rect(235 + j * 70, 150 + i * 70, 70, 70), "7");
                }
            }
            print(count);
        }
        else {
            GUI.Box(new Rect(310, 175, 200, 200), "\n\n\n\n\n\nYou are winner!");
        }
    }
    void Init() {
        goal = 8;
        count = 0;
        for(int i = 0; i < 5; i++)
            for(int j = 0; j < 5; j++)
                chessBoard[i, j] = 0;
    }

    void PutChess(int i,int j) {
        chessBoard[i, j] = 1;
        if(chessBoard[i, j]<0){
            chessBoard[i, j]=(-1)*chessBoard[i, j];
        }
        print(chessBoard[i, j]);
        if(i-1>=0){
            if(chessBoard[i-1, j]==0)
                chessBoard[i-1, j]=chessBoard[i, j]+1;
            else{
                chessBoard[i-1, j]++;
            }
        }
        if(j+1<=4){
            if(chessBoard[i, j+1]==0){
                chessBoard[i, j+1]=chessBoard[i, j]+1;
            }
            else{
                chessBoard[i, j+1]++;
            }
        }
        if(j-1>=0){
            if(chessBoard[i, j-1]==0){
                chessBoard[i, j-1]=chessBoard[i, j]+1;
            }
            else{
                chessBoard[i, j-1]++;
            }
        }
        if(i+1<=4){
            if(chessBoard[i+1, j]==0){
                chessBoard[i+1, j]=chessBoard[i, j]+1;  
            }
            else{
                chessBoard[i+1, j]++;
            }
        }      
    }

    bool GameOver(int x) {
        int tempcount=0;
        for(int i=0;i<5;i++){
            for(int j=0;j<5;j++){
                if(chessBoard[i,j]==3){
                    if(i-1>=0&&j-1>=0&&chessBoard[i-1,j-1]==3){
                        tempcount+=2;
                    }
                    else if(i+1<=4&&j-1>=0&&chessBoard[i+1,j-1]==3){
                        tempcount+=2;
                    }
                    else if(i-1>=0&&j+1<=4&&chessBoard[i-1,j+1]==3){
                        tempcount+=2;
                    }
                    else if(i+1<=4&&j+1<=4&&chessBoard[i+1,j+1]==3){
                        tempcount+=2;
                    }
                }
            }
        }
        tempcount=tempcount/2;
        if (tempcount<x){
            count=tempcount;
            return false;
        }
        return true;
    }
}

游戏游玩视频:

https://www.bilibili.com/video/BV1Pk4y1F7qv/?vd_source=e26fb4af43e5928114e1f085b31774ce

猜你喜欢

转载自blog.csdn.net/qq3098320650/article/details/133386589