
Python: 猜数字游戏
python
import random
def game():
number_to_guess = random.randint(1, 100) dgynx.cn
guess = None
attempts = 0
while guess != number_to_guess:
guess = int(input('猜一个1到100之间的数字: '))
attempts += 1
if guess < number_to_guess:
print('太低了')
elif guess > number_to_guess:
print('太高了')
print(f'恭喜你,你猜对了!数字是 {number_to_guess}。你用了 {attempts} 次尝试。')
if name == “main”:
game()
JavaScript: 简单的网页点击计数器
html
0
<script>
let count = 0;
function increment() {
count++;
document.getElementById('counter').textContent = count;
}
</script>
C++: 控制台中的简单猜拳游戏
cpp
#include
#include
#include
enum class Choice { ROCK, PAPER, SCISSORS };
Choice getComputerChoice() {
std::srand(static_cast(std::time(0)));
return static_cast(std::rand() % 3);
}
std::string getChoiceName(Choice choice) {
switch (choice) {
case Choice::ROCK: return “石头”;
case Choice::PAPER: return “布”;
case Choice::SCISSORS: return “剪刀”;
}
}
void playRound(Choice playerChoice, Choice computerChoice) {
if (playerChoice == computerChoice) {
std::cout << “平局!” << std::endl;
} else if ((playerChoice == Choice::ROCK && computerChoice == Choice::SCISSORS) ||
(playerChoice == Choice::PAPER && computerChoice == Choice::ROCK) ||
(playerChoice == Choice::SCISSORS && computerChoice == Choice::PAPER)) {
std::cout << “你赢了!” << std::endl;
} else {
std::cout << “你输了!” << std::endl;
}
}
int main() {
Choice playerChoice;
std::cout << "请选择(1=石头, 2=布, 3=剪刀): ";
int input;
std::cin >> input;
switch (input) {
case 1: playerChoice = Choice::ROCK; break;
case 2: playerChoice = Choice::PAPER; break;
case 3: playerChoice = Choice::SCISSORS; break;
default: std::cout << “无效的选择!” << std::endl; return 1;
}
Choice computerChoice = getComputerChoice();
std::cout << "电脑选择了: " << getChoiceName(computerChoice) << std::endl;
playRound(playerChoice, computerChoice);
return 0;
}
请注意,这些代码示例是为了演示目的而简化的,并且没有包含错误处理或复杂的游戏逻辑。在开发完整的游戏时,你可能需要考虑更多的因素,如用户界面、图形、音效、网络通信等。由于不同编程语言的特点和用途各异,且一个完整的游戏可能涉及很多代码,我会为你提供几个简单的“小游戏”代码示例,分别使用 Python(易于教学和通用)、JavaScript(常用于网页开发)、和 C++(用于更复杂的应用和游戏开发)。
Python: 猜数字游戏
python
import random
def game():
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
while guess != number_to_guess:
guess = int(input('猜一个1到100之间的数字: '))
attempts += 1
if guess < number_to_guess:
print('太低了')
elif guess > number_to_guess:
print('太高了')
print(f'恭喜你,你猜对了!数字是 {number_to_guess}。你用了 {attempts} 次尝试。')
if name == “main”:
game()
JavaScript: 简单的网页点击计数器
html
0
<script>
let count = 0;
function increment() {
count++;
document.getElementById('counter').textContent = count;
}
</script>
C++: 控制台中的简单猜拳游戏
cpp
#include
#include
#include
enum class Choice { ROCK, PAPER, SCISSORS };
Choice getComputerChoice() {
std::srand(static_cast(std::time(0)));
return static_cast(std::rand() % 3);
}
std::string getChoiceName(Choice choice) {
switch (choice) {
case Choice::ROCK: return “石头”;
case Choice::PAPER: return “布”;
case Choice::SCISSORS: return “剪刀”;
}
}
void playRound(Choice playerChoice, Choice computerChoice) {
if (playerChoice == computerChoice) {
std::cout << “平局!” << std::endl;
} else if ((playerChoice == Choice::ROCK && computerChoice == Choice::SCISSORS) ||
(playerChoice == Choice::PAPER && computerChoice == Choice::ROCK) ||
(playerChoice == Choice::SCISSORS && computerChoice == Choice::PAPER)) {
std::cout << “你赢了!” << std::endl;
} else {
std::cout << “你输了!” << std::endl;
}
}
int main() {
Choice playerChoice;
std::cout << "请选择(1=石头, 2=布, 3=剪刀): ";
int input;
std::cin >> input;
switch (input) {
case 1: playerChoice = Choice::ROCK; break;
case 2: playerChoice = Choice::PAPER; break;
case 3: playerChoice = Choice::SCISSORS; break;
default: std::cout << “无效的选择!” << std::endl; return 1;
}
Choice computerChoice = getComputerChoice();
std::cout << "电脑选择了: " << getChoiceName(computerChoice) << std::endl;
playRound(playerChoice, computerChoice);
return 0;
}
请注意,这些代码示例是为了演示目的而简化的,并且没有包含错误处理或复杂的游戏逻辑。在开发完整的游戏时,你可能需要考虑更多的因素,如用户界面、图形、音效、网络通信等。
2185

被折叠的 条评论
为什么被折叠?



