飞道的博客

【小游戏】你比划我来猜

208人阅读  评论(0)

欢迎加入Unity业内qq交流群:956187480

qq扫描二维码加群


1.制作ui界面
开始,介绍,下一步,翻转开始,游戏界面,结束界面
  

2.调用陀螺仪
横屏下翻:跳过;上翻正确
重点:如何判断上翻还是下翻
需要先写个测试模拟代码,在真机上翻转测试一下。这里因为是只需要获取到横屏的上下翻转,经测试只需要拿到陀螺仪的旋转y值即可,水平放到桌面的时候y值为0或者360度,横屏垂直于桌面的时候y值为90度。
所以在制作期间我定位70<y<110的时候是正常状态,当y>135的时候是下翻状态,当y<45的时候是上翻状态,游戏的全过程就是在Up,Down,None三种状态下来回切换的逻辑.
3.代码:源码源工程https://download.csdn.net/download/qq_37310110/15562084


  
  1. #region 模块信息
  2. // **********************************************************************
  3. // Copyright (C) 2020
  4. // Please contact me if you have any questions
  5. // File Name: GameManager
  6. // Author: 幻世界
  7. // QQ群: 956187480
  8. // **********************************************************************
  9. #endregion
  10. using System.Collections;
  11. using System.Collections.Generic;
  12. using UnityEngine;
  13. using UnityEngine.SceneManagement;
  14. using UnityEngine.UI;
  15. public class GameManager : MonoBehaviour
  16. {
  17. //本机陀螺仪
  18. Gyroscope go;
  19. //是否支持陀螺仪
  20. bool gyinfo;
  21. //词库
  22. private List< string> wordsList;
  23. //猜对的数量
  24. public int correctNumber;
  25. //游戏界面
  26. public GameObject gameView;
  27. public Text contentText;
  28. //结束界面
  29. public GameObject gameOver;
  30. public Text numText;
  31. //下翻开始界面
  32. public GameObject downStart;
  33. //倒计时
  34. private float time = 20;
  35. public Text timeText;
  36. private bool isStart;
  37. private bool isUp;
  38. private bool isDown;
  39. public Text text;
  40. // Start is called before the first frame update
  41. void Start()
  42. {
  43. wordsList = new Words().wordsList;
  44. gyinfo = SystemInfo.supportsGyroscope;
  45. go = Input.gyro;
  46. go.enabled = false;
  47. }
  48. // Update is called once per frame
  49. void Update()
  50. {
  51. timeText.text = ( int)time + "";
  52. if (downStart.activeSelf)
  53. {
  54. go.enabled = true;
  55. }
  56. if (go.enabled)
  57. {
  58. Vector3 a = go.attitude.eulerAngles; //直接使用读取的欧拉角发现不对,于是自己调整一下符号
  59. text.text = a.ToString();
  60. if (a.y> 0 && a.y < 90 && isStart) //Up
  61. {
  62. if (!isUp)
  63. {
  64. isUp = true;
  65. if (wordsList.Count <= 0)
  66. {
  67. contentText.text = "结束";
  68. return;
  69. }
  70. int index = Random.Range( 0, wordsList.Count);
  71. contentText.text = wordsList[index];
  72. wordsList.Remove(contentText.text);
  73. correctNumber++;
  74. }
  75. }
  76. else if (a.y > 135 && a.y< 180) //Down
  77. {
  78. if (!isDown)
  79. {
  80. isDown = true;
  81. if (!isStart)
  82. {
  83. isStart = true;
  84. gameView.SetActive( true);
  85. int index = Random.Range( 0, wordsList.Count);
  86. contentText.text = wordsList[index];
  87. wordsList.Remove(contentText.text);
  88. downStart.SetActive( false);
  89. }
  90. else
  91. {
  92. if (wordsList.Count <= 0)
  93. {
  94. contentText.text = "结束";
  95. return;
  96. }
  97. int index = Random.Range( 0, wordsList.Count);
  98. contentText.text = wordsList[index];
  99. wordsList.Remove(contentText.text);
  100. }
  101. }
  102. }
  103. else if (a.y < 110 && a.y > 70)
  104. {
  105. isUp = false;
  106. isDown = false;
  107. }
  108. }
  109. if (isStart)
  110. {
  111. time -= Time.deltaTime;
  112. if (time <= 0)
  113. {
  114. isStart = false;
  115. gameOver.SetActive( true);
  116. gameView.SetActive( false);
  117. numText.text = correctNumber + "";
  118. go.enabled = false;
  119. }
  120. }
  121. }
  122. public void Regame()
  123. {
  124. wordsList.Clear();
  125. wordsList = new Words().wordsList;
  126. gameOver.SetActive( false);
  127. correctNumber = 0;
  128. time = 20;
  129. isUp = false;
  130. isDown = false;
  131. downStart.SetActive( true);
  132. }
  133. public void Home()
  134. {
  135. SceneManager.LoadScene( 0);
  136. }
  137. }
  138. public class Words
  139. {
  140. public List< string> wordsList;
  141. // Start is called before the first frame update
  142. public Words()
  143. {
  144. wordsList = new List< string> {
  145. "青蛙", "喵咪", "五体投地", "柠檬", "孙悟空", "胶带", "茶杯", "手机", "鼠标", "椅子",
  146. };
  147. }
  148. }


源码源工程https://download.csdn.net/download/qq_37310110/15562084


转载:https://blog.csdn.net/qq_37310110/article/details/114369122
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场