- Python 版本ygahua.com
Python 版本的猜数字游戏非常直观,适合初学者。
python
import random
def guess_number_game():
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
print("猜数字游戏!我想了一个1到100之间的数字。")
while guess != number_to_guess:
try:
guess = int(input("请输入你的猜测:"))
attempts += 1
if guess < number_to_guess:
print("太小了!")
elif guess > number_to_guess:
print("太大了!")
else:
print(f"恭喜你!猜对了!数字是{number_to_guess}。你尝试了{attempts}次。")
except ValueError:
print("请输入一个有效的数字!")
if name == “main”:
guess_number_game()
2. JavaScript 版本(HTML + JavaScript)
这个版本的猜数字游戏将在网页上进行。
HTML (index.html):
html
猜数字游戏
我猜了一个1到100之间的数字。
提交<script src="game.js"></script>
JavaScript (game.js):
javascript
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
function checkGuess() {
guess = parseInt(document.getElementById(‘guess’).value);
attempts++;
let result = document.getElementById('result');
if (isNaN(guess)) {
result.textContent = "请输入一个有效的数字!";
} else if (guess < numberToGuess) {
result.textContent = "太小了!";
} else if (guess > numberToGuess) {
result.textContent = "太大了!";
} else {
result.textContent = `恭喜你!猜对了!数字是${numberToGuess}。你尝试了${attempts}次。`;
}
}
3. C# 版本(Unity 示例)
Unity 使用 C# 编写游戏脚本。下面是一个简单的猜数字游戏逻辑,但请注意,Unity 主要是用于图形和交互性强的游戏开发,因此以下代码将更侧重于逻辑结构。
C# (Unity Script):
由于Unity环境需要UI元素来接收输入和显示结果,这里只提供逻辑部分的伪代码。
csharp
using UnityEngine;
using UnityEngine.UI;
public class GuessNumberGame : MonoBehaviour
{
private int numberToGuess = Random.Range(1, 101);
private int guess = 0;
private int attempts = 0;
public Text resultText; // 假设你已经在Unity中设置了这个Text组件
public void CheckGuess(int playerGuess)
{
guess = playerGuess;
attempts++;
if (guess < numberToGuess)
{
resultText.text = "太小了!";
}
else if (guess > numberToGuess)
{
resultText.text = "太大了!";
}
else
{
resultText.text = $"恭喜你!猜对了!数字是{numberToGuess}。你尝试了{attempts}次。";
}
}
}
请注意,Unity 中的 CheckGuess 方法需要你通过某种方式(如按钮点击事件)来调用,并传递玩家的猜测作为参数。同时,resultText 需要你在Unity编辑器中绑定到一个UI Text组件上。由于篇幅限制,我将为你提供一个简单的猜数字游戏示例,使用三种不同的编程语言来实现:Python、JavaScript(用于网页环境)和C#(用于.NET环境,如Unity游戏开发)。
- Python 版本
Python 版本的猜数字游戏非常直观,适合初学者。
python
import random
def guess_number_game():
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
print("猜数字游戏!我想了一个1到100之间的数字。")
while guess != number_to_guess:
try:
guess = int(input("请输入你的猜测:"))
attempts += 1
if guess < number_to_guess:
print("太小了!")
elif guess > number_to_guess:
print("太大了!")
else:
print(f"恭喜你!猜对了!数字是{number_to_guess}。你尝试了{attempts}次。")
except ValueError:
print("请输入一个有效的数字!")
if name == “main”:
guess_number_game()
2. JavaScript 版本(HTML + JavaScript)
这个版本的猜数字游戏将在网页上进行。
HTML (index.html):
html
猜数字游戏
我猜了一个1到100之间的数字。
提交<script src="game.js"></script>
JavaScript (game.js):
javascript
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
function checkGuess() {
guess = parseInt(document.getElementById(‘guess’).value);
attempts++;
let result = document.getElementById('result');
if (isNaN(guess)) {
result.textContent = "请输入一个有效的数字!";
} else if (guess < numberToGuess) {
result.textContent = "太小了!";
} else if (guess > numberToGuess) {
result.textContent = "太大了!";
} else {
result.textContent = `恭喜你!猜对了!数字是${numberToGuess}。你尝试了${attempts}次。`;
}
}
3. C# 版本(Unity 示例)
Unity 使用 C# 编写游戏脚本。下面是一个简单的猜数字游戏逻辑,但请注意,Unity 主要是用于图形和交互性强的游戏开发,因此以下代码将更侧重于逻辑结构。
C# (Unity Script):
由于Unity环境需要UI元素来接收输入和显示结果,这里只提供逻辑部分的伪代码。
csharp
using UnityEngine;
using UnityEngine.UI;
public class GuessNumberGame : MonoBehaviour
{
private int numberToGuess = Random.Range(1, 101);
private int guess = 0;
private int attempts = 0;
public Text resultText; // 假设你已经在Unity中设置了这个Text组件
public void CheckGuess(int playerGuess)
{
guess = playerGuess;
attempts++;
if (guess < numberToGuess)
{
resultText.text = "太小了!";
}
else if (guess > numberToGuess)
{
resultText.text = "太大了!";
}
else
{
resultText.text = $"恭喜你!猜对了!数字是{numberToGuess}。你尝试了{attempts}次。";
}
}
}
请注意,Unity 中的 CheckGuess 方法需要你通过某种方式(如按钮点击事件)来调用,并传递玩家的猜测作为参数。同时,resultText 需要你在Unity编辑器中绑定到一个UI Text组件上。