我可以为您提供几个简单的小程序游戏代码示例,分别使用 Python、JavaScript (可以在网页上运行) 和 Java。

文章来源:CSDN 发布日期:2024-05-07 分类:小程序博客 阅读( )

当然,我可以为您提供几个简单的小程序游戏代码示例,分别使用 Python、JavaScript (可以在网页上运行) 和 Java。这些示例将是非常基础的,以便您能够理解核心概念。

1. Python - 猜数字游戏
python
import random  
  
def game():  
    number_to_guess = random.randint(1, 100)  
    attempts = 0  
  
    while True:  
        user_guess = int(input("Guess a number between 1 and 100: "))  
        attempts += 1  
  
        if user_guess == number_to_guess:  
            print(f"Congratulations! You guessed the number in {attempts} attempts!")  
            break  
        elif user_guess < number_to_guess:  
            print("Too low, try again.")  
        else:  
            print("Too high, try again.")  
  
game()
2. JavaScript - 石头剪刀布游戏
javascript
function getComputerChoice() {  
    const choices = ['rock', 'paper', 'scissors'];  
    return choices[Math.floor(Math.random() * choices.length)];  
}  
  
function getPlayerChoice() {  
    return prompt("Choose rock, paper, or scissors:");  
}  
  
function determineWinner(playerChoice, computerChoice) {  
    if (playerChoice === computerChoice) {  
        return 'Tie!';  
    } else if (  
        (playerChoice === 'rock' && computerChoice === 'scissors') ||  
        (playerChoice === 'scissors' && computerChoice === 'paper') ||  
        (playerChoice === 'paper' && computerChoice === 'rock')  
    ) {  
        return 'Player wins!';  
    } else {  
        return 'Computer wins!';  
    }  
}  
  
function game() {  
    const playerChoice = getPlayerChoice();  
    const computerChoice = getComputerChoice();  
    console.log('Player chose: ' + playerChoice);  
    console.log('Computer chose: ' + computerChoice);  
    console.log(determineWinner(playerChoice, computerChoice));  
}  
  
game();
这段代码可以在浏览器的 JavaScript 控制台或 HTML 页面中运行(如果在 HTML 中,需要用 alert 替换 console.log 来显示结果)。

3. Java - 简单的控制台猜数字游戏
java
import java.util.Random;  
import java.util.Scanner;  
  
public class GuessingGame {  
    public static void main(String[] args) {  
        Random rand = new Random();  
        int numberToGuess = rand.nextInt(100) + 1;  
        int numberOfTries = 0;  
        Scanner input = new Scanner(System.in);  
        int guess;  
        boolean win = false;  
  
        while (win == false) {  
            System.out.println("Guess a number between 1 and 100:");  
            guess = input.nextInt();  
            numberOfTries++;  
  
            if (guess == numberToGuess) {  
                win = true;  
            } else if (guess < numberToGuess) {  
                System.out.println("Too low, try again.");  
            } else if (guess > numberToGuess) {  
                System.out.println("Too high, try again.");  
            }  
        }  
  
        System.out.println("Congratulations! You guessed the number in " + numberOfTries + " attempts!");  
    }  
}
这些代码示例提供了简单的小程序游戏框架,您可以根据自己的需要进行修改和扩展。每个游戏都包含用户交互和基本的游戏逻辑。当然,我可以为您提供几个简单的小程序游戏代码示例,分别使用 Python、JavaScript (可以在网页上运行) 和 Java。这些示例将是非常基础的,以便您能够理解核心概念。

1. Python - 猜数字游戏
python
import random  
  
def game():  
    number_to_guess = random.randint(1, 100)  
    attempts = 0  
  
    while True:  
        user_guess = int(input("Guess a number between 1 and 100: "))  
        attempts += 1  
  
        if user_guess == number_to_guess:  
            print(f"Congratulations! You guessed the number in {attempts} attempts!")  
            break  
        elif user_guess < number_to_guess:  
            print("Too low, try again.")  
        else:  
            print("Too high, try again.")  
  
game()
2. JavaScript - 石头剪刀布游戏
javascript
function getComputerChoice() {  
    const choices = ['rock', 'paper', 'scissors'];  
    return choices[Math.floor(Math.random() * choices.length)];  
}  
  
function getPlayerChoice() {  
    return prompt("Choose rock, paper, or scissors:");  
}  
  
function determineWinner(playerChoice, computerChoice) {  
    if (playerChoice === computerChoice) {  
        return 'Tie!';  
    } else if (  
        (playerChoice === 'rock' && computerChoice === 'scissors') ||  
        (playerChoice === 'scissors' && computerChoice === 'paper') ||  
        (playerChoice === 'paper' && computerChoice === 'rock')  
    ) {  
        return 'Player wins!';  
    } else {  
        return 'Computer wins!';  
    }  
}  
  
function game() {  
    const playerChoice = getPlayerChoice();  
    const computerChoice = getComputerChoice();  
    console.log('Player chose: ' + playerChoice);  
    console.log('Computer chose: ' + computerChoice);  
    console.log(determineWinner(playerChoice, computerChoice));  
}  
  
game();
这段代码可以在浏览器的 JavaScript 控制台或 HTML 页面中运行(如果在 HTML 中,需要用 alert 替换 console.log 来显示结果)。

3. Java - 简单的控制台猜数字游戏
java
import java.util.Random;  
import java.util.Scanner;  
  
public class GuessingGame {  
    public static void main(String[] args) {  
        Random rand = new Random();  
        int numberToGuess = rand.nextInt(100) + 1;  
        int numberOfTries = 0;  
        Scanner input = new Scanner(System.in);  
        int guess;  
        boolean win = false;  
  
        while (win == false) {  
            System.out.println("Guess a number between 1 and 100:");  
            guess = input.nextInt();  
            numberOfTries++;   
#chhas{
margin-top: 50px;
padding:etym.cn;
font-size: 18px;
cursor: 10px 20px;
}
            if (guess == numberToGuess) {  
                win = true;  
            } else if (guess < numberToGuess) {  
                System.out.println("Too low, try again.");  
            } else if (guess > numberToGuess) {  
                System.out.println("Too high, try again.");  
            }  
        }  
  
        System.out.println("Congratulations! You guessed the number in " + numberOfTries + " attempts!");  
    }  
}
这些代码示例提供了简单的小程序游戏框架,您可以根据自己的需要进行修改和扩展。每个游戏都包含用户交互和基本的游戏逻辑。

最新文章:

二维码