導航:首頁 > 編程語言 > python小車代碼

python小車代碼

發布時間:2022-08-24 15:28:21

python小車轉向後繼續直行如何實現

import turtle
turtle.forward(100) #前進100
turtle.penup() #抬起畫筆
turtle.right(90) #右轉90度
turtle.pendown() #放下畫筆
turtle.forward(100) #前進100
turtle.exitonclick() #單擊關閉窗口

❷ 樹莓派小車python代碼

樹莓派控制小車,python代碼要根據你採用的硬體進行編寫。不同的硬體,比如電機驅動器,感測器等等不同,代碼就不一樣,包括你使用的是樹莓派的哪些GPIO介面,這些都要先確定了,才能編寫python代碼,並沒有統一的一份代碼可以適用與任何一台樹莓派小車。

❸ 用Python畫小車車

你好,下面是一個對應的代碼
import turtle
import time

t = turtle.Pen()

def fun1(t, x, y):
t.forward(x)
t.left(y)

def fun2(t, x, y):
t.forward(x)
t.right(y)

'''
color函數有三個參數
第一個參數指定有多少紅色
第二個參數指定有多少綠色
第三個參數指定有多少藍色

都為0的時候此時為黑色
都為1的時候此時為白色

這種紅色,綠色,藍色的混搭叫做RGB
藍色和紅色混合產生紫色
黃色和紅色混合產生橙色
'''

t.color(1, 0, 0)

t.begin_fill()

fun1(t, 100, 90)
fun1(t, 20, 90)
fun2(t, 20, 90)
fun1(t, 20, 90)
fun1(t, 60, 90)
fun2(t, 20, 90)
fun1(t, 20, 90)
t.forward(20)

t.end_fill()

t.color(0, 0, 0)
t.up()
t.forward(10)
t.down()
# 開始位置
#t.begin_fill()
# 畫圓
t.circle(10)
# 結束位置
#t.end_fill()

# 設置當前的指定角度為0度
t.setheading(0)
t.up()
t.forward(90)
t.right(90)
t.forward(10)
t.setheading(0)
t.down()

#t.begin_fill()
t.circle(10)
#t.end_fill()
t.up()

time.sleep(20)

❹ 求助樹莓派避障小車,遇到 python 了

小白第一次上手 python ,用樹莓派,紅外和超聲波感測器做智障小車。 編譯錯誤:
RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.
GPIO.setup(trip,GPIO.OUT)
Traceback (most recent call last):
File "xiaochetest.py", line 82, in <mole>
fwd()
TypeError: fwd() takes exactly 1 argument (0 given)

下面是小車的代碼:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)

m1_fwd = 12
m1_rev = 11
m2_fwd = 13
m2_rev = 15
red_left = 07
red_right = 16
trip = 38
echo = 37
def init():
GPIO.setup(m1_fwd,GPIO.OUT)
GPIO.setup(m1_rev,GPIO.OUT)
GPIO.setup(m2_fwd,GPIO.OUT)
GPIO.setup(m2_rev,GPIO.OUT)
def stop(sleep_time):
GPIO.output(m1_fwd,False)
GPIO.output(m1_rev,False)
GPIO.output(m2_fwd,False)
GPIO.output(m2_rev,False)
time.sleep(sleep_time)
GPIO.cleanup()
def fwd(sleep_time):
GPIO.output(m1_fwd,GPIO.HIGH)
GPIO.output(m1_rev,GPIO.LOW)
GPIO.output(m2_fwd,GPIO.HIGH)
GPIO.output(m2_rev,GPIO.LOW)
time.sleep(sleep_time)
GPIO.cleanup()
def rev(sleep_time):
GPIO.output(m1_fwd,GPIO.LOW)
GPIO.output(m1_rev,GPIO.HIGH)
GPIO.output(m2_fwd,GPIO.LOW)
GPIO.output(m2_rev,GPIO.HIGH)
time.sleep(sleep_time)
GPIO.cleanup()
def right(sleep_time):
GPIO.output(m1_fwd,GPIO.HIGH)
GPIO.output(m1_rev,GPIO.LOW)
GPIO.output(m2_fwd,False)
GPIO.output(m2_rev,False)
time.sleep(sleep_time)
GPIO.cleanup()
def left(sleep_time):
GPIO.output(m1_fwd,False)
GPIO.output(m1_rev,False)
GPIO.output(m2_fwd,GPIO.HIGH)
GPIO.output(m2_rev,GPIO.LOW)
time.sleep(sleep_time)
GPIO.cleanup()
def get_distance():
GPIO.setup(trip,GPIO.OUT)
GPIO.setup(echo,GPIO.IN)
GPIO.output(trip,GPIO.HIGH)
time.sleep(0.000015)
GPIO.output(trip,GPIO.LOW)
while not GPIO.input(echo):
pass
t1 = time.time()
while GPIO.input(echo):
pass
t2 = time.time()
return (t2-t1)*34300/2
def turnaround():
GPIO.setup(red_left,GPIO.IN)
GPIO.setup(red_right,GPIO.IN)
while GPIO.input(red_left) and GPIO.input(red_right)==0:
rev()
if GPIO.input(red_left)==1:
left(1)
else:
right(1)
GPIO.cleanup()

while True:
distance = get_distance()
time.sleep(0.5)
if distance > 20:
fwd()
elif distance == 20:
stop()

else:
stop()
turnaround()

def fwd(sleep_time)

if distance > 20:
fwd()

調用 fwd 的時候要傳參數啊,錯誤提示說的比較清楚了。

❺ 如何用python畫一輛小車

