raspberry pi(中文名为“树莓派”,简写为rpi,(或者raspi / rpi)是为学习计算机编程教育而设计,只有信用卡大小的微型电脑,其系统基于linux。随着windows 10 iot的发布,我们也将可以用上运行windows的树莓派。
自问世以来,受众多计算机发烧友和创客的追捧,曾经一“派”难求。别看其外表“娇小”,内“心”却很强大,视频、音频等功能通通皆有,可谓是“麻雀虽小,五脏俱全”。本文主要就是说明基于树莓派python编程的游戏程序,一起来了解一下。
raspbian的系统中包含了python的编程环境ide,方便了我们对于python的学习。个人觉得还是游戏程序能够更好的激发我们的潜力所以,话不多说下面就来使用python编写我们简单的游戏程序吧。
储备知识
①import 模块:time时间模块、random随机数模块
②列表:[ ] #[]中放入值,与数组很像,但是不同
③def:定义函数
④python的缩进是编程格式的一种,if等都不需要{}括起来,关键看缩进区分
⑤if,while,def函数都要以 “:”开始
游戏概述
一进入游戏,主人公就会随机获取一个游戏已经设定的道具。最开始,主人公站在丛林的边缘,只有左右可选,左边是个洞穴,右边是沙滩。
选择左边,则开始可以获得手电筒,但是在洞穴中会碰到蛇hp-20,继续向下会到一个未知空间,游戏结束。
选择右边,会到沙滩 获得凉爽的水hp+70,但是由于没有游泳装备,如果继续选择游泳,会遇到海啸而死亡。
代码
#python adventure game
import time
import random
#define variable health point
hp = 30
#define variable object_ to show what the adventurer has
object_=[]
#define variable what the game sets
tools = [“torch”,“rope”,“spanner”,“50hp”,“10hp”]
#set two acceptable answers ,select any one to end loop
def get_input(prompt,accepted):
while true:
value = input(prompt).lower()
if value in accepted:
return value
else :
print(“that is not a recognised answer,must be one of”,accepted)
def handle_room(location) :
global hp
if location== “start”:
print(“you are stading on a path at the edge of a jungle.there is a cave to your left and a beach to your right.”)
object_.append(random.choice(tools))
print(“lucky,you have gained”,object_[-1])
use_tools(object_)
direction = get_input(“do you want to go left or right?”,[“left”,“right”])
if direction == “left”:
return “cave”
elif direction == “right”:
return “beach”
elif location == “cave”:
print(“on the entrance of cave,you find a torch!!!”)
object_.append(“torch”)
print(“you walk to the cave and notice there is an opening.”)
print(“a small snake bites you,and you lose 20 health points.”)
hp-=20
answer = get_input(“do you want to go deeper?(y/n)”,[“y”,“n”])
if answer == “y”:
return “deep_cave”
elif answer == “n”:
return “start”
elif location == “beach”:
print(“you walk to the beach but remember you do not have any swimwming equipment.”)
print(“the cool water revitalizes you.you have never felt more alive,gain 70 health points.”)
hp+=70
answer = get_input(“do you want to go swimming?(y/n)”,[“y”,“n”])
if answer == “y”:
return “sea”
elif answer == “n”:
return “start”
elif location == “sea”:
print(“suddenly there was a tsunami,you can‘t escape.”)
hp=0
return “end”
else :
print(“programmer error,room”,location,“is unknown”)
return “end”
#define the function to use the tools of hp
def use_tools(tool):
global hp
length = len(tool)
for i in range(0,length):
if tool[i]==“50hp”:
hp+=50
tool.pop(i)
print(“you have use the tool of 50hp”)
j+=1
elif tool[i]==“10hp”:
hp+=10
tool.pop(i)
print(“you have use the tool of 10hp”)
j+=1
#the begin of the program.
location = “start”
#loop until we reach the special “end” location
while location!=“end”:
location = handle_room(location)
#check we are not dead each return
print(“you now have”,hp,“health points.”)
if hp《=0:
print(“you are dead.\ngame over!!!”)
break
print(“your adventure has ended,bye~”)
函数功能块简析
get_input(prompt,accepted) //(输入提示,可接受字符串)
handle_room(location) //通过位置的判断给出不同场景的设定和人物的走向
use_tools(tool) //人物若随机获得了hp类物品则立即使用,其他则无任何操作
运行截图
迈动互联成功交付浙江省体育局智慧体育大脑项目
赫迈米姆空军基地逼近一群无人机来历不明,俄驻叙部队将其击落
欧姆表的内阻_欧姆表内部的电路图
金升阳携新一代隔离栅登陆高交会
LTspice:使用.MEAS和 .用于计算效率的STEP命令
基于树莓派python编程的游戏程序
API安全“芯”方案丨芯盾时代APISEC安全平台 助力企业构建API安全管理体系
明纬电源——300W可信赖医疗型电源供应器(RPS-300系列)
什么是网络协议
霍尔水流量传感器内部结构_分类和选型
中欣晶圆:8英寸半导体硅片年产量可达420万枚
位置控制输出组件电路图
Ad8488笔记:积分电荷放大器原理
独立AR眼镜Rokid Glass发布预计于年底量产
电连接器是日常生活的安全保障
新款Moto X首次亮相:全金属+双摄像头
中国移动开展“5G终端嗨购节”直播带货大联播活动
M5256-00000W-010BG压力传感器的调试方法
2350亿!AMD宣布收购FPGA大厂赛灵思
华为Mate10什么时候上市?华为Mate10再次被确认,顶级的配置和出众的颜值,价格也不便宜