- Python 版本:猜数字游戏pzswcc.cn
python
import random
def guess_number_game():
number_to_guess = random.randint(1, 100)
print(“我想了一个1到100之间的数字,你能猜到是哪个吗?”)
tries = 0
while True:
try:
guess = int(input("请输入你的猜测:"))
tries += 1
if guess < number_to_guess:
print("太小了!再试试看。")
elif guess > number_to_guess:
print("太大了!再试试看。")
else:
print(f"恭喜你!猜对了!数字就是{number_to_guess}。你总共猜了{tries}次。")
break
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 tries = 0;
let feedback = document.getElementById(‘feedback’);
function checkGuess() {
const guess = parseInt(document.getElementById(‘guess’).value, 10);
tries++;
if (isNaN(guess)) {
feedback.textContent = "请输入一个有效的数字!";
return;
}
if (guess < numberToGuess) {
feedback.textContent = "太小了!再试试看。";
} else if (guess > numberToGuess) {
feedback.textContent = "太大了!再试试看。";
} else {
feedback.textContent = `恭喜你!猜对了!数字就是${numberToGuess}。你总共猜了${tries}次。`;
}
}
3. C# (Unity) 版本:猜数字游戏
在Unity中,你需要设置UI元素(如Text和Button)来接收输入和显示反馈。这里只给出C#脚本的核心部分。
C# (Script Component attached to a GameObject):
csharp
using System;
using UnityEngine;
using UnityEngine.UI;
public class GuessNumberGame : MonoBehaviour
{
public Text guessInput;
public Text feedbackText;
private int numberToGuess = UnityEngine.Random.Range(1, 101);
private int tries = 0;
public void CheckGuess()
{
if (!int.TryParse(guessInput.text, out int guess))
{
feedbackText.text = "请输入一个有效的数字!";
return;
}
tries++;
if (guess < numberToGuess)
{
feedbackText.text = "太小了!再试试看。";
}
else if (guess > numberToGuess)
{
feedbackText.text = "太大了!再试试看。";
}
else
{
feedbackText.text = $"恭喜你!猜对了!数字就是{numberToGuess}。你总共猜了{tries}次。";
}
// 可选:重置游戏
// numberToGuess = UnityEngine.Random.Range(1, 101);
// tries = 0;
// feedbackText.text = "";
// guess
由于篇幅限制,我将分别为你展示用三种流行的编程语言(Python、JavaScript(用于网页游戏)、和C#(用于Unity游戏引擎))编写的简单小游戏示例:猜数字游戏。
- Python 版本:猜数字游戏
python
import random
def guess_number_game():
number_to_guess = random.randint(1, 100)
print(“我想了一个1到100之间的数字,你能猜到是哪个吗?”)
tries = 0
while True:
try:
guess = int(input("请输入你的猜测:"))
tries += 1
if guess < number_to_guess:
print("太小了!再试试看。")
elif guess > number_to_guess:
print("太大了!再试试看。")
else:
print(f"恭喜你!猜对了!数字就是{number_to_guess}。你总共猜了{tries}次。")
break
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 tries = 0;
let feedback = document.getElementById(‘feedback’);
function checkGuess() {
const guess = parseInt(document.getElementById(‘guess’).value, 10);
tries++;
if (isNaN(guess)) {
feedback.textContent = "请输入一个有效的数字!";
return;
}
if (guess < numberToGuess) {
feedback.textContent = "太小了!再试试看。";
} else if (guess > numberToGuess) {
feedback.textContent = "太大了!再试试看。";
} else {
feedback.textContent = `恭喜你!猜对了!数字就是${numberToGuess}。你总共猜了${tries}次。`;
}
}
3. C# (Unity) 版本:猜数字游戏
在Unity中,你需要设置UI元素(如Text和Button)来接收输入和显示反馈。这里只给出C#脚本的核心部分。
C# (Script Component attached to a GameObject):
csharp
using System;
using UnityEngine;
using UnityEngine.UI;
public class GuessNumberGame : MonoBehaviour
{
public Text guessInput;
public Text feedbackText;
private int numberToGuess = UnityEngine.Random.Range(1, 101);
private int tries = 0;
public void CheckGuess()
{
if (!int.TryParse(guessInput.text, out int guess))
{
feedbackText.text = "请输入一个有效的数字!";
return;
}
tries++;
if (guess < numberToGuess)
{
feedbackText.text = "太小了!再试试看。";
}
else if (guess > numberToGuess)
{
feedbackText.text = "太大了!再试试看。";
}
else
{
feedbackText.text = $"恭喜你!猜对了!数字就是{numberToGuess}。你总共猜了{tries}次。";
}
// 可选:重置游戏
// numberToGuess = UnityEngine.Random.Range(1, 101);
// tries = 0;
// feedbackText.text = "";
// guess