Python 版本
python
import random
def guess_number_game():
number_to_guess = random.randint(1, 100) tzxl8023.com
guess = None
num_guesses = 0
while guess != number_to_guess:
guess = int(input("猜一个1到100之间的数字:"))
num_guesses += 1
if guess < number_to_guess:
print("太小了!")
elif guess > number_to_guess:
print("太大了!")
print(f"恭喜你!你猜对了!数字是 {number_to_guess}。你总共猜了 {num_guesses} 次。")
guess_number_game()
JavaScript (HTML5 Canvas) 版本
HTML:
html
JavaScript (game.js):javascript
const canvas = document.getElementById(‘gameCanvas’);
const ctx = canvas.getContext(‘2d’);
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let numGuesses = 0;
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.font = ‘20px Arial’;
ctx.fillStyle = ‘black’;
ctx.fillText(‘猜一个1到100之间的数字:’, 20, 50);
}
function checkGuess(guess) {
numGuesses++;
if (guess < numberToGuess) {
alert(‘太小了!’);
} else if (guess > numberToGuess) {
alert(‘太大了!’);
} else {
alert(恭喜你!你猜对了!数字是 ${numberToGuess}。你总共猜了 ${numGuesses} 次。
);
location.reload(); // 重启游戏
}
}
draw();
canvas.addEventListener(‘click’, function() {
let guess = prompt(‘请输入你猜的数字:’);
checkGuess(parseInt(guess));
});
Java 版本
java
import java.util.Random;
import java.util.Scanner;
public class GuessNumberGame {
public static void main(String[] args) {
Random rand = new Random();
int numberToGuess = rand.nextInt(100) + 1;
Scanner scanner = new Scanner(System.in);
int guess = 0;
int numGuesses = 0;
System.out.println("猜一个1到100之间的数字:");
while (guess != numberToGuess) {
guess = scanner.nextInt();
numGuesses++;
if (guess < numberToGuess) {
System.out.println("太小了!");
} else if (guess > numberToGuess) {
System.out.println("太大了!");
}
}
System.out.println("恭喜你!你猜对了!数字是 " + numberToGuess + "。你总共猜了 " + numGuesses + " 次。");
}
}
这些代码分别用Python、JavaScript和Java实现了一个简单的猜数字游戏。你可以根据自己的需要和环境选择适合的语言版本。当然可以。下面我将为你提供使用Python、JavaScript(HTML5 Canvas)和Java三种不同语言编写的简单的小游戏代码。我们将创建一个简单的猜数字游戏,玩家需要猜一个介于1到100之间的随机数。
Python 版本
python
import random
def guess_number_game():
number_to_guess = random.randint(1, 100)
guess = None
num_guesses = 0
while guess != number_to_guess:
guess = int(input("猜一个1到100之间的数字:"))
num_guesses += 1
if guess < number_to_guess:
print("太小了!")
elif guess > number_to_guess:
print("太大了!")
print(f"恭喜你!你猜对了!数字是 {number_to_guess}。你总共猜了 {num_guesses} 次。")
guess_number_game()
JavaScript (HTML5 Canvas) 版本
HTML:
html
JavaScript (game.js):javascript
const canvas = document.getElementById(‘gameCanvas’);
const ctx = canvas.getContext(‘2d’);
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let numGuesses = 0;
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.font = ‘20px Arial’;
ctx.fillStyle = ‘black’;
ctx.fillText(‘猜一个1到100之间的数字:’, 20, 50);
}
function checkGuess(guess) {
numGuesses++;
if (guess < numberToGuess) {
alert(‘太小了!’);
} else if (guess > numberToGuess) {
alert(‘太大了!’);
} else {
alert(恭喜你!你猜对了!数字是 ${numberToGuess}。你总共猜了 ${numGuesses} 次。
);
location.reload(); // 重启游戏
}
}
draw();
canvas.addEventListener(‘click’, function() {
let guess = prompt(‘请输入你猜的数字:’);
checkGuess(parseInt(guess));
});
Java 版本
java
import java.util.Random;
import java.util.Scanner;
public class GuessNumberGame {
public static void main(String[] args) {
Random rand = new Random();
int numberToGuess = rand.nextInt(100) + 1;
Scanner scanner = new Scanner(System.in);
int guess = 0;
int numGuesses = 0;
System.out.println("猜一个1到100之间的数字:");
while (guess != numberToGuess) {
guess = scanner.nextInt();
numGuesses++;
if (guess < numberToGuess) {
System.out.println("太小了!");
} else if (guess > numberToGuess) {
System.out.println("太大了!");
}
}
System.out.println("恭喜你!你猜对了!数字是 " + numberToGuess + "。你总共猜了 " + numGuesses + " 次。");
}
}
这些代码分别用Python、JavaScript和Java实现了一个简单的猜数字游戏。你可以根据自己的需要和环境选择适合的语言版本。