U3D实现2048小游戏(含源码)

本文详述了使用Unity3D制作2048小游戏的过程,包括架构设计、UI布局、脚本编写等方面,展示了从无到有实现游戏的步骤。通过这个项目,作者强调了学习态度和技术应用的重要性,提醒开发者保持谦卑,不断进步。
摘要由CSDN通过智能技术生成

摘要

上周面了一家小型游戏公司,见了一位领导(一般我们喜欢叫自己的上级老大,这样比较亲切)。当然结果肯定不算好,不过当时那个哥们问我做个2048我要多久,当时我不是很清楚,大概回了句2天。不过好像还是太小看自己了,昨天下午花了一下午,基本把游戏实现了。(当然这个游戏是开源的,在github上应该有源码,不过为了证明自己,肯定全自己手写)

架构

先预览下
在这里插入图片描述
在这里插入图片描述
U3D架构如下:
在这里插入图片描述
1.一个cavas容器来存储所有UI,参数:
render mode:screen space-camera;
render canmera:main camera;
UI scale mode:scale with scree size;
添加image组件,背景颜色最好白色
2.panel对象,用来装格子的预制体,
添加image组件,背景颜色自己选,在Rect Transform更改panel大小;
3.新建3个text文档,分别表示:
游戏结束显示;
再玩一次,记得添加button组件;
分数显示
4.新建空对象game,用来挂载游戏逻辑
5.创建方块预制体,一个100*100的image,挂一个子对象text用来显示格子的数字

脚本

FieldParameter挂在预制体下,主要是添加属性:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FieldParameter : MonoBehaviour {
   
	public int row;
	public int col;
}

GameControl脚本挂在game对象,在Inspector界面填入对应对象:
Rect输入预制体对象,Cavas输入panel对象,Over,Again,Score_text分别输入三个对应的文本对象(结束,再玩,分数)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameControl : MonoBehaviour {
   
	private static GameObject[,] fields;
	public GameObject rect;
	public GameObject cavas;
	public int score;
	public bool gameover;
	public Text over,again,score_text;
	private int move_state;  //1:上移 -1:下移 2:右移 -2:左移

	// Use this for initialization
	void Start () {
   
		
		Game_Init ();
	}
	
	// Update is called once per frame
	void Update () {
   
		if (!gameover) {
   
			Press ();
			score_text.text = "SCORE:\n"+score.ToString();
		}
	}

	//初始化
	public void Game_Init(){
   
		gameover = false;
		over.enabled = false;
		again.enabled = false;
		score = 0;

		fields = new GameObject[4, 4];
		float distence = 110;                   //两个预制体的距离
		Vector2 position_init=new Vector2(-177f,165f);   //初始化预制体的位置
		for (int i = 0; i < 4; i++) {
   
			for (int j = 0; j < 4; j++) {
   	
				GameObject field = Instantiate (rect);
				field.transform.parent = cavas.transform;
				field.transform.localPosition = new Vector3 (position_init.x + distence * j, position_init.y - distence * i);
				field.transform.localScale = new Vector3 (1, 1, 1);
				field.GetComponent<Image> ().color = Color.grey;
				field.GetComponent<FieldParameter> ().row = i;
				field.GetComponent<FieldParameter> ().col = j;
				field.GetComponentInChildren<Text>().text = "";
				fields [i, j] = field;
			}
		}

		Creat_Num ();
		Creat_Num ();
	}

	//寻找Fileds中为""的field
	private List<GameObject> Find_Field(){
   
		List<GameObject> no_num = new List<GameObject> ();
		foreach (GameObject item in fields) {
   
			if (item.GetComponentInChildren<Text>().text.Equals("")) {
   
				no_num.Add (item);
			}
		}
		return no_num;
	}


	//随机生成数字
	private GameObject Creat_Num(){
   
		string num="2";
		if (Random.value > 0.5) {
   			
			num = "4";
		}

		List<GameObject> no_num = Find_Field ();
		if (no_num.Count > 0) {
   
			int index = (int)(Random.value * no_num.Count);
			no_num [index].GetComponentInChildren<Text> ().text = num;
			no_num [index].GetComponent<Image> ().color = Color.white;
			return no_num [index];
		} else	return null;		
	}

	//上 下 左 右控制
	void Press(){
   
		if (Application.platform == RuntimePlatform.Android && Input.touchCount == 1 && Input.touches [0].phase == TouchPhase.Moved) {
   
			float s01=Input.GetAxis("Mouse X");    
			float s02=Input.GetAxis("Mouse Y");    
			float distence = 5f;
			if (s01 < -1 * distence && s02>-1*distence && s02<distence) {
   
				move_state=-2;
			} else if (s01 > distence  && s02>-1*distence && s02<distence) {
   
				move_state=2;
			} else if (s02 > distence  && s01>-1*distence && s01<distence) {
   
				move_state=1
  • 9
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值