- 猜数字游戏 (Python)dxwaiyan.cn
python
import random
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’恭喜你,猜对了!你用了{attempts}次尝试。')
2. 石头剪刀布游戏 (JavaScript, 在浏览器控制台中运行)
javascript
function getRandomChoice() {
const choices = [‘石头’, ‘剪刀’, ‘布’];
return choices[Math.floor(Math.random() * choices.length)];
}
let userChoice = prompt('请输入你的选择(石头/剪刀/布): ');
let computerChoice = getRandomChoice();
console.log(‘电脑选择了:’, computerChoice);
if (userChoice === computerChoice) {
console.log(‘平局!’);
} else if (
(userChoice === ‘石头’ && computerChoice === ‘剪刀’) ||
(userChoice === ‘剪刀’ && computerChoice === ‘布’) ||
(userChoice === ‘布’ && computerChoice === ‘石头’)
) {
console.log(‘你赢了!’);
} else {
console.log(‘你输了!’);
}
3. 简单的控制台射击游戏 (Java)
由于Java更适合开发图形用户界面(GUI)或基于窗口的游戏,这里仅提供一个非常简化的控制台版本。
java
import java.util.Scanner;
public class SimpleShootingGame {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int enemyHealth = 10;
int bulletCount = 5;
while (enemyHealth > 0 && bulletCount > 0) {
System.out.println("敌人生命值: " + enemyHealth + ", 剩余子弹: " + bulletCount);
System.out.println("输入'射击'或'退出':");
String input = scanner.nextLine();
if (input.equals("射击")) {
enemyHealth--;
bulletCount--;
System.out.println("射击成功!敌人生命值减少。");
} else if (input.equals("退出")) {
System.out.println("游戏结束。");
break;
} else {
System.out.println("无效输入!");
}
}
if (enemyHealth <= 0) {
System.out.println("你赢了!");
} else {
System.out.println("你输了,子弹用完了。");
}
}
}
这些示例都是非常简单的小游戏,但它们应该能够为你提供一个在不同编程语言中编写游戏的基础概念。由于篇幅限制,我无法直接为所有编程语言都提供一个完整的游戏代码,但我可以为你提供几个简单的小游戏概念及其在不同编程语言中的伪代码或简短示例。
- 猜数字游戏 (Python)
python
import random
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’恭喜你,猜对了!你用了{attempts}次尝试。')
2. 石头剪刀布游戏 (JavaScript, 在浏览器控制台中运行)
javascript
function getRandomChoice() {
const choices = [‘石头’, ‘剪刀’, ‘布’];
return choices[Math.floor(Math.random() * choices.length)];
}
let userChoice = prompt('请输入你的选择(石头/剪刀/布): ');
let computerChoice = getRandomChoice();
console.log(‘电脑选择了:’, computerChoice);
if (userChoice === computerChoice) {
console.log(‘平局!’);
} else if (
(userChoice === ‘石头’ && computerChoice === ‘剪刀’) ||
(userChoice === ‘剪刀’ && computerChoice === ‘布’) ||
(userChoice === ‘布’ && computerChoice === ‘石头’)
) {
console.log(‘你赢了!’);
} else {
console.log(‘你输了!’);
}
3. 简单的控制台射击游戏 (Java)
由于Java更适合开发图形用户界面(GUI)或基于窗口的游戏,这里仅提供一个非常简化的控制台版本。
java
import java.util.Scanner;
public class SimpleShootingGame {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int enemyHealth = 10;
int bulletCount = 5;
while (enemyHealth > 0 && bulletCount > 0) {
System.out.println("敌人生命值: " + enemyHealth + ", 剩余子弹: " + bulletCount);
System.out.println("输入'射击'或'退出':");
String input = scanner.nextLine();
if (input.equals("射击")) {
enemyHealth--;
bulletCount--;
System.out.println("射击成功!敌人生命值减少。");
} else if (input.equals("退出")) {
System.out.println("游戏结束。");
break;
} else {
System.out.println("无效输入!");
}
}
if (enemyHealth <= 0) {
System.out.println("你赢了!");
} else {
System.out.println("你输了,子弹用完了。");
}
}
}
这些示例都是非常简单的小游戏,但它们应该能够为你提供一个在不同编程语言中编写游戏的基础概念。