using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
title:俄罗斯方块代码
1、随机的显示方块
2、显示的方块运行脚本
3、碰到标签为a的,就随机克隆方块
4、这里最多能横排可以放下11个方块
5、先把所有方块的y坐标放到列表里
6、如果列表里某个坐标的值一样的有11个说明要消除这一行
*/
public class Move : MonoBehaviour
{
void i()
{
int a1 = Random.Range(1, 2);
if (a1 == 1)
{
t.SetActive(true);
l.SetActive(false);
}
if (a1 == 2)
{
t.SetActive(false);
l.SetActive(true);
}
}
// Start is called before the first frame update
void Start()
{
i();
}
bool right = false;
bool left = false;
bool rotate1 = false;
bool down = false;
bool d = false;
public GameObject object1;
public GameObject t;
public GameObject l;
public GameObject object2;
List<float> y = new List<float>();
// Update is called once per frame
void Update()
{
Vector3 pos = transform.position;
Vector3 rotate = transform.localEulerAngles;
if (Input.GetKeyDown(KeyCode.LeftArrow)){left = true; } else { left = false; }
if (Input.GetKeyDown(KeyCode.RightArrow)) { right = true; } else { right = false; }
if (Input.GetKeyDown(KeyCode.UpArrow)) { rotate1 = true;} else { rotate1 = false; }
if (Input.GetKeyDown(KeyCode.DownArrow)) { down = true; } else { down = false; }
if(left){ pos.x -= 7 * Time.deltaTime;}
if (right){ pos.x += 7 * Time.deltaTime; }
if (rotate1) { rotate.z -= 90; }
if (down) { pos.y -= 5 * Time.deltaTime; }
if (d)
{
if (gameObject.name == "T")
{
object1.SetActive(true);
object1.transform.position = pos;
GameObject a = Instantiate(object1, pos, transform.rotation);
object1.SetActive(false);
}
if(gameObject.name == "L")
{
object2.SetActive(true);
object2.transform.position = pos;
GameObject a = Instantiate(object2,pos, transform.rotation);
object2.SetActive(false);
}
i();
pos.y = 4.66f;
transform.position = pos;
d = false;
}
int p = 0;
int u = 1;
for(int i = 0;i < y.Count-1;i++)
{
if(y[i] == y[u])
{
p++;
}
u += 1;
if (p > 4)
{
Debug.Log("消除一行");
p = 0;
}
}
transform.position = pos ;
transform.localEulerAngles = rotate;
}
void OnCollisionEnter2D(Collision2D e) {
if(e.gameObject.tag == "a" && gameObject.tag == "a")
{
Transform[] myTransforms = GetComponentsInChildren<Transform>();
foreach (var child in myTransforms)
{
Vector3 pos8 = child.position;
y.Add(pos8.y);
Debug.Log(pos8.y);
}
d = true;
}
}
}
俄罗斯方块制作(未完成)
最新推荐文章于 2024-04-21 11:04:37 发布