Raspberry Pi(中文名為“樹(shù)莓派”,簡(jiǎn)寫為RPi,(或者RasPi / RPI)是為學(xué)習(xí)計(jì)算機(jī)編程教育而設(shè)計(jì),只有信用卡大小的微型電腦,其系統(tǒng)基于Linux。隨著Windows 10 IoT的發(fā)布,我們也將可以用上運(yùn)行Windows的樹(shù)莓派。
自問(wèn)世以來(lái),受眾多計(jì)算機(jī)發(fā)燒友和創(chuàng)客的追捧,曾經(jīng)一“派”難求。別看其外表“嬌小”,內(nèi)“心”卻很強(qiáng)大,視頻、音頻等功能通通皆有,可謂是“麻雀雖小,五臟俱全”。本文主要就是說(shuō)明基于樹(shù)莓派python編程的游戲程序,一起來(lái)了解一下。
Raspbian的系統(tǒng)中包含了python的編程環(huán)境IDE,方便了我們對(duì)于python的學(xué)習(xí)。個(gè)人覺(jué)得還是游戲程序能夠更好的激發(fā)我們的潛力所以,話不多說(shuō)下面就來(lái)使用python編寫我們簡(jiǎn)單的游戲程序吧。
儲(chǔ)備知識(shí)
①import 模塊:time時(shí)間模塊、random隨機(jī)數(shù)模塊
②列表:[ ] #[]中放入值,與數(shù)組很像,但是不同
?、踕ef:定義函數(shù)
?、躳ython的縮進(jìn)是編程格式的一種,if等都不需要{}括起來(lái),關(guān)鍵看縮進(jìn)區(qū)分
?、輎f,while,def函數(shù)都要以 “:”開(kāi)始
游戲概述
一進(jìn)入游戲,主人公就會(huì)隨機(jī)獲取一個(gè)游戲已經(jīng)設(shè)定的道具。最開(kāi)始,主人公站在叢林的邊緣,只有左右可選,左邊是個(gè)洞穴,右邊是沙灘。
選擇左邊,則開(kāi)始可以獲得手電筒,但是在洞穴中會(huì)碰到蛇h(yuǎn)p-20,繼續(xù)向下會(huì)到一個(gè)未知空間,游戲結(jié)束。
選擇右邊,會(huì)到沙灘 獲得涼爽的水hp+70,但是由于沒(méi)有游泳裝備,如果繼續(xù)選擇游泳,會(huì)遇到海嘯而死亡。
代碼
#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?”,[“l(fā)eft”,“right”])
if direction == “l(fā)eft”:
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!?。 保?/p>
break
print(“Your adventure has ended,bye~”)
函數(shù)功能塊簡(jiǎn)析
get_input(prompt,accepted) //(輸入提示,可接受字符串)
handle_room(location) //通過(guò)位置的判斷給出不同場(chǎng)景的設(shè)定和人物的走向
use_tools(tool) //人物若隨機(jī)獲得了HP類物品則立即使用,其他則無(wú)任何操作
運(yùn)行截圖
評(píng)論
查看更多