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

时间:2019-04-30 23:55:27

标签: python raspberry-pi flappy-bird-clone

我正在尝试使用python制作类似于飞盘鸟的游戏,并使用超声波传感器作为控制器(它们都连接到树莓派)。如果距离<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

我希望能够使用超声波传感器作为控制器来移动鸟类。随时提供指向在线资源的链接,这些链接可以为我提供帮助,我想了解更多信息。

0 个答案:

没有答案
相关问题