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。