Python
python
import random
def guess_number_game():
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
while guess != number_to_guess:
try:
guess = int(input("猜一个1到100之间的数字: "))
attempts += 1
if guess < number_to_guess:
print("太小了!")
elif guess > number_to_guess:
print("太大了!")
except ValueError:
print("请输入一个数字!")
print(f"恭喜你,你猜对了!数字是 {number_to_guess},你用了 {attempts} 次尝试。")
guess_number_game()
JavaScript (Node.js 环境)
javascript
const readline = require(‘readline’).createInterface({
input: process.stdin,
output: process.stdout
});
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
readline.question('猜一个1到100之间的数字: ', (answer) => {
while (true) {
guess = parseInt(answer, 10);
if (isNaN(guess)) {
readline.question('请输入一个数字: ', (answer) => {
continue;
});
}
yctsy.cn
attempts++;
if (guess < numberToGuess) {
readline.question('太小了,再试一次: ', (answer) => {
continue;
});
} else if (guess > numberToGuess) {
readline.question('太大了,再试一次: ', (answer) => {
continue;
});
} else {
console.log(`恭喜你,你猜对了!数字是 ${numberToGuess},你用了 ${attempts} 次尝试。`);
readline.close();
}
break;
}
});
C++
C++ 的控制台输入/输出相对复杂,但这里是一个简化的示例:
cpp
#include
#include
#include
int main() {
std::srand(static_cast(std::time(0))); // 使用当前时间作为随机数种子
int numberToGuess = std::rand() % 100 + 1;
int guess = 0;
int attempts = 0;
std::cout << "猜一个1到100之间的数字: ";
while (guess != numberToGuess) {
std::cin >> guess;
attempts++;
if (guess < numberToGuess) {
std::cout << "太小了!再试一次: ";
} else if (guess > numberToGuess) {
std::cout << "太大了!再试一次: ";
}
}
std::cout << "恭喜你,你猜对了!数字是 " << numberToGuess << ",你用了 " << attempts << " 次尝试。" << std::endl;
return 0;
}
请注意,这些代码示例都是在控制台环境中运行的,并且可能需要根据你的具体环境进行微调。由于篇幅限制,我无法为所有编程语言都提供一个完整的小游戏代码,但我可以为你提供几种流行编程语言(如Python、JavaScript、Java和C++)的简单游戏示例代码:
Python(使用pygame库制作一个简单的移动矩形游戏)
python
import pygame
import sys
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption(“移动矩形游戏”)
rect = pygame.Rect(50, 50, 50, 50)
rect_speed = [2, 2]
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and rect.left > 0:
rect.left -= rect_speed[0]
if keys[pygame.K_RIGHT] and rect.right < 800:
rect.right += rect_speed[0]
if keys[pygame.K_UP] and rect.top > 0:
rect.top -= rect_speed[1]
if keys[pygame.K_DOWN] and rect.bottom < 600:
rect.bottom += rect_speed[1]
screen.fill((255, 255, 255))
pygame.draw.rect(screen, (0, 0, 255), rect)
pygame.display.update()
JavaScript(使用HTML和Canvas制作一个简单的移动小球游戏)
HTML:
html
javascript
const canvas = document.getElementById(‘gameCanvas’);
const ctx = canvas.getContext(‘2d’);
let ball = {
x: canvas.width / 2,
y: canvas.height - 30,
radius: 10,
dx: 2,
dy: -2
};
function drawBall() {
ctx.beginPath();
ctx.arc(ball.x, ball.y, ball.radius, 0, Math.PI * 2);
ctx.fillStyle = “#0095DD”;
ctx.fill();
ctx.closePath();
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBall();
if (ball.x + ball.dx > canvas.width - ball.radius || ball.x + ball.dx < ball.radius) {
ball.dx = -ball.dx;
}
if (ball.y + ball.dy < ball.radius) {
ball.dy = -ball.dy;
}
ball.x += ball.dx;
ball.y += ball.dy;
requestAnimationFrame(draw);
}
draw();
Java(使用Swing库制作一个简单的猜数字游戏)
由于Java Swing代码较长,这里只提供一个简单的框架。
C++(使用控制台输出制作一个简单的猜数字游戏)
cpp
#include
#include
#include
int main() {
std::srand(static_cast(std::time(nullptr)));
int secretNumber = std::rand() % 100 + 1;
int guess;
std::cout << "猜一个1到100之间的数字: ";
std::cin >> guess;
while (guess != secretNumber) {
if (guess < secretNumber) {
std::cout << "太小了,再试一次: ";
} else {
std::cout << "太大了,再试一次: ";
}
std::cin >> guess;
}
std::cout << "恭喜你,猜对了!" << std::endl;
return 0;
}