Python 如何使用超声波传感器上下移动精灵?

Python 如何使用超声波传感器上下移动精灵?,python,raspberry-pi,flappy-bird-clone,Python,Raspberry Pi,Flappy Bird Clone,我正在尝试制作一个类似于python中flappy bird的游戏,并使用超声波传感器作为控制器(它都连接到一个树莓pi)。如果距离10,则鸟向上移动。我不太确定如何将距离与鸟的运动联系起来 我试着查找各种py游戏命令,但到目前为止我没有任何运气 这是我到目前为止的代码 import RPi.GPIO as GPIO import time import sys import pygame pygame.init() GPIO.setmode(GPIO.B

我正在尝试制作一个类似于python中flappy bird的游戏,并使用超声波传感器作为控制器(它都连接到一个树莓pi)。如果距离<10,则鸟向下移动,如果距离>10,则鸟向上移动。我不太确定如何将距离与鸟的运动联系起来

我试着查找各种py游戏命令,但到目前为止我没有任何运气

这是我到目前为止的代码

import RPi.GPIO as GPIO
import time
import sys
import pygame                   
pygame.init()

GPIO.setmode(GPIO.BCM)

TRIG = 4
ECHO = 18

GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)

def get_distance():                      
    GPIO.output(TRIG, True)
    time.sleep(0.00001)
    GPIO.output(TRIG, False)

    while GPIO.input(ECHO) == False:
        start = time.time()

    while GPIO.input(ECHO) == True:
        end = time.time()

    sig_time = end-start            

    distance = sig_time / 0.000058 

    print ('Distance: {} cm'.format(distance))
    return distance


#   distance = get_distance()  ### stuck here
 #   if distance < 10:
       # bird1.

screenwidth = 600               #creating the world
screenheight = 400
speed = [4,4]
black = 0,0,0
skyblue = 0,231,252

screen = pygame.display.set_mode((600, 400))

bird1 = pygame.image.load("bird1.png")      
bird2 = pygame.image.load("bird2.png")
pipe = pygame.image.load("pipe1.png")
gameover = pygame.image.load("game-over.png")
gameover = pygame.transform.scale(gameover,(width,height))
gameoverrect = gameover.get_rect()

bird1height = bird1.get_height()      
bird1 = pygame.transform.scale(bird1,(int(bird1.get_height()*0.6),int(bird1.get_height()*0.6)))     #making the bird smaller to fit the screen better
bird2 = pygame.transform.scale(bird2,(int(bird2.get_height()*0.6),int(bird2.get_height()*0.6)))
bird1rect = bird1.get_rect()         
print ('bird1rect')                 #positioning 
bird1rect.x = 20
piperect = pipe.get_rect()
pipestartx = 600
pipestarty = 0
piperect.x, piperect.y = pipestartx, pipestarty
for x in range(0,600):
    screen.blit(bird1,(x,200))                                                                                                                                               


class pipe():                               #creating the pipe
    def __init__(self,startx,position = "top",pipeHeight = 200):
        self.sprite = pygame.image.load("pipe1.png")
        self.sprite = pygame.transform.scale(self.sprite,(50,pipeHeight))
        self.rect = self.sprite.get_rect()
        self.rect.x = startx

        if position == "top":
            self.rect.y = 0
        else:
            self.sprite = pygame.transform.rotate(self.sprite,180)
            self.rect.y = height - self.sprite.get_height()

pipelist = []                           #this creates the pipes in the world
pipelist.append(pipe(200,"top",220))
pipelist.append(pipe(200,"bottom",90))
pipelist.append(pipe(600,"top",140))
pipelist.append(pipe(600,"bottom",190))
pipelist.append(pipe(800,"bottom",100))
pipelist.append(pipe(800,"top",220))
pipelist.append(pipe(1000,"top",100))
pipelist.append(pipe(1000,"bottom",220))
pipelist.append(pipe(1200,"top",280))
pipelist.append(pipe(1200,"bottom",50))

fontObj = pygame.font.Font("C:\Windows\Fonts\Arial.ttf",40)       #creating a new font object
将RPi.GPIO导入为GPIO
导入时间
导入系统
导入pygame
pygame.init()
GPIO.setmode(GPIO.BCM)
TRIG=4
回声=18
GPIO.setup(触发,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)
def get_距离():
GPIO.output(触发,真)
睡眠时间(0.00001)
GPIO.output(触发,错误)
当GPIO.input(回波)=False时:
开始=时间。时间()
当GPIO.input(ECHO)=True时:
end=time.time()
信号时间=结束-开始
距离=信号时间/0.000058
打印('距离:{}cm'。格式(距离))
返回距离
#距离=获得距离()卡在这里
#如果距离小于10:
#鸟1。
屏幕宽度=600#创造世界
屏幕高度=400
速度=[4,4]
黑色=0,0,0
天蓝色=0231252
screen=pygame.display.set_模式((600400))
bird1=pygame.image.load(“bird1.png”)
bird2=pygame.image.load(“bird2.png”)
pipe=pygame.image.load(“pipe1.png”)
gameover=pygame.image.load(“game over.png”)
gameover=pygame.transform.scale(gameover,(宽度,高度))
gameoverrect=gameover.get_rect()
bird1height=bird1.get_height()
bird1=pygame.transform.scale(bird1,(int(bird1.get_height()*0.6),int(bird1.get_height()*0.6)))#使鸟更小以更好地适应屏幕
bird2=pygame.transform.scale(bird2,(int(bird2.get_height()*0.6),int(bird2.get_height()*0.6)))
bird1rect=bird1.get_rect()
打印('birdrect')#定位
bird1rect.x=20
piperect=pipe.get_rect()
pipestartx=600
pipestarty=0
piperect.x,piperect.y=pipestartx,pipestarty
对于范围(0600)内的x:
屏幕光点(bird1,(x,200))
类管道():#创建管道
def _;初始(自,起始,位置=“顶部”,管道高度=200):
self.sprite=pygame.image.load(“pipe1.png”)
self.sprite=pygame.transform.scale(self.sprite,(50,pipeHeight))
self.rect=self.sprite.get_rect()
self.rect.x=startx
如果位置==“顶部”:
自校正y=0
其他:
self.sprite=pygame.transform.rotate(self.sprite,180)
self.rect.y=height-self.sprite.get_height()
pipelist=[]#这将在世界上创建管道
管道列表。附加(管道(200,“顶部”,220))
管道列表。附加(管道(200,“底部”,90))
管道列表。附加(管道(600,“顶部”,140))
管道列表。附加(管道(600,“底部”,190))
管道列表。附加(管道(800,“底部”,100))
管道列表。附加(管道(800,“顶部”,220))
管道列表。附加(管道(1000,“顶部”,100))
管道列表。追加(管道(1000,“底部”,220))
管道列表。附加(管道(1200,“顶部”,280))
管道列表。附加(管道(1200,“底部”,50))
fontObj=pygame.font.font(“C:\Windows\Fonts\Arial.ttf”,40)#创建一个新的字体对象
我希望能够使用超声波传感器作为控制器来移动这只鸟。请随时提供可以帮助我的在线资源链接,我很想了解更多