linux系统下农场种菜小游戏!

linux系统下农场种菜小游戏!

今天给大家分享一个linux系统下一个简单的小游戏
在这里插入图片描述
源码如下,在linux系统下创建一个.sh的脚本文件,复制粘贴进去即可!

#!/bin/bash

# 初始化变量
vegetables=("生菜" "西兰花" "萝卜")
inventory=()
profit=0

# 显示菜单
show_menu() {
    echo "欢迎来到农场游戏!"
    echo "1.种菜"
    echo "2.收菜"
    echo "3.卖菜"
    echo "4.查看收益"
    echo "5.查看仓库"
    echo "6.退出"
}

# 种植蔬菜
plant_vegetable() {
    echo "请选择要种植的蔬菜:"
    for i in "${!vegetables[@]}"; do
        echo "$((i+1)). ${vegetables[$i]}"
    done
    read -p "输入序列号: " choice
    if [ $choice -ge 1 ] && [ $choice -le ${#vegetables[@]} ]; then
        inventory+=(${vegetables[$((choice-1))]})
        echo "已成功种植${vegetables[$((choice-1))]}。"
    else
        echo "无效的选择,请重新输入。"
    fi
}

# 收获蔬菜
harvest_vegetable() {
    echo "请选择要收获的蔬菜:"
    for i in "${!inventory[@]}"; do
        echo "$((i+1)). ${inventory[$i]}"
    done
    read -p "输入序列号: " choice
    if [ $choice -ge 1 ] && [ $choice -le ${#inventory[@]} ]; then
        profit=$((profit + 10))
        inventory=("${inventory[@]:0:$choice-1}" "${inventory[@]:$((choice))}")
        echo "已成功收获${inventory[$((choice-1))]}。"
    else
        echo "无效的选择,请重新输入。"
    fi
}

# 出售蔬菜
sell_vegetable() {
    echo "请选择要出售的蔬菜:"
    for i in "${!inventory[@]}"; do
        echo "$((i+1)). ${inventory[$i]}"
    done
    read -p "输入序列号: " choice
    if [ $choice -ge 1 ] && [ $choice -le ${#inventory[@]} ]; then
        profit=$((profit + 20))
        inventory=("${inventory[@]:0:$choice-1}" "${inventory[@]:$((choice))}")
        echo "已成功出售${inventory[$((choice-1))]}。"
    else
        echo "无效的选择,请重新输入。"
    fi
}

# 查看收益
check_profit() {
    echo "当前收益为:$profit"
}

# 查看仓库
check_inventory() {
    echo "当前仓库中的蔬菜有:"
    for vegetable in "${inventory[@]}"; do
        echo "$vegetable"
    done
}

# 主循环
while true; do
    show_menu
    read -p "请输入操作序号: " choice
    case $choice in
        1)
            plant_vegetable
            ;;
        2)
            harvest_vegetable
            ;;
        3)
            sell_vegetable
            ;;
        4)
            check_profit
            ;;
        5)
            check_inventory
            ;;
        6)
            echo "感谢使用农场游戏,再见!"
            exit 0
            ;;
        *)
            echo "无效的选择,请重新输入。"
            ;;
    esac
done

一键三连哦

  • 20
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一份用Java编写的种菜小游戏: import java.util.Scanner; public class VegetableGame { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int money = 100; // 玩家初始资金为100元 int day = 1; // 玩家初始第一天 int vegetableCount = 0; // 玩家初始没有种植蔬菜 int maxVegetableCount = 5; // 玩家最多可以种植5种蔬菜 while (true) { System.out.println("第" + day + "天,你有" + money + "元资金和" + vegetableCount + "个种植盒。"); System.out.println("请选择你要进行的操作:"); System.out.println("1. 购买种植盒"); System.out.println("2. 在一个种植盒中种植蔬菜"); System.out.println("3. 收获所有的蔬菜并出售"); System.out.println("4. 结束游戏"); int choice = scanner.nextInt(); switch (choice) { case 1: if (money < 10) { System.out.println("你的资金不足,无法购买种植盒!"); } else if (vegetableCount >= maxVegetableCount) { System.out.println("你的种植盒已经满了,无法再购买种植盒!"); } else { money -= 10; vegetableCount++; System.out.println("购买成功,你现在有" + vegetableCount + "个种植盒。"); } break; case 2: if (vegetableCount == 0) { System.out.println("你没有种植盒,无法种植蔬菜!"); } else { System.out.println("请选择你要在哪个种植盒中种植蔬菜:"); for (int i = 1; i <= vegetableCount; i++) { System.out.println(i + ". 种植盒" + i); } int boxIndex = scanner.nextInt(); if (boxIndex < 1 || boxIndex > vegetableCount) { System.out.println("无效的选择!"); } else { System.out.println("请选择你要种植什么蔬菜:"); System.out.println("1. 番茄(收成20元)"); System.out.println("2. 茄子(收成30元)"); System.out.println("3. 黄瓜(收成15元)"); int vegetableChoice = scanner.nextInt(); int cost = 0; int income = 0; switch (vegetableChoice) { case 1: cost = 15; income = 20; break; case 2: cost = 20; income = 30; break; case 3: cost = 10; income = 15; break; default: System.out.println("无效的选择!"); break; } if (money < cost) { System.out.println("你的资金不足,无法种植蔬菜!"); } else { money -= cost; System.out.println("种植成功,你在种植盒" + boxIndex + "中种植了一场蔬菜。"); // 记录下这个种植盒中种了哪种蔬菜以及预计成熟时间 } } } break; case 3: if (vegetableCount == 0) { System.out.println("你没有种植盒,也没有蔬菜可以收获!"); } else { int income = 0; // 遍历玩家所有的种植盒以计算总收益 for (int i = 1; i <= vegetableCount; i++) { // 如果这个种植盒里有蔬菜在生长中,那么就需要更新它的成长状态。 // 具体的实现可以通过记录下播种时间以及每个蔬菜需要的生长时间来实现。 // 如果到了成熟时间,就可以将这个蔬菜的收益加到总收益中并将这个种植盒清空。 // 每个种植盒最多只能种植一种蔬菜。 // 同时,收获后的种植盒还可以再次种植,不需要再花费购买种植盒的费用。 } money += income; System.out.println("你获得了" + income + "元收益。"); } break; case 4: System.out.println("你已结束游戏,本次游戏你获得了" + money + "元收益。"); return; default: System.out.println("无效的选择!"); break; } day++; } } } 请注意,此代码仅作示例用途,并未完全实现游戏。如果您需要完整的游戏代码,请自行编写或参考其他游戏的实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值