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