-
Python (使用类来模拟)
python
class Game:
def init(self, name, price):self.name = name self.price = price
class GameStore:
def init(self):
self.games = []
def add_game(self, game):
self.games.append(game) hy029.cnfmtc.cn
def buy_game(self, game_name):
for game in self.games:
if game.name == game_name:
print(f"You have purchased {game.name} for {game.price}!")
# 这里可以添加删除游戏或更新库存的逻辑
return True
print("Game not found!")
return False
使用示例
store = GameStore()
store.add_game(Game(“Counter-Strike”, 19.99))
store.add_game(Game(“Dota 2”, 0.00)) # 免费游戏
store.buy_game(“Counter-Strike”)
2. JavaScript (使用对象和数组)
javascript
// 游戏对象
const game1 = {
name: “Counter-Strike”,
price: 19.99
};
const game2 = {
name: “Dota 2”,
price: 0
};
// 游戏商城
const gameStore = {
games: [game1, game2],
buyGame: function(gameName) {
for (let game of this.games) {
if (game.name === gameName) {
console.log(You have purchased ${game.name} for ${game.price}!
);
// 这里可以添加删除游戏或更新库存的逻辑
return true;
}
}
console.log(“Game not found!”);
return false;
}
};
// 使用示例
gameStore.buyGame(“Counter-Strike”);
3. Java (使用类和集合)
java
import java.util.ArrayList;
import java.util.List;
class Game {
String name;
double price;
Game(String name, double price) {
this.name = name;
this.price = price;
}
}
class GameStore {
List games = new ArrayList<>();
void addGame(Game game) {
games.add(game);
}
boolean buyGame(String gameName) {
for (Game game : games) {
if (game.name.equals(gameName)) {
System.out.println("You have purchased " + game.name + " for " + game.price + "!");
// 这里可以添加删除游戏或更新库存的逻辑
return true;
}
}
System.out.println("Game not found!");
return false;
}
}
// 使用示例
public class Main {
public static void main(String[] args) {
GameStore store = new GameStore();
store.addGame(new Game(“Counter-Strike”, 19.99));
store.addGame(new Game(“Dota 2”, 0.0));
store.buyGame(“Counter-Strike”);
}
}
由于篇幅限制,我无法为所有编程语言都提供一个完整的小程序游戏代码,但我可以为你提供几个流行编程语言(如Python、JavaScript和Java)的简单小游戏示例代码,这些示例都是基于控制台(命令行)的。
Python 示例:猜数字游戏
python
import random
def guess_number():
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} 次。')
guess_number()
JavaScript 示例:控制台版“石头剪刀布”
注意:这个示例需要在Node.js环境中运行,或者你可以将其嵌入到HTML文件中并使用浏览器的控制台。
javascript
function getPlayerChoice() {
const choices = [‘石头’, ‘剪刀’, ‘布’];
return choices[Math.floor(Math.random() * choices.length)];
}
function determineWinner(playerChoice, computerChoice) {
if (playerChoice === computerChoice) {
return ‘平局’;
} else if (
(playerChoice === ‘石头’ && computerChoice === ‘剪刀’) ||
(playerChoice === ‘剪刀’ && computerChoice === ‘布’) ||
(playerChoice === ‘布’ && computerChoice === ‘石头’)
) {
return ‘你赢了’;
} else {
return ‘你输了’;
}
}
function playRockPaperScissors() {
const playerChoice = getPlayerChoice(); // 实际上这里玩家没有选择,只是为了示例
const computerChoice = getPlayerChoice();
console.log(你出了: ${playerChoice}
);
console.log(电脑出了: ${computerChoice}
);
console.log(determineWinner(playerChoice, computerChoice));
}
playRockPaperScissors();
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;
int attempts = 0;
do {
System.out.print("猜一个1到100之间的数字: ");
guess = scanner.nextInt();
attempts++;
if (guess < numberToGuess) {
System.out.println("太小了!");
} else if (guess > numberToGuess) {
System.out.println("太大了!");
}
} while (guess != numberToGuess);
System.out.println("恭喜你,猜对了!数字是 " + numberToGuess + ",你尝试了 " + attempts + " 次。");
}
}