Python版猜数字游戏
python
import random
def guess_number_game(): gzjiazheng.cn
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()
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 attempts = 0;
function checkGuess() {
const guess = parseInt(document.getElementById(‘guess’).value, 10);
attempts++;
const feedback = document.getElementById(‘feedback’);
if (isNaN(guess)) {
feedback.textContent = "请输入一个有效的数字。";
return;
}
if (guess < numberToGuess) {
feedback.textContent = "太小了!";
} else if (guess > numberToGuess) {
feedback.textContent = "太大了!";
} else {
feedback.textContent = `恭喜你!答对了!数字就是${numberToGuess}。你尝试了${attempts}次。`;
}
}
C#版猜数字游戏(Unity示例)
在Unity中,你需要创建一个C#脚本(例如GuessNumberGame.cs),并附加到一个游戏对象上。这里只提供脚本的关键部分,因为Unity项目的设置会稍微复杂一些。
csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; // 如果你使用UI元素来显示信息
public class GuessNumberGame : MonoBehaviour
{
private int numberToGuess = Random.Range(1, 101);
private int attempts = 0;
// 假设你有一个输入字段和几个文本组件来显示提示和结果
public InputField guessInput;
public Text feedbackText;
public void CheckGuess()
{
int guess;
if (int.TryParse(guessInput.text, out guess))
{
attempts++;
if (guess < numberToGuess)
{
feedbackText.text = "太小了!";
}
else if (guess > numberToGuess)
{
feedbackText.text = "太大了!";
}
else
{
feedbackText.text = $"恭喜你!答对了!数字就是{numberToGuess}。你尝试了{attempts}次。";
}
}
else
{
feedbackText.text = "请输入一个有效的数字。";
}
}
// 确保在Unity编辑器中正确连接了InputField和Text组件
}
请注意,Unity示例假设你已经有了一个基本的Unity项目设置,包括UI元素(如InputField由于篇幅限制,我将分别用Python(一个流行的解释型语言,常用于入门编程和游戏开发)、JavaScript(常用于网页开发,但也可以用于游戏开发)和C#(常用于Unity游戏引擎)来展示一个简单的“猜数字”游戏的基础代码。
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()
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 attempts = 0;
function checkGuess() {
const guess = parseInt(document.getElementById(‘guess’).value, 10);
attempts++;
const feedback = document.getElementById(‘feedback’);
if (isNaN(guess)) {
feedback.textContent = "请输入一个有效的数字。";
return;
}
if (guess < numberToGuess) {
feedback.textContent = "太小了!";
} else if (guess > numberToGuess) {
feedback.textContent = "太大了!";
} else {
feedback.textContent = `恭喜你!答对了!数字就是${numberToGuess}。你尝试了${attempts}次。`;
}
}
C#版猜数字游戏(Unity示例)
在Unity中,你需要创建一个C#脚本(例如GuessNumberGame.cs),并附加到一个游戏对象上。这里只提供脚本的关键部分,因为Unity项目的设置会稍微复杂一些。
csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; // 如果你使用UI元素来显示信息
public class GuessNumberGame : MonoBehaviour
{
private int numberToGuess = Random.Range(1, 101);
private int attempts = 0;
// 假设你有一个输入字段和几个文本组件来显示提示和结果
public InputField guessInput;
public Text feedbackText;
public void CheckGuess()
{
int guess;
if (int.TryParse(guessInput.text, out guess))
{
attempts++;
if (guess < numberToGuess)
{
feedbackText.text = "太小了!";
}
else if (guess > numberToGuess)
{
feedbackText.text = "太大了!";
}
else
{
feedbackText.text = $"恭喜你!答对了!数字就是{numberToGuess}。你尝试了{attempts}次。";
}
}
else
{
feedbackText.text = "请输入一个有效的数字。";
}
}
// 确保在Unity编辑器中正确连接了InputField和Text组件
}
请注意,Unity示例假设你已经有了一个基本的Unity项目设置,包括UI元素(如InputField