Leap Motion + Pygame |在窗口中显示文本

时间:2013-08-10 03:31:18

标签: python class variables methods pygame

我正在尝试将pygame库与Leap Motion一起使用。我正试图在我的窗口中显示Leap Motion中的一些数据,但是当我尝试从SampleListener类中检索变量并将其输入我的Main方法时,我收到此错误:

  

AttributeError:'function'对象没有属性'avg_pos'

有什么我对Class>的误解吗?方法>变结构?

以下是我正在使用的代码:

import Leap, sys, pygame

#Window 
windowWidth = 800
windowHeight = 600

#Leap Motion Settings
class SampleListener(Leap.Listener):

    def on_init(self, controller):
        print "Initialized"

    def on_connect(self, controller):
        print "Connected"

    def on_disconnect(self, controller):
        # Note: not dispatched when running in a debugger.
        print "Disconnected"

    def on_exit(self, controller):
        print "Exited"

    def on_frame(self, controller):
        # Get the most recent frame and report some basic information
            if not self.frame.hands.empty:
                # Get the first hand
                hand = self.frame.hands[0]

                # Check if the hand has any fingers
                fingers = hand.fingers
                if not fingers.empty:
                    # Calculate the hand's average finger tip position
                    avg_pos = Leap.Vector()
                    for finger in fingers:
                        avg_pos += finger.tip_position
                    avg_pos /= len(fingers)
                    print "Hand has %d fingers, average finger tip position: %s" % (
                          len(fingers), avg_pos)

#Application Loop
def main():
    pygame.init()
    window = pygame.display.set_mode((windowWidth,windowHeight))
    pygame.display.set_caption("TEST123")

    #Surface 
    background = pygame.Surface(window.get_size())
    background = background.convert()
    background.fill ((50,0,80))

    if pygame.font:
        font = pygame.font.Font(None, 36)
        text = font.render("%s" % SampleListener.on_frame.len(fingers), 1, (255, 255, 255))
        textpos = text.get_rect(centerx=background.get_width()/2)
        background.blit(text, textpos)

    # Create a sample listener and controller
    listener = SampleListener()
    controller = Leap.Controller()

    # Have the sample listener receive events from the controller
    controller.add_listener(listener)

    clock = pygame.time.Clock()

    while True:
        clock.tick(60)
        #Handle Input Events
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
                # Remove the sample listener when done
                controller.remove_listener(listener)

        window.blit(background, (0,0))
        pygame.display.flip()

if __name__ == "__main__":
    main()

编辑:这是完整的回溯错误 -

Line 53, in main
    text = font.render("%s" % SampleListener.on_frame.avg_pos, 1, (255, 255, 255))
AttributeError: 'function' object has no attribute 'avg_pos'

谢谢!

1 个答案:

答案 0 :(得分:0)

对我来说,这似乎可能是pygame.font不是布尔值,因此不能在if语句中使用。 ( 引用:

def main():
pygame.init()
window = pygame.display.set_mode((windowWidth,windowHeight))
pygame.display.set_caption("TEST123")

#Surface 
background = pygame.Surface(window.get_size())
background = background.convert()
background.fill ((50,0,80))

if pygame.font: \\ HERE   <---
    font = pygame.font.Font(None, 36)
    text = font.render("%s" % SampleListener.on_frame.len(fingers), 1, (255, 255, 255))
    textpos = text.get_rect(centerx=background.get_width()/2)
    background.blit(text, textpos)