【小游戏】你比划我来猜
程序员文章站
2024-03-16 20:50:04
...
欢迎加入Unity业内qq交流群:956187480
qq扫描二维码加群
1.制作ui界面
开始,介绍,下一步,翻转开始,游戏界面,结束界面
2.调用陀螺仪
横屏下翻:跳过;上翻正确
重点:如何判断上翻还是下翻
需要先写个测试模拟代码,在真机上翻转测试一下。这里因为是只需要获取到横屏的上下翻转,经测试只需要拿到陀螺仪的旋转y值即可,水平放到桌面的时候y值为0或者360度,横屏垂直于桌面的时候y值为90度。
所以在制作期间我定位70<y<90的时候是正常状态,当y>135的时候是下翻状态,当y<45的时候是上翻状态,游戏的全过程就是在Up,Down,None三种状态下来回切换的逻辑.
3.代码
#region 模块信息
// **********************************************************************
// Copyright (C) 2020
// Please contact me if you have any questions
// File Name: GameManager
// Author: 幻世界
// QQ群: 956187480
// **********************************************************************
#endregion
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
//本机陀螺仪
Gyroscope go;
//是否支持陀螺仪
bool gyinfo;
//词库
private List<string> wordsList;
//猜对的数量
public int correctNumber;
//游戏界面
public GameObject gameView;
public Text contentText;
//结束界面
public GameObject gameOver;
public Text numText;
//下翻开始界面
public GameObject downStart;
//倒计时
private float time = 20;
public Text timeText;
private bool isStart;
private bool isUp;
private bool isDown;
public Text text;
// Start is called before the first frame update
void Start()
{
wordsList = new Words().wordsList;
gyinfo = SystemInfo.supportsGyroscope;
go = Input.gyro;
go.enabled = false;
}
// Update is called once per frame
void Update()
{
timeText.text = (int)time + "";
if (downStart.activeSelf)
{
go.enabled = true;
}
if (go.enabled)
{
Vector3 a = go.attitude.eulerAngles; //直接使用读取的欧拉角发现不对,于是自己调整一下符号
text.text = a.ToString();
if (a.y < 45 && isStart)//Up
{
if (!isUp)
{
isUp = true;
if (wordsList.Count <= 0)
{
contentText.text ="结束";
return;
}
int index = Random.Range(0, wordsList.Count);
contentText.text = wordsList[index];
wordsList.Remove(contentText.text);
correctNumber++;
}
}
else if (a.y > 135)//Down
{
if (!isDown)
{
isDown = true;
if (!isStart)
{
isStart = true;
gameView.SetActive(true);
int index = Random.Range(0, wordsList.Count);
contentText.text = wordsList[index];
wordsList.Remove(contentText.text);
downStart.SetActive(false);
}
else
{
if (wordsList.Count <= 0)
{
contentText.text = "结束";
return;
}
int index = Random.Range(0, wordsList.Count);
contentText.text = wordsList[index];
wordsList.Remove(contentText.text);
}
}
}
else if (a.y < 110 && a.y > 70)
{
isUp = false;
isDown = false;
}
}
if (isStart)
{
time -= Time.deltaTime;
if (time <= 0)
{
isStart = false;
gameOver.SetActive(true);
gameView.SetActive(false);
numText.text = correctNumber + "";
go.enabled = false;
}
}
}
public void Regame()
{
wordsList.Clear();
wordsList = new Words().wordsList;
gameOver.SetActive(false);
correctNumber = 0;
time = 20;
isUp = false;
isDown = false;
downStart.SetActive(true);
}
public void Home()
{
SceneManager.LoadScene(0);
}
}
public class Words
{
public List<string> wordsList;
// Start is called before the first frame update
public Words()
{
wordsList = new List<string> {
"青蛙","喵咪","五体投地","柠檬","孙悟空","胶带","茶杯","手机","鼠标","椅子",
};
}
}