- Python 版本的猜数字游戏yctsy.cn
python
import random
def guess_number_game():
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
print("欢迎来到猜数字游戏!")
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}。")
print(f"你总共猜了 {attempts} 次。")
except ValueError:
print("请输入一个有效的数字。")
if name == “main”:
guess_number_game()
2. JavaScript (Node.js) 版本的猜数字游戏
首先,确保你的环境中安装了Node.js。
javascript
const readline = require(‘readline’).createInterface({
input: process.stdin,
output: process.stdout
});
const numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
console.log(“欢迎来到猜数字游戏!”);
console.log(“我已经想好了一个1到100之间的数字。”);
readline.question('请猜一个数字: ', (input) => {
guess = parseInt(input, 10);
attempts++;
if (isNaN(guess)) {
console.log("请输入一个有效的数字。");
readline.close();
return;
}
if (guess < numberToGuess) {
console.log("太小了!");
} else if (guess > numberToGuess) {
console.log("太大了!");
} else {
console.log(`恭喜你!答案就是 ${numberToGuess}。`);
console.log(`你总共猜了 ${attempts} 次。`);
}
if (guess !== numberToGuess) {
readline.question('请再猜一个数字: ', (input) => {
// 递归调用或修改逻辑以适应多次猜测
// 这里为了简洁,我们仅允许一次猜测
readline.close();
});
} else {
readline.close();
}
});
3. C# (Unity) 版本的猜数字游戏(简化版)
Unity 主要用于图形界面游戏,但这里我们可以用 C# 脚本在 Unity 控制台中模拟这个游戏。
csharp
using System;
using UnityEngine;
public class GuessNumberGame : MonoBehaviour
{
private int numberToGuess = UnityEngine.Random.Range(1, 101);
private int guess = 0;
private int attempts = 0;
void Start()
{
Debug.Log("欢迎来到猜数字游戏!");
Debug.Log("我已经想好了一个1到100之间的数字。");
// Unity 游戏中通常不直接在 Start 中运行游戏逻辑,这里仅为演示
// 真实情况中,你可能需要在 Update 中检测输入或调用一个方法
GuessLoop();
}
void GuessLoop()
{
// 假设我们在这里有一个方法来获取用户的输入
// 实际上,Unity 中获取输入会更复杂,需要绑定到 UI 或 Input 系统中
// 这里我们直接模拟用户输入
guess = 50; // 假设用户输入了50
attempts++;
if (guess < numberToGuess)
{
Debug.Log("太小了!");
}
else if (guess > numberToGuess)
{
Debug.Log("太大了!");
}
else
{
Debug.Log($"恭喜你!答案就是 {numberToGuess}。");
Debug.Log($"你总共猜了 {attempts} 次。");
return; // 结束游戏
}
// 递归调用或重新设计逻辑以适应多次猜测
由于篇幅限制,我将为你展示如何使用三种流行的编程语言(Python、JavaScript (Node.js 环境下的简单命令行游戏)、和 C# (Unity 游戏引擎的一部分)) 来创建一个非常基础的猜数字游戏。
- Python 版本的猜数字游戏
python
import random
def guess_number_game():
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
print("欢迎来到猜数字游戏!")
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}。")
print(f"你总共猜了 {attempts} 次。")
except ValueError:
print("请输入一个有效的数字。")
if name == “main”:
guess_number_game()
2. JavaScript (Node.js) 版本的猜数字游戏
首先,确保你的环境中安装了Node.js。
javascript
const readline = require(‘readline’).createInterface({
input: process.stdin,
output: process.stdout
});
const numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
console.log(“欢迎来到猜数字游戏!”);
console.log(“我已经想好了一个1到100之间的数字。”);
readline.question('请猜一个数字: ', (input) => {
guess = parseInt(input, 10);
attempts++;
if (isNaN(guess)) {
console.log("请输入一个有效的数字。");
readline.close();
return;
}
if (guess < numberToGuess) {
console.log("太小了!");
} else if (guess > numberToGuess) {
console.log("太大了!");
} else {
console.log(`恭喜你!答案就是 ${numberToGuess}。`);
console.log(`你总共猜了 ${attempts} 次。`);
}
if (guess !== numberToGuess) {
readline.question('请再猜一个数字: ', (input) => {
// 递归调用或修改逻辑以适应多次猜测
// 这里为了简洁,我们仅允许一次猜测
readline.close();
});
} else {
readline.close();
}
});
3. C# (Unity) 版本的猜数字游戏(简化版)
Unity 主要用于图形界面游戏,但这里我们可以用 C# 脚本在 Unity 控制台中模拟这个游戏。
csharp
using System;
using UnityEngine;
public class GuessNumberGame : MonoBehaviour
{
private int numberToGuess = UnityEngine.Random.Range(1, 101);
private int guess = 0;
private int attempts = 0;
void Start()
{
Debug.Log("欢迎来到猜数字游戏!");
Debug.Log("我已经想好了一个1到100之间的数字。");
// Unity 游戏中通常不直接在 Start 中运行游戏逻辑,这里仅为演示
// 真实情况中,你可能需要在 Update 中检测输入或调用一个方法
GuessLoop();
}
void GuessLoop()
{
// 假设我们在这里有一个方法来获取用户的输入
// 实际上,Unity 中获取输入会更复杂,需要绑定到 UI 或 Input 系统中
// 这里我们直接模拟用户输入
guess = 50; // 假设用户输入了50
attempts++;
if (guess < numberToGuess)
{
Debug.Log("太小了!");
}
else if (guess > numberToGuess)
{
Debug.Log("太大了!");
}
else
{
Debug.Log($"恭喜你!答案就是 {numberToGuess}。");
Debug.Log($"你总共猜了 {attempts} 次。");
return; // 结束游戏
}
// 递归调用或重新设计逻辑以适应多次猜测