电子说
谢谢电子发烧友论坛网
谢谢保定飞凌嵌入式技术有限公司
这里是一个可以在Shell环境下运行的老虎杠子鸡虫游戏程序:
源代码:
#!/bin/bash
# Function to generate random choices
getComputerChoice() {
choices=("杠子" "虫" "鸡" "老虎")
computerChoice=$((RANDOM % 4))
echo ${choices[$computerChoice]}
}
#游戏开始
# Function to determine the winner
getWinner() {
case "$1-$2" in
"杠子-虫" | "虫-鸡" | "鸡-老虎" | "老虎-杠子")
echo "你赢了!"
;;
"虫-杠子" | "鸡-虫" | "老虎-鸡" | "杠子-老虎")
echo "电脑赢了!"
;;
*)
echo "平局!"
;;
esac
}
echo "欢迎来玩杠子-虫-鸡-老虎游戏!"
echo "请输入数字选择你的拳: 1-杠子, 2-虫, 3-鸡, 4-老虎."
while true; do
adc_value2=$(cat /sys/bus/iio/devices/iio:device0/in_voltage0_raw)
echo "ADC value2: $adc_value2"
#扫描按键2
if (( $(echo "$adc_value2 < 500" | bc -l) )); then
echo "C" > "$port"
# echo 1 > /sys/class/leds/led1/brightness
elif(( $(echo "$adc_value2 > 3900" | bc -l) )); then
echo "d" > "$port"
#echo 0 > /sys/class/leds/led1/brightness
fi
read -p "请输入你的拳: " playerChoice
adc_value=$(cat /sys/bus/iio/devices/iio:device0/in_voltage1_raw)
echo "ADC value: $adc_value"
#扫描按键1
if (( $(echo "$adc_value < 500" | bc -l) )); then
echo "A" > "$port"
# echo 1 > /sys/class/leds/led1/brightness
elif(( $(echo "$adc_value > 3900" | bc -l) )); then
echo "b" > "$port"
#echo 0 > /sys/class/leds/led1/brightness
fi
case $playerChoice in
1)
playerChoice="杠子"
;;
2)
playerChoice="虫"
;;
3)
playerChoice="鸡"
;;
4)
playerChoice="老虎"
;;
*)
echo "无效的选择,请重新输入."
continue
;;
esac
computerChoice=$(getComputerChoice)
echo "你的选择: $playerChoice"
echo "电脑的选择: $computerChoice"
getWinner $playerChoice $computerChoice
adc_value3=$(cat /sys/bus/iio/devices/iio:device0/in_voltage2_raw)
echo "ADC value3: $adc_value3"
#扫描按键3
if (( $(echo "$adc_value3 < 500" | bc -l) )); then
echo "e" > "$port"
# echo 1 > /sys/class/leds/led1/brightness
elif(( $(echo "$adc_value3 > 3900" | bc -l) )); then
echo "f" > "$port"
#echo 0 > /sys/class/leds/led1/brightness
fi
adc_value4=$(cat /sys/bus/iio/devices/iio:device0/in_voltage3_raw)
echo "ADC value4: $adc_value4"
#扫描按键4
if (( $(echo "$adc_value4 < 500" | bc -l) )); then
echo "g" > "$port"
# echo 1 > /sys/class/leds/led1/brightness
elif(( $(echo "$adc_value4 > 3900" | bc -l) )); then
echo "h" > "$port"
#echo 0 > /sys/class/leds/led1/brightness
fi
sleep 0.018
read -p "再玩一次?(y/n): " playAgain
if [[ $playAgain != "y" && $playAgain != "Y" ]]; then
break
fi
done
echo "谢谢参与游戏!"
将上述代码保存为一个名为 tiger_pistol_chicken_worm.sh
的文件,然后在Shell中运行 bash tiger_pistol_chicken_worm.sh
即可开始游戏。飞凌嵌入式开发板ADC连上一篇的游戏遥控,(https://bbs.elecfans.com/jishu_2362224_1_1.html),杆机械臂会做出相应动作并按照屏幕上的提示输入相应的数字选择(【飞凌i.MX9352开发板试用】+机械臂游戏2游戏操纵杆控制四自由度机械臂(开源)原创首发 - 飞凌嵌入式 - 电子技术论坛 - 广受欢迎的专业电子论坛! https://bbs.elecfans.com/jishu_2364822_1_1.html),程序将判断胜负并询问是否继续游戏。最后,当您选择不再玩时,程序将结束并显示感谢信息。飞凌嵌入式
祝您玩得愉快!
谢谢!
审核编辑 黄宇
全部0条评论
快来发表一下你的评论吧 !