代码看看
时间: 2023-07-02 16:12:39 浏览: 52
以下是一个简单的电路解密小游戏的代码示例:
```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CircuitGame : MonoBehaviour
{
public GameObject power;
public GameObject[] switches;
public GameObject[] bulbs;
public AudioClip successSound;
public AudioClip failureSound;
private bool[] switchStates;
private bool[] bulbStates;
// Start is called before the first frame update
void Start()
{
switchStates = new bool[switches.Length];
bulbStates = new bool[bulbs.Length];
}
// Update is called once per frame
void Update()
{
// Check if the power is on
bool powerOn = power.activeSelf;
// Check the state of each switch
for (int i = 0; i < switches.Length; i++)
{
switchStates[i] = switches[i].GetComponent<Switch>().isOn;
}
// Update the state of each bulb
for (int i = 0; i < bulbs.Length; i++)
{
if (powerOn && switchStates[i])
{
bulbStates[i] = true;
bulbs[i].GetComponent<Bulb>().TurnOn();
}
else
{
bulbStates[i] = false;
bulbs[i].GetComponent<Bulb>().TurnOff();
}
}
// Check if all bulbs are on
bool allBulbsOn = true;
for (int i = 0; i < bulbStates.Length; i++)
{
if (!bulbStates[i])
{
allBulbsOn = false;
break;
}
}
// Play sound and show message if all bulbs are on
if (allBulbsOn)
{
GetComponent<AudioSource>().PlayOneShot(successSound);
Debug.Log("You win!");
}
}
// Reset the game
public void ResetGame()
{
power.SetActive(false);
foreach (GameObject s in switches)
{
s.GetComponent<Switch>().isOn = false;
}
foreach (GameObject b in bulbs)
{
b.GetComponent<Bulb>().TurnOff();
}
}
// Turn on the power
public void TurnOnPower()
{
power.SetActive(true);
}
// Turn off the power
public void TurnOffPower()
{
power.SetActive(false);
GetComponent<AudioSource>().PlayOneShot(failureSound);
}
}
```
这段代码通过检查电源、开关和灯泡的状态,来判断是否成功解密电路。在电路解密成功时,会播放胜利的音效并输出"you win!"的消息。在电路解密失败时,会播放失败的音效。同时,还可以通过"ResetGame"函数来重置游戏。