A. 如何在windows下或者linux下對樹莓派編程(C語言或C++)
Raspberry Pi(中文名為「樹莓派」,簡寫為RPi,或者RasPi/RPi)是為學生計算機編程教育而設計,只有信用卡大小的卡片式電腦,其系統基於Linux。自問世以來,受眾多計算機發燒友和創客的追捧,曾經一「派」難求。別看其外表「嬌小」,內「心」卻很強大,視頻、音頻等功能通通皆有,可謂是「麻雀雖小,五臟俱全」。
樹莓派定位於學習。可以學操作系統,編程,也可以學習嵌入編程,工業控制,人工智慧等等。它比其它的幾款比如ario要全面些。當然在某些方面也弱些。國外把樹莓派的功能發揮差不多到極致。幾乎所有的領域都有樹莓派的身影。從並行計算,到機器人,到3D列印,探索太空,家庭防盜,路由器,PAD都有。國內與國外的用法基本相同。只是國人更在於家庭用途。都是些動手能力強的人玩的小玩具。
「樹莓派」的系統是基於Linux。因此樹莓派里的編程完全和Linux相同,使用Linux下的編譯器編譯程序並運行。樹莓派不能進行windows下的編程。
常式:
//在home/pi/文件夾下建立一個hello.c文件,輸入以下內容
#include<stdio.h>
voidmain(void)
{
printf("Hello!ThisismyfirstCprogram. ");
}
//在終端輸入gcchello.c-ohello實現對hello.c的編譯和連接
//./hello執行生成的可執行文件hello
B. 樹莓派怎麼運行程序pathon
前言
Raspbian的系統中包含了Python的編程環境IDE,方便了我們對於python的學習。個人覺得還是游戲程序能夠更好的激發我們的潛力所以,話不多說下面就來使用python編寫我們簡單的游戲程序吧。
儲備知識
①import 模塊:time時間模塊、random隨機數模塊
②列表:[ ] #[]中放入值,與數組很像,但是不同
③def:定義函數
④python的縮進是編程格式的一種,if等都不需要{}括起來,關鍵看縮進區分
⑤if,while,def函數都要以 「:」開始
游戲概述
一進入游戲,主人公就會隨機獲取一個游戲已經設定的道具。最開始,主人公站在叢林的邊緣,只有左右可選,左邊是個洞穴,右邊是沙灘。
選擇左邊,則開始可以獲得手電筒,但是在洞穴中會碰到蛇hp-20,繼續向下會到一個未知空間,游戲結束。
選擇右邊,會到沙灘 獲得涼爽的水hp+70,但是由於沒有游泳裝備,如果繼續選擇游泳,會遇到海嘯而死亡。
代碼
[python] view plain 在CODE上查看代碼片派生到我的代碼片
#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~")
C. 使用PyCharm連接樹莓派遠程編程
這篇教程是關於如何在Windows操作系統上使用PyCharm IDE遠程連接樹莓派執行Python 2.7程序。 這篇教程環境是基於運行Windows 10電腦和運行Raspbian OS的樹莓派。
第一步 : 確認Windows電腦和樹莓派在同一個網路里。
第二步: 在你的Windows電腦上安裝PyCharm Professional Edition。
第三步: 必須獲取到樹莓派的IP地址. 打開樹莓派的終端窗口輸入以下命令: ifconfig 。
第九步: 配置遠程Python Interpreter。