emm有一个BUG:level>10后出现的第二棵树,苹果掉落间隔没整好……不管了 反正也没人玩。
昨天提到为了测试,写了一个……我也不知道干什么的测试工程。
今天就补充了一下,稍微能玩一下的游戏DEMO。
好吧……虽然这似乎也没什么可以玩的。
本来想做一个网络排行榜……但涉及到Socket通信,就没去为了这个DEMO搞。
(其实后来发现好像也可以用php读取MQSQL数据,然后用Unity加载PHP的返回数据……不过懒得搞了……)
这个就是一个很无聊的捡苹果游戏吧……每掉落一个苹果(没接到),就会life-1。life归零时游戏结束。
会有一个最高分(仅本地存储)
游戏链接:捡苹果
食用方式:开袋即食。篮子会跟随光标移动。(移动端即触碰位置)
开发引擎:Unity3D
引擎版本:2019.2.17f1
编程语言:C#
该工程基于webGL,部分浏览器可能不支持。移动端可能会弹出性能警示窗口(选择继续即可。)
核心代码:
……先丢个包把……
https://cxbox.luheqiu.com/Article/Deane/game-workshop/ApplePicker/C# Script.zip
Choice
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Choice : MonoBehaviour
{
public void GameStart()
{
SceneManager.LoadScene("_Scene_0");
}
public void GameBack()
{
SceneManager.LoadScene("Start");
}
public void GameOver()
{
SceneManager.LoadScene("Over");
}
}
LevelControl
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LevelControl : MonoBehaviour
{
static public int highScore = 0;
public GameObject appleTreePrefab;
public Text scoreGT;
public Text levelUI;
static public int yourScore;
public int level;
//难度系数
public float speed = 10f;
public float leftAndRightEdge = 20f;
public float chanceToChangeDirections = 0.02f;
public float secondsBetweenAppleDrops = 1f;
public int treeNum = 1;
// Start is called before the first frame update
void Awake()
{
if (PlayerPrefs.HasKey("ApplePickerHighScore"))
{
highScore = PlayerPrefs.GetInt("ApplePickerHighScore");
}
else
{
PlayerPrefs.SetInt("ApplePickerHighScore", highScore);
}
}
void Start()
{
//计分板初始化
GameObject scoreGO = GameObject.Find("ScoreCounter");
scoreGT = scoreGO.GetComponent<Text>();
yourScore = 0;
scoreGT.text = "Your Score: " + yourScore;
//关卡初始化
GameObject levelGO = GameObject.Find("LevelCounter");
levelUI = levelGO.GetComponent<Text>();
level = 1;
levelUI.text = "Level: " + level;
speed = 10f + (level - 1) * 10;
secondsBetweenAppleDrops = 1f - (level - 1) * 0.3f;
treeNum = 1;
Vector3 pos = Vector3.zero;
pos.y = 12;
GameObject appleTree = Instantiate(appleTreePrefab) as GameObject;
appleTree.transform.position = pos;
}
// Update is called once per frame
void LateUpdate()
{
if (yourScore - level * 10 >= 0)
{
//关卡更新
level += 1;
levelUI.text = "Level: " + level;
//难度更新
speed = 10f + (level - 1) * 10;
if (speed >= 80) speed = 80;
if (treeNum >= 2)
{
secondsBetweenAppleDrops = 0.6f + treeNum * 0.2f;
speed = 60;
}
else
{
secondsBetweenAppleDrops = 1f - (level - 1) * 0.3f;
if (secondsBetweenAppleDrops < 0.1f) { secondsBetweenAppleDrops = 0.1f; }
}
}
if (level - treeNum * 10 >= 0&&treeNum<=10)
{
treeNum += 1;
Vector3 pos = Vector3.zero;
pos.y = 12;
GameObject appleTree = Instantiate(appleTreePrefab) as GameObject;
appleTree.transform.position = pos;
}
}
public void ApplePicked()
{
//计分
yourScore += 1;
scoreGT.text = "Your Score: " + yourScore;
}
}
ApplePicker
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class ApplePicker : MonoBehaviour
{
public Text lifeUI;
public int lifeNum;
public GameObject basketPrefab;
public float basketBottomY=-11f;
public float basketSpacingY = 2f;
public float appleBottomY = -20f;
public GameObject tBasketGO;
// Start is called before the first frame update
void Start()
{
//生命计数板初始化
GameObject lifeGO = GameObject.Find("LifeCounter");
lifeUI = lifeGO.GetComponent<Text>();
lifeNum = 3;
lifeUI.text = "Life: " + lifeNum;
tBasketGO = Instantiate(basketPrefab) as GameObject;
Vector3 pos = Vector3.zero;
pos.y = basketBottomY;
tBasketGO.transform.position = pos;
}
public void AppleDestroyed()
{
lifeNum -= 1;
lifeUI.text = "Life: " + lifeNum;
if (lifeNum <= 0)
{
GameObject[] tAppleArray = GameObject.FindGameObjectsWithTag("Apple");
foreach (GameObject tGO in tAppleArray) { Destroy(tGO); }
Destroy(tBasketGO);
//分数统一
//最高分监视
if (LevelControl.yourScore > LevelControl.highScore)
{
LevelControl.highScore = LevelControl.yourScore;
PlayerPrefs.SetInt("ApplePickerHighScore", LevelControl.highScore);
PlayerPrefs.Save();
}
SceneManager.LoadScene("Over");
}
}
}
Basket
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Basket : MonoBehaviour
{
// Update is called once per frame
void Update()
{
//获取鼠标坐标
Vector3 mousePos2D = Input.mousePosition;
mousePos2D.z = -Camera.main.transform.position.z;
Vector3 mousePos3D = Camera.main.ScreenToWorldPoint(mousePos2D);
//篮筐跟随
Vector3 pos = transform.position;
pos.x = mousePos3D.x;
transform.position = pos;
}
}
BasketTop
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BasketTop : MonoBehaviour
{
void OnCollisionEnter(Collision collision)
{
//碰撞检查
GameObject collidedWith = collision.gameObject;
if (collidedWith.tag == "Apple")
{
Destroy(collidedWith);
//通知计分板计分
LevelControl tLevelControl = GameObject.Find("LevelCounter").GetComponent<LevelControl>();
tLevelControl.ApplePicked();
}
}
}
Apple
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Apple : MonoBehaviour
{
// Update is called once per frame
void Update()
{
ApplePicker apScript = Camera.main.GetComponent<ApplePicker>();
//未接住苹果
if (transform.position.y < apScript.appleBottomY)
{
Destroy(gameObject);
//调用ApplePicker类的AppleDestroyed()函数
apScript.AppleDestroyed();
}
}
AppleTree
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AppleTree : MonoBehaviour
{
public GameObject applePrefab;
public float tSpeed;
// Start is called before the first frame update
void Start()
{
LevelControl tLevelControl = GameObject.Find("LevelCounter").GetComponent<LevelControl>();
//速度初始化
tSpeed = tLevelControl.speed;
InvokeRepeating("DropApple", 2f, tLevelControl.secondsBetweenAppleDrops);
}
void DropApple()
{
GameObject apple = Instantiate(applePrefab) as GameObject;
apple.transform.position = transform.position;
}
// Update is called once per frame
void Update()
{
LevelControl tLevelControl = GameObject.Find("LevelCounter").GetComponent<LevelControl>();
if (Mathf.Abs(tSpeed) < Mathf.Abs(tLevelControl.speed)){ tSpeed = tLevelControl.speed; }
//移动
Vector3 pos = transform.position;
pos.x += tSpeed * Time.deltaTime;
this.transform.position = pos;
//变向
if (pos.x < -tLevelControl.leftAndRightEdge) { tSpeed= Mathf.Abs(tSpeed); }
else if(pos.x > tLevelControl.leftAndRightEdge) { tSpeed= -Mathf.Abs(tSpeed); }
else if (Random.value < tLevelControl.chanceToChangeDirections) { tSpeed *= -1; }
}
}
GameOver
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameOver : MonoBehaviour
{
public Text scoreUT;
public Text highScoreUT;
void Start()
{
//计分板初始化
GameObject scoreGO = GameObject.Find("YourScore");
scoreUT = scoreGO.GetComponent<Text>();
scoreUT.text = "Your Score: " + LevelControl.yourScore;
GameObject highScoreGO = GameObject.Find("HighScore");
scoreUT = highScoreGO.GetComponent<Text>();
int highScore= PlayerPrefs.GetInt("ApplePickerHighScore",0);
scoreUT.text = "High Score: " + highScore;
}
}