你好,你可以撥打114查詢的哦

❻ python有趣的編程代碼

classPoint:
row=0
col=0
def__init__(self,row,col):
self.row=row
self.col=col

def(self):
returnPoint(row=self.row,col=self.col)


#初始框架
importpygame
importrandom

#初始化
pygame.init()
W=800
H=600

ROW=30
COL=40

size=(W,H)
window=pygame.display.set_mode(size)
pygame.display.set_caption('貪吃蛇')

bg_color=(255,255,255)
snake_color=(200,200,200)

head=Point(row=int(ROW/2),col=int(COL/2))
head_color=(0,128,128)

snakes=[
Point(row=head.row,col=head.col+1),
Point(row=head.row,col=head.col+2),
Point(row=head.row,col=head.col+3)
]

#生成食物
defgen_food():
while1:
pos=Point(row=random.randint(0,ROW-1),col=random.randint(0,COL-1))

#
is_coll=False

#是否跟蛇碰上了
ifhead.row==pos.rowandhead.col==pos.col:
is_coll=True

#蛇身子
forsnakeinsnakes:
ifsnake.row==pos.rowandsnake.col==pos.col:
is_coll=True
break

ifnotis_coll:
break

returnpos


#定義坐標


food=gen_food()
food_color=(255,255,0)direct='left'#left,right,up,down

#
defrect(point,color):
cell_width=W/COL
cell_height=H/ROW

left=point.col*cell_width
top=point.row*cell_height

pygame.draw.rect(
window,color,
(left,top,cell_width,cell_height)
)
pass

#游戲循環
quit=True
clock=pygame.time.Clock()
whilequit:
#處理事件
foreventinpygame.event.get():
ifevent.type==pygame.QUIT:
quit=False
elifevent.type==pygame.KEYDOWN:
ifevent.key==273orevent.key==119:
ifdirect=='left'ordirect=='right':
direct='up'
elifevent.key==274orevent.key==115:
ifdirect=='left'ordirect=='right':
direct='down'
elifevent.key==276orevent.key==97:
ifdirect=='up'ordirect=='down':
direct='left'
elifevent.key==275orevent.key==100:
ifdirect=='up'ordirect=='down':
direct='right'

#吃東西
eat=(head.row==food.rowandhead.col==food.col)

#重新產生食物
ifeat:
food=gen_food()

#處理身子
#1.把原來的頭,插入到snakes的頭上
snakes.insert(0,head.())
#2.把snakes的最後一個刪掉
ifnoteat:
snakes.pop()

#移動
ifdirect=='left':
head.col-=1
elifdirect=='right':
head.col+=1
elifdirect=='up':
head.row-=1
elifdirect=='down':
head.row+=1

#檢測
dead=False
#1.撞牆
ifhead.col<0orhead.row<0orhead.col>=COLorhead.row>=ROW:
dead=True

#2.撞自己
forsnakeinsnakes:
ifhead.col==snake.colandhead.row==snake.row:
dead=True
break

ifdead:
print('死了')
quit=False

#渲染——畫出來
#背景
pygame.draw.rect(window,bg_color,(0,0,W,H))

#蛇頭
forsnakeinsnakes:
rect(snake,snake_color)
rect(head,head_color)
rect(food,food_color)

#
pygame.display.flip()

#設置幀頻(速度)
clock.tick(8)

#收尾工作

這是一個簡易版貪吃蛇的代碼,雖然結構簡單,但是該有的功能都是完整的,可玩性也不錯

❼ 想問一下,用Python腳本編寫的語音識別功能去控制stm32智能小車,這個可以實現嗎

你好python有一個語音識別的庫,
pip install speech
然後你就可以將一些特定的語言編程命令去控制stm32智能小車了。

❽ python 樹莓派如何控制小車轉向90度後停止

首先申明js我不會
看到樓主你的思路,我感覺你應該不會成功,看我分析一下:
當程序自啟動,python程序開始運行,running=?Ture,那麼程序將一直按照running=?Ture執行下去,外部任何輸入都是沒有用,除非程序停下來檢測一下,running的狀態,換句話說:你啟動的時候running就是Ture,那麼我就運行,你什麼時候改變狀態我也不知道,建議樓主,能在循環中檢測一下running狀態。這樣才能有效的控製程序運行,下面是都偽代碼:
while?Ture:????????#?程序一直循環開啟
????if?running:??????#?如果如果運行狀態是真
????????do?something??#?開始執行當然這里要注意一下 全局變數問題,我這沒有測試,怕全局變數會有影響!!!

閱讀全文

與python小車代碼相關的資料

熱點內容
漢壽小程序源碼 瀏覽:340
易助erp雲伺服器 瀏覽:530
修改本地賬戶管理員文件夾 瀏覽:416
python爬蟲工程師招聘 瀏覽:283
小鵬p7聽音樂哪個app好 瀏覽:354
linux下的防火牆 瀏覽:954
凌達壓縮機美芝壓縮機 瀏覽:350
php後面代碼不執行 瀏覽:236
微我手機怎樣設置應用加密 瀏覽:202
條件加密 瀏覽:628
androidstudio設置中文 瀏覽:641
汽車換壓縮機能提升製冷 瀏覽:628
安卓開發配什麼電腦 瀏覽:607
linux下php模塊 瀏覽:78
阿里雲伺服器終端在哪裡 瀏覽:148
app紙有什麼用 瀏覽:224
cuteftp命令 瀏覽:507
最開始的編程語言是什麼 瀏覽:760
at遠程命令 瀏覽:493
雲伺服器哪家好點 瀏覽:215